netdev: Factor restoring flags into new "struct netdev_saved_flags".
[sliver-openvswitch.git] / lib / netdev-bsd.c
1 /*
2  * Copyright (c) 2011 Gaetano Catalli.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
26 #include <sys/sockio.h>
27 #include <ifaddrs.h>
28 #include <pcap/pcap.h>
29 #include <net/if.h>
30 #include <net/if_dl.h>
31 #include <net/if_media.h>
32 #include <net/if_tap.h>
33 #include <netinet/in.h>
34 #include <net/if_mib.h>
35 #include <poll.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <sys/sysctl.h>
39
40 #include "rtbsd.h"
41 #include "coverage.h"
42 #include "dynamic-string.h"
43 #include "fatal-signal.h"
44 #include "netdev-provider.h"
45 #include "ofpbuf.h"
46 #include "openflow/openflow.h"
47 #include "packets.h"
48 #include "poll-loop.h"
49 #include "socket-util.h"
50 #include "shash.h"
51 #include "svec.h"
52 #include "util.h"
53 #include "vlog.h"
54
55 VLOG_DEFINE_THIS_MODULE(netdev_bsd);
56
57 \f
58 /*
59  * This file implements objects to access interfaces.
60  * Externally, interfaces are represented by two structures:
61  *   + struct netdev_dev, representing a network device,
62  *     containing e.g. name and a refcount;
63  *     We can have private variables by embedding the
64  *     struct netdev_dev into our own structure
65  *     (e.g. netdev_dev_bsd)
66  *
67  *   + struct netdev, representing an instance of an open netdev_dev.
68  *     The structure contains a pointer to the 'struct netdev'
69  *     representing the device. Again, private information
70  *     such as file descriptor etc. are stored in our
71  *     own struct netdev_bsd which includes a struct netdev.
72  *
73  * Both 'struct netdev' and 'struct netdev_dev' are referenced
74  * in containers which hold pointers to the data structures.
75  * We can reach our own struct netdev_XXX_bsd by putting a
76  * struct netdev_XXX within our own struct, and using CONTAINER_OF
77  * to access the parent structure.
78  */
79 struct netdev_bsd {
80     struct netdev netdev;
81
82     int netdev_fd;   /* Selectable file descriptor for the network device.
83                         This descriptor will be used for polling operations */
84
85     pcap_t *pcap_handle;  /* Packet capture descriptor for a system network
86                              device */
87 };
88
89 struct netdev_dev_bsd {
90     struct netdev_dev netdev_dev;
91     unsigned int cache_valid;
92     unsigned int change_seq;
93
94     int ifindex;
95     uint8_t etheraddr[ETH_ADDR_LEN];
96     struct in_addr in4;
97     struct in6_addr in6;
98     int mtu;
99     int carrier;
100
101     bool tap_opened;
102     int tap_fd;         /* TAP character device, if any */
103 };
104
105
106 enum {
107     VALID_IFINDEX = 1 << 0,
108     VALID_ETHERADDR = 1 << 1,
109     VALID_IN4 = 1 << 2,
110     VALID_IN6 = 1 << 3,
111     VALID_MTU = 1 << 4,
112     VALID_CARRIER = 1 << 5
113 };
114
115 /* An AF_INET socket (used for ioctl operations). */
116 static int af_inet_sock = -1;
117
118 #define PCAP_SNAPLEN 2048
119
120
121 /*
122  * Notifier used to invalidate device informations in case of status change.
123  *
124  * It will be registered with a 'rtbsd_notifier_register()' when the first
125  * device will be created with the call of either 'netdev_bsd_tap_create()' or
126  * 'netdev_bsd_system_create()'.
127  *
128  * The callback associated with this notifier ('netdev_bsd_cache_cb()') will
129  * invalidate cached information about the device.
130  */
131 static struct rtbsd_notifier netdev_bsd_cache_notifier;
132 static int cache_notifier_refcount;
133
134 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
135
136 static int netdev_bsd_do_ioctl(const char *, struct ifreq *, unsigned long cmd,
137                                const char *cmd_name);
138 static void destroy_tap(int fd, const char *name);
139 static int get_flags(const struct netdev_dev *, int *flagsp);
140 static int set_flags(const char *, int flags);
141 static int do_set_addr(struct netdev *netdev,
142                        int ioctl_nr, const char *ioctl_name,
143                        struct in_addr addr);
144 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
145 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
146                          int hwaddr_len, const uint8_t[ETH_ADDR_LEN]);
147 static int get_ifindex(const struct netdev *, int *ifindexp);
148
149 static int netdev_bsd_init(void);
150
151 static bool
152 is_netdev_bsd_class(const struct netdev_class *netdev_class)
153 {
154     return netdev_class->init == netdev_bsd_init;
155 }
156
157 static struct netdev_bsd *
158 netdev_bsd_cast(const struct netdev *netdev)
159 {
160     ovs_assert(is_netdev_bsd_class(netdev_dev_get_class(
161                                        netdev_get_dev(netdev))));
162     return CONTAINER_OF(netdev, struct netdev_bsd, netdev);
163 }
164
165 static struct netdev_dev_bsd *
166 netdev_dev_bsd_cast(const struct netdev_dev *netdev_dev)
167 {
168     ovs_assert(is_netdev_bsd_class(netdev_dev_get_class(netdev_dev)));
169     return CONTAINER_OF(netdev_dev, struct netdev_dev_bsd, netdev_dev);
170 }
171
172 /* Initialize the AF_INET socket used for ioctl operations */
173 static int
174 netdev_bsd_init(void)
175 {
176     static int status = -1;
177
178     if (status >= 0) {  /* already initialized */
179         return status;
180     }
181
182     af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
183     status = af_inet_sock >= 0 ? 0 : errno;
184
185     if (status) {
186         VLOG_ERR("failed to create inet socket: %s", strerror(status));
187     }
188
189     return status;
190 }
191
192 /*
193  * Perform periodic work needed by netdev. In BSD netdevs it checks for any
194  * interface status changes, and eventually calls all the user callbacks.
195  */
196 static void
197 netdev_bsd_run(void)
198 {
199     rtbsd_notifier_run();
200 }
201
202 /*
203  * Arranges for poll_block() to wake up if the "run" member function needs to
204  * be called.
205  */
206 static void
207 netdev_bsd_wait(void)
208 {
209     rtbsd_notifier_wait();
210 }
211
212 static void
213 netdev_dev_bsd_changed(struct netdev_dev_bsd *dev)
214 {
215     dev->change_seq++;
216     if (!dev->change_seq) {
217         dev->change_seq++;
218     }
219 }
220
221 /* Invalidate cache in case of interface status change. */
222 static void
223 netdev_bsd_cache_cb(const struct rtbsd_change *change,
224                       void *aux OVS_UNUSED)
225 {
226     struct netdev_dev_bsd *dev;
227
228     if (change) {
229         struct netdev_dev *base_dev = netdev_dev_from_name(change->if_name);
230
231         if (base_dev) {
232             const struct netdev_class *netdev_class =
233                                                 netdev_dev_get_class(base_dev);
234
235             if (is_netdev_bsd_class(netdev_class)) {
236                 dev = netdev_dev_bsd_cast(base_dev);
237                 dev->cache_valid = 0;
238                 netdev_dev_bsd_changed(dev);
239             }
240         }
241     } else {
242         /*
243          * XXX the API is lacking, we should be able to iterate on the list of
244          * netdevs without having to store the info in a temp shash.
245          */
246         struct shash device_shash;
247         struct shash_node *node;
248
249         shash_init(&device_shash);
250         netdev_dev_get_devices(&netdev_bsd_class, &device_shash);
251         SHASH_FOR_EACH (node, &device_shash) {
252             dev = node->data;
253             dev->cache_valid = 0;
254             netdev_dev_bsd_changed(dev);
255         }
256         shash_destroy(&device_shash);
257     }
258 }
259
260 static int
261 cache_notifier_ref(void)
262 {
263     int ret = 0;
264
265     if (!cache_notifier_refcount) {
266         ret = rtbsd_notifier_register(&netdev_bsd_cache_notifier,
267                                                 netdev_bsd_cache_cb, NULL);
268         if (ret) {
269             return ret;
270         }
271     }
272     cache_notifier_refcount++;
273     return 0;
274 }
275
276 static int
277 cache_notifier_unref(void)
278 {
279     cache_notifier_refcount--;
280     if (cache_notifier_refcount == 0) {
281         rtbsd_notifier_unregister(&netdev_bsd_cache_notifier);
282     }
283     return 0;
284 }
285
286 /* Allocate a netdev_dev_bsd structure */
287 static int
288 netdev_bsd_create_system(const struct netdev_class *class, const char *name,
289                   struct netdev_dev **netdev_devp)
290 {
291     struct netdev_dev_bsd *netdev_dev;
292     int error;
293
294     error = cache_notifier_ref();
295     if (error) {
296         return error;
297     }
298
299     netdev_dev = xzalloc(sizeof *netdev_dev);
300     netdev_dev->change_seq = 1;
301     netdev_dev_init(&netdev_dev->netdev_dev, name, class);
302     *netdev_devp = &netdev_dev->netdev_dev;
303
304     return 0;
305 }
306
307 /*
308  * Allocate a netdev_dev_bsd structure with 'tap' class.
309  */
310 static int
311 netdev_bsd_create_tap(const struct netdev_class *class, const char *name,
312                   struct netdev_dev **netdev_devp)
313 {
314     struct netdev_dev_bsd *netdev_dev = NULL;
315     int error = 0;
316     struct ifreq ifr;
317
318     error = cache_notifier_ref();
319     if (error) {
320         goto error;
321     }
322
323     /* allocate the device structure and set the internal flag */
324     netdev_dev = xzalloc(sizeof *netdev_dev);
325
326     memset(&ifr, 0, sizeof(ifr));
327
328     /* Create a tap device by opening /dev/tap.  The TAPGIFNAME ioctl is used
329      * to retrieve the name of the tap device. */
330     netdev_dev->tap_fd = open("/dev/tap", O_RDWR);
331     netdev_dev->change_seq = 1;
332     if (netdev_dev->tap_fd < 0) {
333         error = errno;
334         VLOG_WARN("opening \"/dev/tap\" failed: %s", strerror(error));
335         goto error_undef_notifier;
336     }
337
338     /* Retrieve tap name (e.g. tap0) */
339     if (ioctl(netdev_dev->tap_fd, TAPGIFNAME, &ifr) == -1) {
340         /* XXX Need to destroy the device? */
341         error = errno;
342         goto error_undef_notifier;
343     }
344
345     /* Change the name of the tap device */
346     ifr.ifr_data = (void *)name;
347     if (ioctl(af_inet_sock, SIOCSIFNAME, &ifr) == -1) {
348         error = errno;
349         destroy_tap(netdev_dev->tap_fd, ifr.ifr_name);
350         goto error_undef_notifier;
351     }
352
353     /* set non-blocking. */
354     error = set_nonblocking(netdev_dev->tap_fd);
355     if (error) {
356         destroy_tap(netdev_dev->tap_fd, name);
357         goto error_undef_notifier;
358     }
359
360     /* Turn device UP */
361     ifr.ifr_flags = (uint16_t)IFF_UP;
362     ifr.ifr_flagshigh = 0;
363     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
364     if (ioctl(af_inet_sock, SIOCSIFFLAGS, &ifr) == -1) {
365         error = errno;
366         destroy_tap(netdev_dev->tap_fd, name);
367         goto error_undef_notifier;
368     }
369
370     /* initialize the device structure and
371      * link the structure to its netdev */
372     netdev_dev_init(&netdev_dev->netdev_dev, name, class);
373     *netdev_devp = &netdev_dev->netdev_dev;
374
375     return 0;
376
377 error_undef_notifier:
378     cache_notifier_unref();
379 error:
380     free(netdev_dev);
381     return error;
382 }
383
384 static void
385 netdev_bsd_destroy(struct netdev_dev *netdev_dev_)
386 {
387     struct netdev_dev_bsd *netdev_dev = netdev_dev_bsd_cast(netdev_dev_);
388
389     cache_notifier_unref();
390
391     if (netdev_dev->tap_fd >= 0 &&
392             !strcmp(netdev_dev_get_type(netdev_dev_), "tap")) {
393         destroy_tap(netdev_dev->tap_fd, netdev_dev_get_name(netdev_dev_));
394     }
395     free(netdev_dev);
396 }
397
398
399 static int
400 netdev_bsd_open_system(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
401 {
402     struct netdev_bsd *netdev;
403     int error;
404     enum netdev_flags flags;
405
406     /* Allocate network device. */
407     netdev = xcalloc(1, sizeof *netdev);
408     netdev->netdev_fd = -1;
409     netdev_init(&netdev->netdev, netdev_dev_);
410
411     /* Verify that the netdev really exists by attempting to read its flags */
412     error = netdev_get_flags(&netdev->netdev, &flags);
413     if (error == ENXIO) {
414         goto error;
415     }
416
417     *netdevp = &netdev->netdev;
418     return 0;
419
420 error:
421     netdev_uninit(&netdev->netdev, true);
422     return error;
423 }
424
425
426
427 /* Close a 'netdev'. */
428 static void
429 netdev_bsd_close(struct netdev *netdev_)
430 {
431     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
432
433     if (netdev->netdev_fd >= 0 && strcmp(netdev_get_type(netdev_), "tap")) {
434         pcap_close(netdev->pcap_handle);
435     }
436
437     free(netdev);
438 }
439
440 static int
441 netdev_bsd_listen(struct netdev *netdev_)
442 {
443     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
444     struct netdev_dev_bsd *netdev_dev =
445                               netdev_dev_bsd_cast(netdev_get_dev(netdev_));
446
447     char errbuf[PCAP_ERRBUF_SIZE];
448     int error;
449     int fd = -1;
450     int one = 1;
451
452     if (netdev->netdev_fd >= 0) {
453         return 0;
454     }
455
456     if (!strcmp(netdev_get_type(netdev_), "tap") &&
457             !netdev_dev->tap_opened) {
458         netdev->netdev_fd = netdev_dev->tap_fd;
459         netdev_dev->tap_opened = true;
460         return 0;
461     }
462
463     /* open the pcap device. The device is opened in non-promiscuous mode
464      * because the interface flags are manually set by the caller. */
465     errbuf[0] = '\0';
466     netdev->pcap_handle = pcap_open_live(netdev_get_name(netdev_), PCAP_SNAPLEN,
467                                      0, 1000, errbuf);
468     if (netdev->pcap_handle == NULL) {
469         VLOG_ERR("%s: pcap_open_live failed: %s",
470                 netdev_get_name(netdev_), errbuf);
471         error = EIO;
472         goto error;
473     } else if (errbuf[0] !=  '\0') {
474         VLOG_WARN("%s: pcap_open_live: %s",
475                 netdev_get_name(netdev_), errbuf);
476     }
477
478     netdev_dev_bsd_changed(netdev_dev_bsd_cast(netdev_get_dev(netdev_)));
479
480     /* initialize netdev->netdev_fd */
481     fd = pcap_get_selectable_fd(netdev->pcap_handle);
482     if (fd == -1) {
483         error = errno;
484         goto error;
485     }
486
487     /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
488      * on the file descriptor returned by pcap_get_selectable_fd to achieve
489      * a real non-blocking behaviour.*/
490     error = pcap_setnonblock(netdev->pcap_handle, 1, errbuf);
491     if (error == -1) {
492         error = errno;
493         goto error;
494     }
495
496     /* This call assure that reads return immediately upon packet reception.
497      * Otherwise, a read will block until either the kernel buffer becomes
498      * full or a timeout occurs. */
499     if(ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) {
500         VLOG_ERR("ioctl(BIOCIMMEDIATE) on %s device failed: %s",
501                 netdev_get_name(netdev_), strerror(errno));
502         error = errno;
503         goto error;
504     }
505
506     /* Capture only incoming packets */
507     error = pcap_setdirection(netdev->pcap_handle, PCAP_D_IN);
508     if (error == -1) {
509         error = errno;
510         goto error;
511     }
512
513     netdev->netdev_fd = fd;
514     return 0;
515
516 error:
517     if (fd >= 0) {
518         close(netdev->netdev_fd);
519     }
520     return error;
521 }
522
523
524 /* The recv callback of the netdev class returns the number of bytes of the
525  * received packet.
526  *
527  * This can be done by the pcap_next() function. Unfortunately pcap_next() does
528  * not make difference between a missing packet on the capture interface and
529  * an error during the file capture.  We can use the pcap_dispatch() function
530  * instead, which is able to distinguish between errors and null packet.
531  *
532  * To make pcap_dispatch() returns the number of bytes read from the interface
533  * we need to define the following callback and argument.
534  */
535 struct pcap_arg {
536     void *data;
537     int size;
538     int retval;
539 };
540
541 /*
542  * This callback will be executed on every captured packet.
543  *
544  * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
545  * pcap returns a truncated packet and we follow this behavior.
546  *
547  * The argument args->retval is the packet size in bytes.
548  */
549 static void
550 proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
551 {
552     struct pcap_arg *args = (struct pcap_arg *)args_;
553
554     if (args->size < hdr->len) {
555         VLOG_WARN_RL(&rl, "packet truncated");
556         args->retval = args->size;
557     } else {
558         args->retval = hdr->len;
559     }
560
561     /* copy the packet to our buffer */
562     memcpy(args->data, packet, args->retval);
563 }
564
565 /*
566  * This function attempts to receive a packet from the specified network
567  * device. It is assumed that the network device is a system device or a tap
568  * device opened as a system one. In this case the read operation is performed
569  * on the 'netdev' pcap descriptor.
570  */
571 static int
572 netdev_bsd_recv_system(struct netdev_bsd *netdev, void *data, size_t size)
573 {
574     struct pcap_arg arg;
575     int ret;
576
577     if (netdev->netdev_fd < 0) {
578         return -EAGAIN;
579     }
580
581     /* prepare the pcap argument to store the packet */
582     arg.size = size;
583     arg.data = data;
584
585     for (;;) {
586         ret = pcap_dispatch(netdev->pcap_handle, 1, proc_pkt, (u_char *)&arg);
587
588         if (ret > 0) {
589             return arg.retval;  /* arg.retval < 0 is handled in the caller */
590         }
591         if (ret == -1) {
592             if (errno == EINTR) {
593                  continue;
594             }
595         }
596
597         return -EAGAIN;
598     }
599 }
600
601 /*
602  * This function attempts to receive a packet from the specified network
603  * device. It is assumed that the network device is a tap device and the
604  * 'netdev_fd' member of the 'netdev' structure is initialized with the tap
605  * file descriptor.
606  */
607 static int
608 netdev_bsd_recv_tap(struct netdev_bsd *netdev, void *data, size_t size)
609 {
610     if (netdev->netdev_fd < 0) {
611         return -EAGAIN;
612     }
613
614     for (;;) {
615         ssize_t retval = read(netdev->netdev_fd, data, size);
616         if (retval >= 0) {
617             return retval;
618         } else if (errno != EINTR) {
619             if (errno != EAGAIN) {
620                 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
621                              strerror(errno), netdev->netdev.netdev_dev->name);
622             }
623             return -errno;
624         }
625     }
626 }
627
628
629 /*
630  * According with the nature of the device a different function must be called.
631  * If the device is the bridge local port the 'netdev_bsd_recv_tap' function
632  * must be called, otherwise the 'netdev_bsd_recv_system' function is called.
633  *
634  * type!="tap"                                        --->  system device.
635  * type=="tap" && netdev_fd == tap_fd                 --->  internal tap device
636  * type=="tap" && netdev_fd != tap_fd                 --->  internal tap device
637  *                                                          opened as a system
638  *                                                          device.
639  */
640 static int
641 netdev_bsd_recv(struct netdev *netdev_, void* data, size_t size)
642 {
643     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
644     struct netdev_dev_bsd * netdev_dev =
645         netdev_dev_bsd_cast(netdev_get_dev(netdev_));
646
647     if (!strcmp(netdev_get_type(netdev_), "tap") &&
648             netdev->netdev_fd == netdev_dev->tap_fd) {
649         return netdev_bsd_recv_tap(netdev, data, size);
650     } else {
651         return netdev_bsd_recv_system(netdev, data, size);
652     }
653 }
654
655
656 /*
657  * Registers with the poll loop to wake up from the next call to poll_block()
658  * when a packet is ready to be received with netdev_recv() on 'netdev'.
659  */
660 static void
661 netdev_bsd_recv_wait(struct netdev *netdev_)
662 {
663     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
664
665     if (netdev->netdev_fd >= 0) {
666         poll_fd_wait(netdev->netdev_fd, POLLIN);
667     }
668 }
669
670 /* Discards all packets waiting to be received from 'netdev'. */
671 static int
672 netdev_bsd_drain(struct netdev *netdev_)
673 {
674     struct ifreq ifr;
675     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
676
677     strcpy(ifr.ifr_name, netdev_get_name(netdev_));
678     if (ioctl(netdev->netdev_fd, BIOCFLUSH, &ifr) == -1) {
679         VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
680                     netdev_get_name(netdev_), strerror(errno));
681         return errno;
682     }
683     return 0;
684 }
685
686 /*
687  * Send a packet on the specified network device. The device could be either a
688  * system or a tap device.
689  */
690 static int
691 netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size)
692 {
693     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
694     struct netdev_dev_bsd * netdev_dev =
695         netdev_dev_bsd_cast(netdev_get_dev(netdev_));
696
697     if (netdev->netdev_fd < 0) {
698         return EPIPE;
699     }
700
701     for (;;) {
702         ssize_t retval;
703         if (!strcmp(netdev_get_type(netdev_), "tap") &&
704                 netdev_dev->tap_fd == netdev->netdev_fd) {
705             retval = write(netdev->netdev_fd, data, size);
706         } else {
707             retval = pcap_inject(netdev->pcap_handle, data, size);
708         }
709         if (retval < 0) {
710             if (errno == EINTR) {
711                 continue;
712             } else if (errno != EAGAIN) {
713                 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
714                              netdev_get_name(netdev_), strerror(errno));
715             }
716             return errno;
717         } else if (retval != size) {
718             VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
719                          "%zu) on %s", retval, size,
720                          netdev_get_name(netdev_));
721            return EMSGSIZE;
722         } else {
723             return 0;
724         }
725     }
726 }
727
728 /*
729  * Registers with the poll loop to wake up from the next call to poll_block()
730  * when the packet transmission queue has sufficient room to transmit a packet
731  * with netdev_send().
732  */
733 static void
734 netdev_bsd_send_wait(struct netdev *netdev_)
735 {
736     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
737
738     if (netdev->netdev_fd < 0) { /* Nothing to do. */
739         return;
740     }
741
742     if (strcmp(netdev_get_type(netdev_), "tap")) {
743         poll_fd_wait(netdev->netdev_fd, POLLOUT);
744     } else {
745         /* TAP device always accepts packets. */
746         poll_immediate_wake();
747     }
748 }
749
750 /*
751  * Attempts to set 'netdev''s MAC address to 'mac'.  Returns 0 if successful,
752  * otherwise a positive errno value.
753  */
754 static int
755 netdev_bsd_set_etheraddr(struct netdev *netdev_,
756                            const uint8_t mac[ETH_ADDR_LEN])
757 {
758     struct netdev_dev_bsd *netdev_dev =
759                                 netdev_dev_bsd_cast(netdev_get_dev(netdev_));
760     int error;
761
762     if (!(netdev_dev->cache_valid & VALID_ETHERADDR)
763         || !eth_addr_equals(netdev_dev->etheraddr, mac)) {
764         error = set_etheraddr(netdev_get_name(netdev_), AF_LINK, ETH_ADDR_LEN,
765                               mac);
766         if (!error) {
767             netdev_dev->cache_valid |= VALID_ETHERADDR;
768             memcpy(netdev_dev->etheraddr, mac, ETH_ADDR_LEN);
769             netdev_dev_bsd_changed(netdev_dev);
770         }
771     } else {
772         error = 0;
773     }
774     return error;
775 }
776
777 /*
778  * Returns a pointer to 'netdev''s MAC address.  The caller must not modify or
779  * free the returned buffer.
780  */
781 static int
782 netdev_bsd_get_etheraddr(const struct netdev *netdev_,
783                            uint8_t mac[ETH_ADDR_LEN])
784 {
785     struct netdev_dev_bsd *netdev_dev =
786         netdev_dev_bsd_cast(netdev_get_dev(netdev_));
787
788     if (!(netdev_dev->cache_valid & VALID_ETHERADDR)) {
789         int error = get_etheraddr(netdev_get_name(netdev_),
790                                   netdev_dev->etheraddr);
791         if (error) {
792             return error;
793         }
794         netdev_dev->cache_valid |= VALID_ETHERADDR;
795     }
796     memcpy(mac, netdev_dev->etheraddr, ETH_ADDR_LEN);
797
798     return 0;
799 }
800
801 /*
802  * Returns the maximum size of transmitted (and received) packets on 'netdev',
803  * in bytes, not including the hardware header; thus, this is typically 1500
804  * bytes for Ethernet devices.
805  */
806 static int
807 netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
808 {
809     struct netdev_dev_bsd *netdev_dev =
810         netdev_dev_bsd_cast(netdev_get_dev(netdev_));
811
812     if (!(netdev_dev->cache_valid & VALID_MTU)) {
813         struct ifreq ifr;
814         int error;
815
816         error = netdev_bsd_do_ioctl(netdev_get_name(netdev_), &ifr, SIOCGIFMTU,
817                                     "SIOCGIFMTU");
818         if (error) {
819             return error;
820         }
821         netdev_dev->mtu = ifr.ifr_mtu;
822         netdev_dev->cache_valid |= VALID_MTU;
823     }
824
825     *mtup = netdev_dev->mtu;
826     return 0;
827 }
828
829 static int
830 netdev_bsd_get_ifindex(const struct netdev *netdev)
831 {
832     int ifindex, error;
833
834     error = get_ifindex(netdev, &ifindex);
835     return error ? -error : ifindex;
836 }
837
838 static int
839 netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
840 {
841     struct netdev_dev_bsd *netdev_dev =
842         netdev_dev_bsd_cast(netdev_get_dev(netdev_));
843
844     if (!(netdev_dev->cache_valid & VALID_CARRIER)) {
845         struct ifmediareq ifmr;
846
847         memset(&ifmr, 0, sizeof(ifmr));
848         strncpy(ifmr.ifm_name, netdev_get_name(netdev_), sizeof ifmr.ifm_name);
849
850         if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
851             VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
852                         netdev_get_name(netdev_), strerror(errno));
853             return errno;
854         }
855
856         netdev_dev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
857         netdev_dev->cache_valid |= VALID_CARRIER;
858
859         /* If the interface doesn't report whether the media is active,
860          * just assume it is active. */
861         if ((ifmr.ifm_status & IFM_AVALID) == 0) {
862             netdev_dev->carrier = true;
863         }
864     }
865     *carrier = netdev_dev->carrier;
866
867     return 0;
868 }
869
870 /* Retrieves current device stats for 'netdev'. */
871 static int
872 netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
873 {
874     int if_count, i;
875     int mib[6];
876     size_t len;
877     struct ifmibdata ifmd;
878
879
880     mib[0] = CTL_NET;
881     mib[1] = PF_LINK;
882     mib[2] = NETLINK_GENERIC;
883     mib[3] = IFMIB_SYSTEM;
884     mib[4] = IFMIB_IFCOUNT;
885
886     len = sizeof(if_count);
887
888     if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) {
889         VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
890                     netdev_get_name(netdev_), strerror(errno));
891         return errno;
892     }
893
894     mib[5] = IFDATA_GENERAL;
895     mib[3] = IFMIB_IFDATA;
896     len = sizeof(ifmd);
897     for (i = 1; i <= if_count; i++) {
898         mib[4] = i; //row
899         if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) {
900             VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
901                         netdev_get_name(netdev_), strerror(errno));
902             return errno;
903         } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
904             stats->rx_packets = ifmd.ifmd_data.ifi_ipackets;
905             stats->tx_packets = ifmd.ifmd_data.ifi_opackets;
906             stats->rx_bytes = ifmd.ifmd_data.ifi_ibytes;
907             stats->tx_bytes = ifmd.ifmd_data.ifi_obytes;
908             stats->rx_errors = ifmd.ifmd_data.ifi_ierrors;
909             stats->tx_errors = ifmd.ifmd_data.ifi_oerrors;
910             stats->rx_dropped = ifmd.ifmd_data.ifi_iqdrops;
911             stats->tx_dropped = UINT64_MAX;
912             stats->multicast = ifmd.ifmd_data.ifi_imcasts;
913             stats->collisions = ifmd.ifmd_data.ifi_collisions;
914
915             stats->rx_length_errors = UINT64_MAX;
916             stats->rx_over_errors = UINT64_MAX;
917             stats->rx_crc_errors = UINT64_MAX;
918             stats->rx_frame_errors = UINT64_MAX;
919             stats->rx_fifo_errors = UINT64_MAX;
920             stats->rx_missed_errors = UINT64_MAX;
921
922             stats->tx_aborted_errors = UINT64_MAX;
923             stats->tx_carrier_errors = UINT64_MAX;
924             stats->tx_fifo_errors = UINT64_MAX;
925             stats->tx_heartbeat_errors = UINT64_MAX;
926             stats->tx_window_errors = UINT64_MAX;
927             break;
928         }
929     }
930
931     return 0;
932 }
933
934 static uint32_t
935 netdev_bsd_parse_media(int media)
936 {
937     uint32_t supported = 0;
938     bool half_duplex = media & IFM_HDX ? true : false;
939
940     switch (IFM_SUBTYPE(media)) {
941     case IFM_10_2:
942     case IFM_10_5:
943     case IFM_10_STP:
944     case IFM_10_T:
945         supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
946         supported |= NETDEV_F_COPPER;
947         break;
948
949     case IFM_10_FL:
950         supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
951         supported |= NETDEV_F_FIBER;
952         break;
953
954     case IFM_100_T2:
955     case IFM_100_T4:
956     case IFM_100_TX:
957     case IFM_100_VG:
958         supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
959         supported |= NETDEV_F_COPPER;
960         break;
961
962     case IFM_100_FX:
963         supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
964         supported |= NETDEV_F_FIBER;
965         break;
966
967     case IFM_1000_CX:
968     case IFM_1000_T:
969         supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
970         supported |= NETDEV_F_COPPER;
971         break;
972
973     case IFM_1000_LX:
974     case IFM_1000_SX:
975         supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
976         supported |= NETDEV_F_FIBER;
977         break;
978
979     case IFM_10G_CX4:
980         supported |= NETDEV_F_10GB_FD;
981         supported |= NETDEV_F_COPPER;
982         break;
983
984     case IFM_10G_LR:
985     case IFM_10G_SR:
986         supported |= NETDEV_F_10GB_FD;
987         supported |= NETDEV_F_FIBER;
988         break;
989
990     default:
991         return 0;
992     }
993
994     if (IFM_SUBTYPE(media) == IFM_AUTO) {
995         supported |= NETDEV_F_AUTONEG;
996     }
997     /*
998     if (media & IFM_ETH_FMASK) {
999         supported |= NETDEV_F_PAUSE;
1000     }
1001     */
1002
1003     return supported;
1004 }
1005
1006 /*
1007  * Stores the features supported by 'netdev' into each of '*current',
1008  * '*advertised', '*supported', and '*peer' that are non-null.  Each value is a
1009  * bitmap of "enum ofp_port_features" bits, in host byte order.  Returns 0 if
1010  * successful, otherwise a positive errno value.  On failure, all of the
1011  * passed-in values are set to 0.
1012  */
1013 static int
1014 netdev_bsd_get_features(const struct netdev *netdev,
1015                           enum netdev_features *current, uint32_t *advertised,
1016                           enum netdev_features *supported, uint32_t *peer)
1017 {
1018     struct ifmediareq ifmr;
1019     int *media_list;
1020     int i;
1021     int error;
1022
1023
1024     /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
1025
1026     memset(&ifmr, 0, sizeof(ifmr));
1027     strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
1028
1029     /* We make two SIOCGIFMEDIA ioctl calls.  The first to determine the
1030      * number of supported modes, and a second with a buffer to retrieve
1031      * them. */
1032     if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
1033         VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1034                     netdev_get_name(netdev), strerror(errno));
1035         return errno;
1036     }
1037
1038     media_list = xcalloc(ifmr.ifm_count, sizeof(int));
1039     ifmr.ifm_ulist = media_list;
1040
1041     if (IFM_TYPE(ifmr.ifm_current) != IFM_ETHER) {
1042         VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet",
1043                     netdev_get_name(netdev));
1044         error = EINVAL;
1045         goto cleanup;
1046     }
1047
1048     if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
1049         VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1050                     netdev_get_name(netdev), strerror(errno));
1051         error = errno;
1052         goto cleanup;
1053     }
1054
1055     /* Current settings. */
1056     *current = netdev_bsd_parse_media(ifmr.ifm_active);
1057
1058     /* Advertised features. */
1059     *advertised = netdev_bsd_parse_media(ifmr.ifm_current);
1060
1061     /* Supported features. */
1062     *supported = 0;
1063     for (i = 0; i < ifmr.ifm_count; i++) {
1064         *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]);
1065     }
1066
1067     /* Peer advertisements. */
1068     *peer = 0;                  /* XXX */
1069
1070     error = 0;
1071 cleanup:
1072     free(media_list);
1073     return error;
1074 }
1075
1076 /*
1077  * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if
1078  * 'in4' is non-null) and returns true.  Otherwise, returns false.
1079  */
1080 static int
1081 netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
1082                    struct in_addr *netmask)
1083 {
1084     struct netdev_dev_bsd *netdev_dev =
1085         netdev_dev_bsd_cast(netdev_get_dev(netdev_));
1086
1087     if (!(netdev_dev->cache_valid & VALID_IN4)) {
1088         const struct sockaddr_in *sin;
1089         struct ifreq ifr;
1090         int error;
1091
1092         ifr.ifr_addr.sa_family = AF_INET;
1093         error = netdev_bsd_do_ioctl(netdev_get_name(netdev_), &ifr,
1094                                       SIOCGIFADDR, "SIOCGIFADDR");
1095         if (error) {
1096             return error;
1097         }
1098
1099         sin = (struct sockaddr_in *) &ifr.ifr_addr;
1100         netdev_dev->in4 = sin->sin_addr;
1101         netdev_dev->cache_valid |= VALID_IN4;
1102         error = netdev_bsd_do_ioctl(netdev_get_name(netdev_), &ifr,
1103                                       SIOCGIFNETMASK, "SIOCGIFNETMASK");
1104         if (error) {
1105             return error;
1106         }
1107         *netmask = ((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr;
1108     }
1109     *in4 = netdev_dev->in4;
1110
1111     return in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1112 }
1113
1114 /*
1115  * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask.  If
1116  * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared.  Returns a
1117  * positive errno value.
1118  */
1119 static int
1120 netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
1121                      struct in_addr mask)
1122 {
1123     struct netdev_dev_bsd *netdev_dev =
1124         netdev_dev_bsd_cast(netdev_get_dev(netdev_));
1125     int error;
1126
1127     error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
1128     if (!error) {
1129         netdev_dev->cache_valid |= VALID_IN4;
1130         netdev_dev->in4 = addr;
1131         if (addr.s_addr != INADDR_ANY) {
1132             error = do_set_addr(netdev_, SIOCSIFNETMASK,
1133                                 "SIOCSIFNETMASK", mask);
1134         }
1135         netdev_dev_bsd_changed(netdev_dev);
1136     }
1137     return error;
1138 }
1139
1140 static int
1141 netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1142 {
1143     struct netdev_dev_bsd *netdev_dev =
1144                                 netdev_dev_bsd_cast(netdev_get_dev(netdev_));
1145     if (!(netdev_dev->cache_valid & VALID_IN6)) {
1146         struct ifaddrs *ifa, *head;
1147         struct sockaddr_in6 *sin6;
1148         const char *netdev_name = netdev_get_name(netdev_);
1149
1150         if (getifaddrs(&head) != 0) {
1151             VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1152                     strerror(errno));
1153             return errno;
1154         }
1155
1156         for (ifa = head; ifa; ifa = ifa->ifa_next) {
1157             if (ifa->ifa_addr->sa_family == AF_INET6 &&
1158                     !strcmp(ifa->ifa_name, netdev_name)) {
1159                 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1160                 if (sin6) {
1161                     memcpy(&netdev_dev->in6, &sin6->sin6_addr, sin6->sin6_len);
1162                     netdev_dev->cache_valid |= VALID_IN6;
1163                     *in6 = netdev_dev->in6;
1164                     freeifaddrs(head);
1165                     return 0;
1166                 }
1167             }
1168         }
1169         return EADDRNOTAVAIL;
1170     }
1171     *in6 = netdev_dev->in6;
1172     return 0;
1173 }
1174
1175 static void
1176 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1177 {
1178     struct sockaddr_in sin;
1179     memset(&sin, 0, sizeof sin);
1180     sin.sin_family = AF_INET;
1181     sin.sin_addr = addr;
1182     sin.sin_port = 0;
1183
1184     memset(sa, 0, sizeof *sa);
1185     memcpy(sa, &sin, sizeof sin);
1186 }
1187
1188 static int
1189 do_set_addr(struct netdev *netdev,
1190             int ioctl_nr, const char *ioctl_name, struct in_addr addr)
1191 {
1192     struct ifreq ifr;
1193     make_in4_sockaddr(&ifr.ifr_addr, addr);
1194     return netdev_bsd_do_ioctl(netdev, &ifr, ioctl_nr, ioctl_name);
1195 }
1196
1197 static int
1198 nd_to_iff_flags(enum netdev_flags nd)
1199 {
1200     int iff = 0;
1201     if (nd & NETDEV_UP) {
1202         iff |= IFF_UP;
1203     }
1204     if (nd & NETDEV_PROMISC) {
1205         iff |= IFF_PROMISC;
1206         iff |= IFF_PPROMISC;
1207     }
1208     return iff;
1209 }
1210
1211 static int
1212 iff_to_nd_flags(int iff)
1213 {
1214     enum netdev_flags nd = 0;
1215     if (iff & IFF_UP) {
1216         nd |= NETDEV_UP;
1217     }
1218     if (iff & IFF_PROMISC) {
1219         nd |= NETDEV_PROMISC;
1220     }
1221     return nd;
1222 }
1223
1224 static int
1225 netdev_bsd_update_flags(struct netdev_dev *dev_, enum netdev_flags off,
1226                           enum netdev_flags on, enum netdev_flags *old_flagsp)
1227 {
1228     struct netdev_dev_bsd *netdev_dev;
1229     int old_flags, new_flags;
1230     int error;
1231
1232     netdev_dev = netdev_dev_bsd_cast(dev_);
1233     error = get_flags(dev_, &old_flags);
1234     if (!error) {
1235         *old_flagsp = iff_to_nd_flags(old_flags);
1236         new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1237         if (new_flags != old_flags) {
1238             error = set_flags(netdev_dev_get_name(dev_), new_flags);
1239             netdev_dev_bsd_changed(netdev_dev);
1240         }
1241     }
1242     return error;
1243 }
1244
1245 static unsigned int
1246 netdev_bsd_change_seq(const struct netdev *netdev)
1247 {
1248     return netdev_dev_bsd_cast(netdev_get_dev(netdev))->change_seq;
1249 }
1250
1251
1252 const struct netdev_class netdev_bsd_class = {
1253     "system",
1254
1255     netdev_bsd_init,
1256     netdev_bsd_run,
1257     netdev_bsd_wait,
1258     netdev_bsd_create_system,
1259     netdev_bsd_destroy,
1260     NULL, /* get_config */
1261     NULL, /* set_config */
1262     NULL, /* get_tunnel_config */
1263     netdev_bsd_open_system,
1264     netdev_bsd_close,
1265
1266     netdev_bsd_listen,
1267
1268     netdev_bsd_recv,
1269     netdev_bsd_recv_wait,
1270     netdev_bsd_drain,
1271
1272     netdev_bsd_send,
1273     netdev_bsd_send_wait,
1274
1275     netdev_bsd_set_etheraddr,
1276     netdev_bsd_get_etheraddr,
1277     netdev_bsd_get_mtu,
1278     NULL, /* set_mtu */
1279     netdev_bsd_get_ifindex,
1280     netdev_bsd_get_carrier,
1281     NULL, /* get_carrier_resets */
1282     NULL, /* set_miimon_interval */
1283     netdev_bsd_get_stats,
1284     NULL, /* set_stats */
1285
1286     netdev_bsd_get_features,
1287     NULL, /* set_advertisement */
1288     NULL, /* set_policing */
1289     NULL, /* get_qos_type */
1290     NULL, /* get_qos_capabilities */
1291     NULL, /* get_qos */
1292     NULL, /* set_qos */
1293     NULL, /* get_queue */
1294     NULL, /* set_queue */
1295     NULL, /* delete_queue */
1296     NULL, /* get_queue_stats */
1297     NULL, /* dump_queue */
1298     NULL, /* dump_queue_stats */
1299
1300     netdev_bsd_get_in4,
1301     netdev_bsd_set_in4,
1302     netdev_bsd_get_in6,
1303     NULL, /* add_router */
1304     NULL, /* get_next_hop */
1305     NULL, /* get_status */
1306     NULL, /* arp_lookup */
1307
1308     netdev_bsd_update_flags,
1309
1310     netdev_bsd_change_seq
1311 };
1312
1313 const struct netdev_class netdev_tap_class = {
1314     "tap",
1315
1316     netdev_bsd_init,
1317     netdev_bsd_run,
1318     netdev_bsd_wait,
1319     netdev_bsd_create_tap,
1320     netdev_bsd_destroy,
1321     NULL, /* get_config */
1322     NULL, /* set_config */
1323     NULL, /* get_tunnel_config */
1324     netdev_bsd_open_system,
1325     netdev_bsd_close,
1326
1327     netdev_bsd_listen,
1328
1329     netdev_bsd_recv,
1330     netdev_bsd_recv_wait,
1331     netdev_bsd_drain,
1332
1333     netdev_bsd_send,
1334     netdev_bsd_send_wait,
1335
1336     netdev_bsd_set_etheraddr,
1337     netdev_bsd_get_etheraddr,
1338     netdev_bsd_get_mtu,
1339     NULL, /* set_mtu */
1340     netdev_bsd_get_ifindex,
1341     netdev_bsd_get_carrier,
1342     NULL, /* get_carrier_resets */
1343     NULL, /* set_miimon_interval */
1344     netdev_bsd_get_stats,
1345     NULL, /* set_stats */
1346
1347     netdev_bsd_get_features,
1348     NULL, /* set_advertisement */
1349     NULL, /* set_policing */
1350     NULL, /* get_qos_type */
1351     NULL, /* get_qos_capabilities */
1352     NULL, /* get_qos */
1353     NULL, /* set_qos */
1354     NULL, /* get_queue */
1355     NULL, /* set_queue */
1356     NULL, /* delete_queue */
1357     NULL, /* get_queue_stats */
1358     NULL, /* dump_queue */
1359     NULL, /* dump_queue_stats */
1360
1361     netdev_bsd_get_in4,
1362     netdev_bsd_set_in4,
1363     netdev_bsd_get_in6,
1364     NULL, /* add_router */
1365     NULL, /* get_next_hop */
1366     NULL, /* get_status */
1367     NULL, /* arp_lookup */
1368
1369     netdev_bsd_update_flags,
1370
1371     netdev_bsd_change_seq
1372 };
1373 \f
1374
1375 static void
1376 destroy_tap(int fd, const char *name)
1377 {
1378     struct ifreq ifr;
1379
1380     close(fd);
1381     strcpy(ifr.ifr_name, name);
1382     /* XXX What to do if this call fails? */
1383     ioctl(af_inet_sock, SIOCIFDESTROY, &ifr);
1384 }
1385
1386 static int
1387 get_flags(const struct netdev_dev *dev, int *flags)
1388 {
1389     struct ifreq ifr;
1390     int error;
1391
1392     error = netdev_bsd_do_ioctl(dev->name, &ifr, SIOCGIFFLAGS, "SIOCGIFFLAGS");
1393
1394     *flags = 0xFFFF0000 & (ifr.ifr_flagshigh << 16);
1395     *flags |= 0x0000FFFF & ifr.ifr_flags;
1396
1397     return error;
1398 }
1399
1400 static int
1401 set_flags(const char *name, int flags)
1402 {
1403     struct ifreq ifr;
1404
1405     ifr.ifr_flags = 0x0000FFFF & flags;
1406     ifr.ifr_flagshigh = (0xFFFF0000 & flags) >> 16;
1407
1408     return netdev_bsd_do_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1409 }
1410
1411 static int
1412 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1413 {
1414     struct netdev_dev_bsd *netdev_dev =
1415                                 netdev_dev_bsd_cast(netdev_get_dev(netdev_));
1416     *ifindexp = 0;
1417     if (!(netdev_dev->cache_valid & VALID_IFINDEX)) {
1418         int ifindex = if_nametoindex(netdev_get_name(netdev_));
1419         if (ifindex <= 0) {
1420             return errno;
1421         }
1422         netdev_dev->cache_valid |= VALID_IFINDEX;
1423         netdev_dev->ifindex = ifindex;
1424     }
1425     *ifindexp = netdev_dev->ifindex;
1426     return 0;
1427 }
1428
1429 static int
1430 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1431 {
1432     struct ifaddrs *head;
1433     struct ifaddrs *ifa;
1434     struct sockaddr_dl *sdl;
1435
1436     if (getifaddrs(&head) != 0) {
1437         VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1438                 strerror(errno));
1439         return errno;
1440     }
1441
1442     for (ifa = head; ifa; ifa = ifa->ifa_next) {
1443         if (ifa->ifa_addr->sa_family == AF_LINK) {
1444             if (!strcmp(ifa->ifa_name, netdev_name)) {
1445                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1446                 if (sdl) {
1447                     memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
1448                     freeifaddrs(head);
1449                     return 0;
1450                 }
1451             }
1452         }
1453     }
1454
1455     VLOG_ERR("could not find ethernet address for %s device", netdev_name);
1456     freeifaddrs(head);
1457     return ENODEV;
1458 }
1459
1460 static int
1461 set_etheraddr(const char *netdev_name, int hwaddr_family,
1462               int hwaddr_len, const uint8_t mac[ETH_ADDR_LEN])
1463 {
1464     struct ifreq ifr;
1465
1466     memset(&ifr, 0, sizeof ifr);
1467     strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1468     ifr.ifr_addr.sa_family = hwaddr_family;
1469     ifr.ifr_addr.sa_len = hwaddr_len;
1470     memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
1471     if (ioctl(af_inet_sock, SIOCSIFLLADDR, &ifr) < 0) {
1472         VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
1473                  netdev_name, strerror(errno));
1474         return errno;
1475     }
1476     return 0;
1477 }
1478
1479 static int
1480 netdev_bsd_do_ioctl(const char *name, struct ifreq *ifr, unsigned long cmd,
1481                     const char *cmd_name)
1482 {
1483     strncpy(ifr->ifr_name, name, sizeof ifr->ifr_name);
1484     if (ioctl(af_inet_sock, cmd, ifr) == -1) {
1485         VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name,
1486                     strerror(errno));
1487         return errno;
1488     }
1489     return 0;
1490 }