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