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