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