]> git.proxmox.com Git - ovs.git/blob - lib/netdev-bsd.c
netdev-bsd: Fix tx/rx stats for type=tap netdev
[ovs.git] / lib / netdev-bsd.c
1 /*
2 * Copyright (c) 2011, 2013 Gaetano Catalli.
3 * Copyright (c) 2013 YAMAMOTO Takashi.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <config.h>
19
20 #include "netdev-provider.h"
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <sys/types.h>
25 #include <sys/time.h>
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <sys/sockio.h>
29 #include <ifaddrs.h>
30 #include <pcap/pcap.h>
31 #include <net/if.h>
32 #include <net/if_dl.h>
33 #include <net/if_media.h>
34 #include <net/if_tap.h>
35 #include <netinet/in.h>
36 #ifdef HAVE_NET_IF_MIB_H
37 #include <net/if_mib.h>
38 #endif
39 #include <poll.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <sys/sysctl.h>
43 #if defined(__NetBSD__)
44 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netinet/if_inarp.h>
47 #endif
48
49 #include "rtbsd.h"
50 #include "connectivity.h"
51 #include "coverage.h"
52 #include "dynamic-string.h"
53 #include "fatal-signal.h"
54 #include "ofpbuf.h"
55 #include "openflow/openflow.h"
56 #include "ovs-thread.h"
57 #include "packets.h"
58 #include "poll-loop.h"
59 #include "seq.h"
60 #include "shash.h"
61 #include "socket-util.h"
62 #include "svec.h"
63 #include "util.h"
64 #include "vlog.h"
65
66 VLOG_DEFINE_THIS_MODULE(netdev_bsd);
67
68 \f
69 struct netdev_rx_bsd {
70 struct netdev_rx up;
71
72 /* Packet capture descriptor for a system network device.
73 * For a tap device this is NULL. */
74 pcap_t *pcap_handle;
75
76 /* Selectable file descriptor for the network device.
77 * This descriptor will be used for polling operations. */
78 int fd;
79 };
80
81 struct netdev_bsd {
82 struct netdev up;
83
84 /* Never changes after initialization. */
85 char *kernel_name;
86
87 /* Protects all members below. */
88 struct ovs_mutex mutex;
89
90 unsigned int cache_valid;
91
92 int ifindex;
93 uint8_t etheraddr[ETH_ADDR_LEN];
94 struct in_addr in4;
95 struct in_addr netmask;
96 struct in6_addr in6;
97 int mtu;
98 int carrier;
99
100 int tap_fd; /* TAP character device, if any, otherwise -1. */
101
102 /* Used for sending packets on non-tap devices. */
103 pcap_t *pcap;
104 int fd;
105 };
106
107
108 enum {
109 VALID_IFINDEX = 1 << 0,
110 VALID_ETHERADDR = 1 << 1,
111 VALID_IN4 = 1 << 2,
112 VALID_IN6 = 1 << 3,
113 VALID_MTU = 1 << 4,
114 VALID_CARRIER = 1 << 5
115 };
116
117 #define PCAP_SNAPLEN 2048
118
119
120 /*
121 * Notifier used to invalidate device informations in case of status change.
122 *
123 * It will be registered with a 'rtbsd_notifier_register()' when the first
124 * device will be created with the call of either 'netdev_bsd_tap_create()' or
125 * 'netdev_bsd_system_create()'.
126 *
127 * The callback associated with this notifier ('netdev_bsd_cache_cb()') will
128 * invalidate cached information about the device.
129 */
130 static struct rtbsd_notifier netdev_bsd_cache_notifier;
131 static int cache_notifier_refcount;
132
133 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
134
135 static void destroy_tap(int fd, const char *name);
136 static int get_flags(const struct netdev *, int *flagsp);
137 static int set_flags(const char *, int flags);
138 static int do_set_addr(struct netdev *netdev,
139 unsigned long ioctl_nr, const char *ioctl_name,
140 struct in_addr addr);
141 static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
142 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
143 int hwaddr_len, const uint8_t[ETH_ADDR_LEN]);
144 static int get_ifindex(const struct netdev *, int *ifindexp);
145
146 static int ifr_get_flags(const struct ifreq *);
147 static void ifr_set_flags(struct ifreq *, int flags);
148
149 #ifdef __NetBSD__
150 static int af_link_ioctl(unsigned long command, const void *arg);
151 #endif
152
153 static void netdev_bsd_run(void);
154
155 static bool
156 is_netdev_bsd_class(const struct netdev_class *netdev_class)
157 {
158 return netdev_class->run == netdev_bsd_run;
159 }
160
161 static struct netdev_bsd *
162 netdev_bsd_cast(const struct netdev *netdev)
163 {
164 ovs_assert(is_netdev_bsd_class(netdev_get_class(netdev)));
165 return CONTAINER_OF(netdev, struct netdev_bsd, up);
166 }
167
168 static struct netdev_rx_bsd *
169 netdev_rx_bsd_cast(const struct netdev_rx *rx)
170 {
171 ovs_assert(is_netdev_bsd_class(netdev_get_class(rx->netdev)));
172 return CONTAINER_OF(rx, struct netdev_rx_bsd, up);
173 }
174
175 static const char *
176 netdev_get_kernel_name(const struct netdev *netdev)
177 {
178 return netdev_bsd_cast(netdev)->kernel_name;
179 }
180
181 /*
182 * Perform periodic work needed by netdev. In BSD netdevs it checks for any
183 * interface status changes, and eventually calls all the user callbacks.
184 */
185 static void
186 netdev_bsd_run(void)
187 {
188 rtbsd_notifier_run();
189 }
190
191 /*
192 * Arranges for poll_block() to wake up if the "run" member function needs to
193 * be called.
194 */
195 static void
196 netdev_bsd_wait(void)
197 {
198 rtbsd_notifier_wait();
199 }
200
201 /* Invalidate cache in case of interface status change. */
202 static void
203 netdev_bsd_cache_cb(const struct rtbsd_change *change,
204 void *aux OVS_UNUSED)
205 {
206 struct netdev_bsd *dev;
207
208 if (change) {
209 struct netdev *base_dev = netdev_from_name(change->if_name);
210
211 if (base_dev) {
212 const struct netdev_class *netdev_class =
213 netdev_get_class(base_dev);
214
215 if (is_netdev_bsd_class(netdev_class)) {
216 dev = netdev_bsd_cast(base_dev);
217 dev->cache_valid = 0;
218 seq_change(connectivity_seq_get());
219 }
220 netdev_close(base_dev);
221 }
222 } else {
223 /*
224 * XXX the API is lacking, we should be able to iterate on the list of
225 * netdevs without having to store the info in a temp shash.
226 */
227 struct shash device_shash;
228 struct shash_node *node;
229
230 shash_init(&device_shash);
231 netdev_get_devices(&netdev_bsd_class, &device_shash);
232 SHASH_FOR_EACH (node, &device_shash) {
233 struct netdev *netdev = node->data;
234 dev = netdev_bsd_cast(netdev);
235 dev->cache_valid = 0;
236 seq_change(connectivity_seq_get());
237 netdev_close(netdev);
238 }
239 shash_destroy(&device_shash);
240 }
241 }
242
243 static int
244 cache_notifier_ref(void)
245 {
246 int ret = 0;
247
248 if (!cache_notifier_refcount) {
249 ret = rtbsd_notifier_register(&netdev_bsd_cache_notifier,
250 netdev_bsd_cache_cb, NULL);
251 if (ret) {
252 return ret;
253 }
254 }
255 cache_notifier_refcount++;
256 return 0;
257 }
258
259 static int
260 cache_notifier_unref(void)
261 {
262 cache_notifier_refcount--;
263 if (cache_notifier_refcount == 0) {
264 rtbsd_notifier_unregister(&netdev_bsd_cache_notifier);
265 }
266 return 0;
267 }
268
269 static struct netdev *
270 netdev_bsd_alloc(void)
271 {
272 struct netdev_bsd *netdev = xzalloc(sizeof *netdev);
273 return &netdev->up;
274 }
275
276 static int
277 netdev_bsd_construct_system(struct netdev *netdev_)
278 {
279 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
280 enum netdev_flags flags;
281 int error;
282
283 error = cache_notifier_ref();
284 if (error) {
285 return error;
286 }
287
288 ovs_mutex_init(&netdev->mutex);
289 netdev->tap_fd = -1;
290 netdev->kernel_name = xstrdup(netdev_->name);
291
292 /* Verify that the netdev really exists by attempting to read its flags */
293 error = netdev_get_flags(netdev_, &flags);
294 if (error == ENXIO) {
295 free(netdev->kernel_name);
296 cache_notifier_unref();
297 return error;
298 }
299
300 return 0;
301 }
302
303 static int
304 netdev_bsd_construct_tap(struct netdev *netdev_)
305 {
306 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
307 const char *name = netdev_->name;
308 int error = 0;
309 struct ifreq ifr;
310 char *kernel_name = NULL;
311
312 error = cache_notifier_ref();
313 if (error) {
314 goto error;
315 }
316
317 memset(&ifr, 0, sizeof(ifr));
318
319 /* Create a tap device by opening /dev/tap. The TAPGIFNAME ioctl is used
320 * to retrieve the name of the tap device. */
321 ovs_mutex_init(&netdev->mutex);
322 netdev->tap_fd = open("/dev/tap", O_RDWR);
323 if (netdev->tap_fd < 0) {
324 error = errno;
325 VLOG_WARN("opening \"/dev/tap\" failed: %s", ovs_strerror(error));
326 goto error_unref_notifier;
327 }
328
329 /* Retrieve tap name (e.g. tap0) */
330 if (ioctl(netdev->tap_fd, TAPGIFNAME, &ifr) == -1) {
331 /* XXX Need to destroy the device? */
332 error = errno;
333 close(netdev->tap_fd);
334 goto error_unref_notifier;
335 }
336
337 /* Change the name of the tap device */
338 #if defined(SIOCSIFNAME)
339 ifr.ifr_data = (void *)name;
340 error = af_inet_ioctl(SIOCSIFNAME, &ifr);
341 if (error) {
342 destroy_tap(netdev->tap_fd, ifr.ifr_name);
343 goto error_unref_notifier;
344 }
345 kernel_name = xstrdup(name);
346 #else
347 /*
348 * NetBSD doesn't support inteface renaming.
349 */
350 VLOG_INFO("tap %s is created for bridge %s", ifr.ifr_name, name);
351 kernel_name = xstrdup(ifr.ifr_name);
352 #endif
353
354 /* set non-blocking. */
355 error = set_nonblocking(netdev->tap_fd);
356 if (error) {
357 destroy_tap(netdev->tap_fd, kernel_name);
358 goto error_unref_notifier;
359 }
360
361 /* Turn device UP */
362 ifr_set_flags(&ifr, IFF_UP);
363 strncpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
364 error = af_inet_ioctl(SIOCSIFFLAGS, &ifr);
365 if (error) {
366 destroy_tap(netdev->tap_fd, kernel_name);
367 goto error_unref_notifier;
368 }
369
370 netdev->kernel_name = kernel_name;
371
372 return 0;
373
374 error_unref_notifier:
375 ovs_mutex_destroy(&netdev->mutex);
376 cache_notifier_unref();
377 error:
378 free(kernel_name);
379 return error;
380 }
381
382 static void
383 netdev_bsd_destruct(struct netdev *netdev_)
384 {
385 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
386
387 cache_notifier_unref();
388
389 if (netdev->tap_fd >= 0) {
390 destroy_tap(netdev->tap_fd, netdev_get_kernel_name(netdev_));
391 }
392 if (netdev->pcap) {
393 pcap_close(netdev->pcap);
394 }
395 free(netdev->kernel_name);
396 ovs_mutex_destroy(&netdev->mutex);
397 }
398
399 static void
400 netdev_bsd_dealloc(struct netdev *netdev_)
401 {
402 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
403
404 free(netdev);
405 }
406
407 static int
408 netdev_bsd_open_pcap(const char *name, pcap_t **pcapp, int *fdp)
409 {
410 char errbuf[PCAP_ERRBUF_SIZE];
411 pcap_t *pcap = NULL;
412 int one = 1;
413 int error;
414 int fd;
415
416 /* Open the pcap device. The device is opened in non-promiscuous mode
417 * because the interface flags are manually set by the caller. */
418 errbuf[0] = '\0';
419 pcap = pcap_open_live(name, PCAP_SNAPLEN, 0, 1000, errbuf);
420 if (!pcap) {
421 VLOG_ERR_RL(&rl, "%s: pcap_open_live failed: %s", name, errbuf);
422 error = EIO;
423 goto error;
424 }
425 if (errbuf[0] != '\0') {
426 VLOG_WARN_RL(&rl, "%s: pcap_open_live: %s", name, errbuf);
427 }
428
429 /* Get the underlying fd. */
430 fd = pcap_get_selectable_fd(pcap);
431 if (fd == -1) {
432 VLOG_WARN_RL(&rl, "%s: no selectable file descriptor", name);
433 error = errno;
434 goto error;
435 }
436
437 /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
438 * on the file descriptor returned by pcap_get_selectable_fd to achieve
439 * a real non-blocking behaviour.*/
440 error = pcap_setnonblock(pcap, 1, errbuf);
441 if (error == -1) {
442 error = errno;
443 goto error;
444 }
445
446 /* This call assure that reads return immediately upon packet
447 * reception. Otherwise, a read will block until either the kernel
448 * buffer becomes full or a timeout occurs. */
449 if (ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) {
450 VLOG_ERR_RL(&rl, "ioctl(BIOCIMMEDIATE) on %s device failed: %s",
451 name, ovs_strerror(errno));
452 error = errno;
453 goto error;
454 }
455
456 /* Capture only incoming packets. */
457 error = pcap_setdirection(pcap, PCAP_D_IN);
458 if (error == -1) {
459 error = errno;
460 goto error;
461 }
462
463 *pcapp = pcap;
464 *fdp = fd;
465 return 0;
466
467 error:
468 if (pcap) {
469 pcap_close(pcap);
470 }
471 *pcapp = NULL;
472 *fdp = -1;
473 return error;
474 }
475
476 static struct netdev_rx *
477 netdev_bsd_rx_alloc(void)
478 {
479 struct netdev_rx_bsd *rx = xzalloc(sizeof *rx);
480 return &rx->up;
481 }
482
483 static int
484 netdev_bsd_rx_construct(struct netdev_rx *rx_)
485 {
486 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
487 struct netdev *netdev_ = rx->up.netdev;
488 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
489 int error;
490
491 if (!strcmp(netdev_get_type(netdev_), "tap")) {
492 rx->pcap_handle = NULL;
493 rx->fd = netdev->tap_fd;
494 error = 0;
495 } else {
496 ovs_mutex_lock(&netdev->mutex);
497 error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_),
498 &rx->pcap_handle, &rx->fd);
499 ovs_mutex_unlock(&netdev->mutex);
500 }
501
502 return error;
503 }
504
505 static void
506 netdev_bsd_rx_destruct(struct netdev_rx *rx_)
507 {
508 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
509
510 if (rx->pcap_handle) {
511 pcap_close(rx->pcap_handle);
512 }
513 }
514
515 static void
516 netdev_bsd_rx_dealloc(struct netdev_rx *rx_)
517 {
518 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
519
520 free(rx);
521 }
522
523 /* The recv callback of the netdev class returns the number of bytes of the
524 * received packet.
525 *
526 * This can be done by the pcap_next() function. Unfortunately pcap_next() does
527 * not make difference between a missing packet on the capture interface and
528 * an error during the file capture. We can use the pcap_dispatch() function
529 * instead, which is able to distinguish between errors and null packet.
530 *
531 * To make pcap_dispatch() returns the number of bytes read from the interface
532 * we need to define the following callback and argument.
533 */
534 struct pcap_arg {
535 void *data;
536 int size;
537 int retval;
538 };
539
540 /*
541 * This callback will be executed on every captured packet.
542 *
543 * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
544 * pcap returns a truncated packet and we follow this behavior.
545 *
546 * The argument args->retval is the packet size in bytes.
547 */
548 static void
549 proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
550 {
551 struct pcap_arg *args = (struct pcap_arg *)args_;
552
553 if (args->size < hdr->len) {
554 VLOG_WARN_RL(&rl, "packet truncated");
555 args->retval = args->size;
556 } else {
557 args->retval = hdr->len;
558 }
559
560 /* copy the packet to our buffer */
561 memcpy(args->data, packet, args->retval);
562 }
563
564 /*
565 * This function attempts to receive a packet from the specified network
566 * device. It is assumed that the network device is a system device or a tap
567 * device opened as a system one. In this case the read operation is performed
568 * from rx->pcap.
569 */
570 static int
571 netdev_rx_bsd_recv_pcap(struct netdev_rx_bsd *rx, struct ofpbuf *buffer)
572 {
573 struct pcap_arg arg;
574 int ret;
575
576 /* prepare the pcap argument to store the packet */
577 arg.size = ofpbuf_tailroom(buffer);
578 arg.data = buffer->data;
579
580 for (;;) {
581 ret = pcap_dispatch(rx->pcap_handle, 1, proc_pkt, (u_char *) &arg);
582
583 if (ret > 0) {
584 buffer->size += arg.retval;
585 return 0;
586 }
587 if (ret == -1) {
588 if (errno == EINTR) {
589 continue;
590 }
591 }
592
593 return EAGAIN;
594 }
595 }
596
597 /*
598 * This function attempts to receive a packet from the specified network
599 * device. It is assumed that the network device is a tap device and
600 * 'rx->fd' is initialized with the tap file descriptor.
601 */
602 static int
603 netdev_rx_bsd_recv_tap(struct netdev_rx_bsd *rx, struct ofpbuf *buffer)
604 {
605 size_t size = ofpbuf_tailroom(buffer);
606
607 for (;;) {
608 ssize_t retval = read(rx->fd, buffer->data, size);
609 if (retval >= 0) {
610 buffer->size += retval;
611 return 0;
612 } else if (errno != EINTR) {
613 if (errno != EAGAIN) {
614 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
615 ovs_strerror(errno), netdev_rx_get_name(&rx->up));
616 }
617 return errno;
618 }
619 }
620 }
621
622 static int
623 netdev_bsd_rx_recv(struct netdev_rx *rx_, struct ofpbuf *buffer)
624 {
625 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
626
627 return (rx->pcap_handle
628 ? netdev_rx_bsd_recv_pcap(rx, buffer)
629 : netdev_rx_bsd_recv_tap(rx, buffer));
630 }
631
632 /*
633 * Registers with the poll loop to wake up from the next call to poll_block()
634 * when a packet is ready to be received with netdev_rx_recv() on 'rx'.
635 */
636 static void
637 netdev_bsd_rx_wait(struct netdev_rx *rx_)
638 {
639 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
640
641 poll_fd_wait(rx->fd, POLLIN);
642 }
643
644 /* Discards all packets waiting to be received from 'rx'. */
645 static int
646 netdev_bsd_rx_drain(struct netdev_rx *rx_)
647 {
648 struct ifreq ifr;
649 struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
650
651 strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rx_get_netdev(rx_)));
652 if (ioctl(rx->fd, BIOCFLUSH, &ifr) == -1) {
653 VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
654 netdev_rx_get_name(rx_), ovs_strerror(errno));
655 return errno;
656 }
657 return 0;
658 }
659
660 /*
661 * Send a packet on the specified network device. The device could be either a
662 * system or a tap device.
663 */
664 static int
665 netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size)
666 {
667 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
668 const char *name = netdev_get_name(netdev_);
669 int error;
670
671 ovs_mutex_lock(&dev->mutex);
672 if (dev->tap_fd < 0 && !dev->pcap) {
673 error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd);
674 } else {
675 error = 0;
676 }
677
678 while (!error) {
679 ssize_t retval;
680 if (dev->tap_fd >= 0) {
681 retval = write(dev->tap_fd, data, size);
682 } else {
683 retval = pcap_inject(dev->pcap, data, size);
684 }
685 if (retval < 0) {
686 if (errno == EINTR) {
687 continue;
688 } else {
689 error = errno;
690 if (error != EAGAIN) {
691 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: "
692 "%s", name, ovs_strerror(error));
693 }
694 }
695 } else if (retval != size) {
696 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIuSIZE"d bytes of "
697 "%"PRIuSIZE") on %s", retval, size, name);
698 error = EMSGSIZE;
699 } else {
700 break;
701 }
702 }
703
704 ovs_mutex_unlock(&dev->mutex);
705 return error;
706 }
707
708 /*
709 * Registers with the poll loop to wake up from the next call to poll_block()
710 * when the packet transmission queue has sufficient room to transmit a packet
711 * with netdev_send().
712 */
713 static void
714 netdev_bsd_send_wait(struct netdev *netdev_)
715 {
716 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
717
718 ovs_mutex_lock(&dev->mutex);
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 ovs_mutex_unlock(&dev->mutex);
729 }
730
731 /*
732 * Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
733 * otherwise a positive errno value.
734 */
735 static int
736 netdev_bsd_set_etheraddr(struct netdev *netdev_,
737 const uint8_t mac[ETH_ADDR_LEN])
738 {
739 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
740 int error = 0;
741
742 ovs_mutex_lock(&netdev->mutex);
743 if (!(netdev->cache_valid & VALID_ETHERADDR)
744 || !eth_addr_equals(netdev->etheraddr, mac)) {
745 error = set_etheraddr(netdev_get_kernel_name(netdev_), AF_LINK,
746 ETH_ADDR_LEN, mac);
747 if (!error) {
748 netdev->cache_valid |= VALID_ETHERADDR;
749 memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
750 seq_change(connectivity_seq_get());
751 }
752 }
753 ovs_mutex_unlock(&netdev->mutex);
754
755 return error;
756 }
757
758 /*
759 * Returns a pointer to 'netdev''s MAC address. The caller must not modify or
760 * free the returned buffer.
761 */
762 static int
763 netdev_bsd_get_etheraddr(const struct netdev *netdev_,
764 uint8_t mac[ETH_ADDR_LEN])
765 {
766 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
767 int error = 0;
768
769 ovs_mutex_lock(&netdev->mutex);
770 if (!(netdev->cache_valid & VALID_ETHERADDR)) {
771 error = get_etheraddr(netdev_get_kernel_name(netdev_),
772 netdev->etheraddr);
773 if (!error) {
774 netdev->cache_valid |= VALID_ETHERADDR;
775 }
776 }
777 if (!error) {
778 memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
779 }
780 ovs_mutex_unlock(&netdev->mutex);
781
782 return error;
783 }
784
785 /*
786 * Returns the maximum size of transmitted (and received) packets on 'netdev',
787 * in bytes, not including the hardware header; thus, this is typically 1500
788 * bytes for Ethernet devices.
789 */
790 static int
791 netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
792 {
793 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
794 int error = 0;
795
796 ovs_mutex_lock(&netdev->mutex);
797 if (!(netdev->cache_valid & VALID_MTU)) {
798 struct ifreq ifr;
799
800 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
801 SIOCGIFMTU, "SIOCGIFMTU");
802 if (!error) {
803 netdev->mtu = ifr.ifr_mtu;
804 netdev->cache_valid |= VALID_MTU;
805 }
806 }
807 if (!error) {
808 *mtup = netdev->mtu;
809 }
810 ovs_mutex_unlock(&netdev->mutex);
811
812 return 0;
813 }
814
815 static int
816 netdev_bsd_get_ifindex(const struct netdev *netdev_)
817 {
818 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
819 int ifindex, error;
820
821 ovs_mutex_lock(&netdev->mutex);
822 error = get_ifindex(netdev_, &ifindex);
823 ovs_mutex_unlock(&netdev->mutex);
824
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 int error = 0;
833
834 ovs_mutex_lock(&netdev->mutex);
835 if (!(netdev->cache_valid & VALID_CARRIER)) {
836 struct ifmediareq ifmr;
837
838 memset(&ifmr, 0, sizeof(ifmr));
839 strncpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
840 sizeof ifmr.ifm_name);
841
842 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
843 if (!error) {
844 netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
845 netdev->cache_valid |= VALID_CARRIER;
846
847 /* If the interface doesn't report whether the media is active,
848 * just assume it is active. */
849 if ((ifmr.ifm_status & IFM_AVALID) == 0) {
850 netdev->carrier = true;
851 }
852 } else {
853 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
854 netdev_get_name(netdev_), ovs_strerror(error));
855 }
856 }
857 if (!error) {
858 *carrier = netdev->carrier;
859 }
860 ovs_mutex_unlock(&netdev->mutex);
861
862 return error;
863 }
864
865 static void
866 convert_stats_system(struct netdev_stats *stats, const struct if_data *ifd)
867 {
868 /*
869 * note: UINT64_MAX means unsupported
870 */
871 stats->rx_packets = ifd->ifi_ipackets;
872 stats->tx_packets = ifd->ifi_opackets;
873 stats->rx_bytes = ifd->ifi_obytes;
874 stats->tx_bytes = ifd->ifi_ibytes;
875 stats->rx_errors = ifd->ifi_ierrors;
876 stats->tx_errors = ifd->ifi_oerrors;
877 stats->rx_dropped = ifd->ifi_iqdrops;
878 stats->tx_dropped = UINT64_MAX;
879 stats->multicast = ifd->ifi_imcasts;
880 stats->collisions = ifd->ifi_collisions;
881 stats->rx_length_errors = UINT64_MAX;
882 stats->rx_over_errors = UINT64_MAX;
883 stats->rx_crc_errors = UINT64_MAX;
884 stats->rx_frame_errors = UINT64_MAX;
885 stats->rx_fifo_errors = UINT64_MAX;
886 stats->rx_missed_errors = UINT64_MAX;
887 stats->tx_aborted_errors = UINT64_MAX;
888 stats->tx_carrier_errors = UINT64_MAX;
889 stats->tx_fifo_errors = UINT64_MAX;
890 stats->tx_heartbeat_errors = UINT64_MAX;
891 stats->tx_window_errors = UINT64_MAX;
892 }
893
894 static void
895 convert_stats_tap(struct netdev_stats *stats, const struct if_data *ifd)
896 {
897 /*
898 * Similar to convert_stats_system but swapping rx and tx
899 * because 'ifd' is stats for the network interface side of the
900 * tap device and what the caller wants is one for the character
901 * device side.
902 *
903 * note: UINT64_MAX means unsupported
904 */
905 stats->rx_packets = ifd->ifi_opackets;
906 stats->tx_packets = ifd->ifi_ipackets;
907 stats->rx_bytes = ifd->ifi_ibytes;
908 stats->tx_bytes = ifd->ifi_obytes;
909 stats->rx_errors = ifd->ifi_oerrors;
910 stats->tx_errors = ifd->ifi_ierrors;
911 stats->rx_dropped = UINT64_MAX;
912 stats->tx_dropped = ifd->ifi_iqdrops;
913 stats->multicast = ifd->ifi_omcasts;
914 stats->collisions = UINT64_MAX;
915 stats->rx_length_errors = UINT64_MAX;
916 stats->rx_over_errors = UINT64_MAX;
917 stats->rx_crc_errors = UINT64_MAX;
918 stats->rx_frame_errors = UINT64_MAX;
919 stats->rx_fifo_errors = UINT64_MAX;
920 stats->rx_missed_errors = UINT64_MAX;
921 stats->tx_aborted_errors = UINT64_MAX;
922 stats->tx_carrier_errors = UINT64_MAX;
923 stats->tx_fifo_errors = UINT64_MAX;
924 stats->tx_heartbeat_errors = UINT64_MAX;
925 stats->tx_window_errors = UINT64_MAX;
926 }
927
928 static void
929 convert_stats(const struct netdev *netdev, struct netdev_stats *stats,
930 const struct if_data *ifd)
931 {
932 if (netdev_bsd_cast(netdev)->tap_fd == -1) {
933 convert_stats_system(stats, ifd);
934 } else {
935 convert_stats_tap(stats, ifd);
936 }
937 }
938
939 /* Retrieves current device stats for 'netdev'. */
940 static int
941 netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
942 {
943 #if defined(__FreeBSD__)
944 int if_count, i;
945 int mib[6];
946 size_t len;
947 struct ifmibdata ifmd;
948
949
950 mib[0] = CTL_NET;
951 mib[1] = PF_LINK;
952 mib[2] = NETLINK_GENERIC;
953 mib[3] = IFMIB_SYSTEM;
954 mib[4] = IFMIB_IFCOUNT;
955
956 len = sizeof(if_count);
957
958 if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) {
959 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
960 netdev_get_name(netdev_), ovs_strerror(errno));
961 return errno;
962 }
963
964 mib[5] = IFDATA_GENERAL;
965 mib[3] = IFMIB_IFDATA;
966 len = sizeof(ifmd);
967 for (i = 1; i <= if_count; i++) {
968 mib[4] = i; //row
969 if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) {
970 VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
971 netdev_get_name(netdev_), ovs_strerror(errno));
972 return errno;
973 } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
974 convert_stats(netdev, stats, &ifdr.ifdr_data);
975 break;
976 }
977 }
978
979 return 0;
980 #elif defined(__NetBSD__)
981 struct ifdatareq ifdr;
982 int error;
983
984 memset(&ifdr, 0, sizeof(ifdr));
985 strncpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_),
986 sizeof(ifdr.ifdr_name));
987 error = af_link_ioctl(SIOCGIFDATA, &ifdr);
988 if (!error) {
989 convert_stats(netdev_, stats, &ifdr.ifdr_data);
990 }
991 return error;
992 #else
993 #error not implemented
994 #endif
995 }
996
997 static uint32_t
998 netdev_bsd_parse_media(int media)
999 {
1000 uint32_t supported = 0;
1001 bool half_duplex = media & IFM_HDX ? true : false;
1002
1003 switch (IFM_SUBTYPE(media)) {
1004 case IFM_10_2:
1005 case IFM_10_5:
1006 case IFM_10_STP:
1007 case IFM_10_T:
1008 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
1009 supported |= NETDEV_F_COPPER;
1010 break;
1011
1012 case IFM_10_FL:
1013 supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
1014 supported |= NETDEV_F_FIBER;
1015 break;
1016
1017 case IFM_100_T2:
1018 case IFM_100_T4:
1019 case IFM_100_TX:
1020 case IFM_100_VG:
1021 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
1022 supported |= NETDEV_F_COPPER;
1023 break;
1024
1025 case IFM_100_FX:
1026 supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
1027 supported |= NETDEV_F_FIBER;
1028 break;
1029
1030 case IFM_1000_CX:
1031 case IFM_1000_T:
1032 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
1033 supported |= NETDEV_F_COPPER;
1034 break;
1035
1036 case IFM_1000_LX:
1037 case IFM_1000_SX:
1038 supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
1039 supported |= NETDEV_F_FIBER;
1040 break;
1041
1042 case IFM_10G_CX4:
1043 supported |= NETDEV_F_10GB_FD;
1044 supported |= NETDEV_F_COPPER;
1045 break;
1046
1047 case IFM_10G_LR:
1048 case IFM_10G_SR:
1049 supported |= NETDEV_F_10GB_FD;
1050 supported |= NETDEV_F_FIBER;
1051 break;
1052
1053 default:
1054 return 0;
1055 }
1056
1057 if (IFM_SUBTYPE(media) == IFM_AUTO) {
1058 supported |= NETDEV_F_AUTONEG;
1059 }
1060 /*
1061 if (media & IFM_ETH_FMASK) {
1062 supported |= NETDEV_F_PAUSE;
1063 }
1064 */
1065
1066 return supported;
1067 }
1068
1069 /*
1070 * Stores the features supported by 'netdev' into each of '*current',
1071 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1072 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1073 * successful, otherwise a positive errno value. On failure, all of the
1074 * passed-in values are set to 0.
1075 */
1076 static int
1077 netdev_bsd_get_features(const struct netdev *netdev,
1078 enum netdev_features *current, uint32_t *advertised,
1079 enum netdev_features *supported, uint32_t *peer)
1080 {
1081 struct ifmediareq ifmr;
1082 int *media_list;
1083 int i;
1084 int error;
1085
1086
1087 /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
1088
1089 memset(&ifmr, 0, sizeof(ifmr));
1090 strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
1091
1092 /* We make two SIOCGIFMEDIA ioctl calls. The first to determine the
1093 * number of supported modes, and a second with a buffer to retrieve
1094 * them. */
1095 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
1096 if (error) {
1097 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1098 netdev_get_name(netdev), ovs_strerror(error));
1099 return error;
1100 }
1101
1102 media_list = xcalloc(ifmr.ifm_count, sizeof(int));
1103 ifmr.ifm_ulist = media_list;
1104
1105 if (IFM_TYPE(ifmr.ifm_current) != IFM_ETHER) {
1106 VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet",
1107 netdev_get_name(netdev));
1108 error = EINVAL;
1109 goto cleanup;
1110 }
1111
1112 error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
1113 if (error) {
1114 VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
1115 netdev_get_name(netdev), ovs_strerror(error));
1116 goto cleanup;
1117 }
1118
1119 /* Current settings. */
1120 *current = netdev_bsd_parse_media(ifmr.ifm_active);
1121
1122 /* Advertised features. */
1123 *advertised = netdev_bsd_parse_media(ifmr.ifm_current);
1124
1125 /* Supported features. */
1126 *supported = 0;
1127 for (i = 0; i < ifmr.ifm_count; i++) {
1128 *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]);
1129 }
1130
1131 /* Peer advertisements. */
1132 *peer = 0; /* XXX */
1133
1134 error = 0;
1135 cleanup:
1136 free(media_list);
1137 return error;
1138 }
1139
1140 /*
1141 * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address and
1142 * '*netmask' to its netmask and returns true. Otherwise, returns false.
1143 */
1144 static int
1145 netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
1146 struct in_addr *netmask)
1147 {
1148 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1149 int error = 0;
1150
1151 ovs_mutex_lock(&netdev->mutex);
1152 if (!(netdev->cache_valid & VALID_IN4)) {
1153 struct ifreq ifr;
1154
1155 ifr.ifr_addr.sa_family = AF_INET;
1156 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1157 SIOCGIFADDR, "SIOCGIFADDR");
1158 if (!error) {
1159 const struct sockaddr_in *sin;
1160
1161 sin = (struct sockaddr_in *) &ifr.ifr_addr;
1162 netdev->in4 = sin->sin_addr;
1163 netdev->cache_valid |= VALID_IN4;
1164 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
1165 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1166 if (!error) {
1167 *netmask = sin->sin_addr;
1168 }
1169 }
1170 }
1171 if (!error) {
1172 *in4 = netdev->in4;
1173 *netmask = netdev->netmask;
1174 }
1175 ovs_mutex_unlock(&netdev->mutex);
1176
1177 return error ? error : in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
1178 }
1179
1180 /*
1181 * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
1182 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
1183 * positive errno value.
1184 */
1185 static int
1186 netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
1187 struct in_addr mask)
1188 {
1189 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1190 int error;
1191
1192 ovs_mutex_lock(&netdev->mutex);
1193 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
1194 if (!error) {
1195 if (addr.s_addr != INADDR_ANY) {
1196 error = do_set_addr(netdev_, SIOCSIFNETMASK,
1197 "SIOCSIFNETMASK", mask);
1198 if (!error) {
1199 netdev->cache_valid |= VALID_IN4;
1200 netdev->in4 = addr;
1201 netdev->netmask = mask;
1202 }
1203 }
1204 seq_change(connectivity_seq_get());
1205 }
1206 ovs_mutex_unlock(&netdev->mutex);
1207
1208 return error;
1209 }
1210
1211 static int
1212 netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1213 {
1214 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1215 if (!(netdev->cache_valid & VALID_IN6)) {
1216 struct ifaddrs *ifa, *head;
1217 struct sockaddr_in6 *sin6;
1218 const char *netdev_name = netdev_get_name(netdev_);
1219
1220 if (getifaddrs(&head) != 0) {
1221 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1222 ovs_strerror(errno));
1223 return errno;
1224 }
1225
1226 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1227 if (ifa->ifa_addr->sa_family == AF_INET6 &&
1228 !strcmp(ifa->ifa_name, netdev_name)) {
1229 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1230 if (sin6) {
1231 memcpy(&netdev->in6, &sin6->sin6_addr, sin6->sin6_len);
1232 netdev->cache_valid |= VALID_IN6;
1233 *in6 = netdev->in6;
1234 freeifaddrs(head);
1235 return 0;
1236 }
1237 }
1238 }
1239 return EADDRNOTAVAIL;
1240 }
1241 *in6 = netdev->in6;
1242 return 0;
1243 }
1244
1245 #if defined(__NetBSD__)
1246 static char *
1247 netdev_bsd_kernel_name_to_ovs_name(const char *kernel_name)
1248 {
1249 char *ovs_name = NULL;
1250 struct shash device_shash;
1251 struct shash_node *node;
1252
1253 shash_init(&device_shash);
1254 netdev_get_devices(&netdev_tap_class, &device_shash);
1255 SHASH_FOR_EACH(node, &device_shash) {
1256 struct netdev *netdev = node->data;
1257 struct netdev_bsd * const dev = netdev_bsd_cast(netdev);
1258
1259 if (!strcmp(dev->kernel_name, kernel_name)) {
1260 free(ovs_name);
1261 ovs_name = xstrdup(netdev_get_name(&dev->up));
1262 }
1263 netdev_close(netdev);
1264 }
1265 shash_destroy(&device_shash);
1266
1267 return ovs_name ? ovs_name : xstrdup(kernel_name);
1268 }
1269 #endif
1270
1271 static int
1272 netdev_bsd_get_next_hop(const struct in_addr *host OVS_UNUSED,
1273 struct in_addr *next_hop OVS_UNUSED,
1274 char **netdev_name OVS_UNUSED)
1275 {
1276 #if defined(__NetBSD__)
1277 static int seq = 0;
1278 struct sockaddr_in sin;
1279 struct sockaddr_dl sdl;
1280 int s;
1281 int i;
1282 struct {
1283 struct rt_msghdr h;
1284 char space[512];
1285 } buf;
1286 struct rt_msghdr *rtm = &buf.h;
1287 const pid_t pid = getpid();
1288 char *cp;
1289 ssize_t ssz;
1290 bool gateway = false;
1291 char *ifname = NULL;
1292 int saved_errno;
1293
1294 memset(next_hop, 0, sizeof(*next_hop));
1295 *netdev_name = NULL;
1296
1297 memset(&sin, 0, sizeof(sin));
1298 sin.sin_len = sizeof(sin);
1299 sin.sin_family = AF_INET;
1300 sin.sin_port = 0;
1301 sin.sin_addr = *host;
1302
1303 memset(&sdl, 0, sizeof(sdl));
1304 sdl.sdl_len = sizeof(sdl);
1305 sdl.sdl_family = AF_LINK;
1306
1307 s = socket(PF_ROUTE, SOCK_RAW, 0);
1308 memset(&buf, 0, sizeof(buf));
1309 rtm->rtm_flags = RTF_HOST|RTF_UP;
1310 rtm->rtm_version = RTM_VERSION;
1311 rtm->rtm_addrs = RTA_DST|RTA_IFP;
1312 cp = (void *)&buf.space;
1313 memcpy(cp, &sin, sizeof(sin));
1314 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sin);
1315 memcpy(cp, &sdl, sizeof(sdl));
1316 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sdl);
1317 rtm->rtm_msglen = cp - (char *)(void *)rtm;
1318 rtm->rtm_seq = ++seq;
1319 rtm->rtm_type = RTM_GET;
1320 rtm->rtm_pid = pid;
1321 write(s, rtm, rtm->rtm_msglen);
1322 memset(&buf, 0, sizeof(buf));
1323 do {
1324 ssz = read(s, &buf, sizeof(buf));
1325 } while (ssz > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
1326 saved_errno = errno;
1327 close(s);
1328 if (ssz <= 0) {
1329 if (ssz < 0) {
1330 return saved_errno;
1331 }
1332 return EPIPE; /* XXX */
1333 }
1334 cp = (void *)&buf.space;
1335 for (i = 1; i; i <<= 1) {
1336 if ((rtm->rtm_addrs & i) != 0) {
1337 const struct sockaddr *sa = (const void *)cp;
1338
1339 if ((i == RTA_GATEWAY) && sa->sa_family == AF_INET) {
1340 const struct sockaddr_in * const sin =
1341 (const struct sockaddr_in *)sa;
1342
1343 *next_hop = sin->sin_addr;
1344 gateway = true;
1345 }
1346 if ((i == RTA_IFP) && sa->sa_family == AF_LINK) {
1347 const struct sockaddr_dl * const sdl =
1348 (const struct sockaddr_dl *)sa;
1349 char *kernel_name;
1350
1351 kernel_name = xmemdup0(sdl->sdl_data, sdl->sdl_nlen);
1352 ifname = netdev_bsd_kernel_name_to_ovs_name(kernel_name);
1353 free(kernel_name);
1354 }
1355 RT_ADVANCE(cp, sa);
1356 }
1357 }
1358 if (ifname == NULL) {
1359 return ENXIO;
1360 }
1361 if (!gateway) {
1362 *next_hop = *host;
1363 }
1364 *netdev_name = ifname;
1365 VLOG_DBG("host " IP_FMT " next-hop " IP_FMT " if %s",
1366 IP_ARGS(host->s_addr), IP_ARGS(next_hop->s_addr), *netdev_name);
1367 return 0;
1368 #else
1369 return EOPNOTSUPP;
1370 #endif
1371 }
1372
1373 static int
1374 netdev_bsd_arp_lookup(const struct netdev *netdev OVS_UNUSED,
1375 ovs_be32 ip OVS_UNUSED,
1376 uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1377 {
1378 #if defined(__NetBSD__)
1379 const struct rt_msghdr *rtm;
1380 size_t needed;
1381 char *buf;
1382 const char *cp;
1383 const char *ep;
1384 int mib[6];
1385 int error;
1386
1387 buf = NULL;
1388 mib[0] = CTL_NET;
1389 mib[1] = PF_ROUTE;
1390 mib[2] = 0;
1391 mib[3] = AF_INET;
1392 mib[4] = NET_RT_FLAGS;
1393 mib[5] = RTF_LLINFO;
1394 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) {
1395 error = errno;
1396 goto error;
1397 }
1398 buf = xmalloc(needed);
1399 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) {
1400 error = errno;
1401 goto error;
1402 }
1403 ep = buf + needed;
1404 for (cp = buf; cp < ep; cp += rtm->rtm_msglen) {
1405 const struct sockaddr_inarp *sina;
1406 const struct sockaddr_dl *sdl;
1407
1408 rtm = (const void *)cp;
1409 sina = (const void *)(rtm + 1);
1410 if (ip != sina->sin_addr.s_addr) {
1411 continue;
1412 }
1413 sdl = (const void *)
1414 ((const char *)(const void *)sina + RT_ROUNDUP(sina->sin_len));
1415 if (sdl->sdl_alen == ETH_ADDR_LEN) {
1416 memcpy(mac, &sdl->sdl_data[sdl->sdl_nlen], ETH_ADDR_LEN);
1417 error = 0;
1418 goto error;
1419 }
1420 }
1421 error = ENXIO;
1422 error:
1423 free(buf);
1424 return error;
1425 #else
1426 return EOPNOTSUPP;
1427 #endif
1428 }
1429
1430 static void
1431 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1432 {
1433 struct sockaddr_in sin;
1434 memset(&sin, 0, sizeof sin);
1435 sin.sin_family = AF_INET;
1436 sin.sin_addr = addr;
1437 sin.sin_port = 0;
1438
1439 memset(sa, 0, sizeof *sa);
1440 memcpy(sa, &sin, sizeof sin);
1441 }
1442
1443 static int
1444 do_set_addr(struct netdev *netdev,
1445 unsigned long ioctl_nr, const char *ioctl_name,
1446 struct in_addr addr)
1447 {
1448 struct ifreq ifr;
1449 make_in4_sockaddr(&ifr.ifr_addr, addr);
1450 return af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr,
1451 ioctl_name);
1452 }
1453
1454 static int
1455 nd_to_iff_flags(enum netdev_flags nd)
1456 {
1457 int iff = 0;
1458 if (nd & NETDEV_UP) {
1459 iff |= IFF_UP;
1460 }
1461 if (nd & NETDEV_PROMISC) {
1462 iff |= IFF_PROMISC;
1463 #if defined(IFF_PPROMISC)
1464 iff |= IFF_PPROMISC;
1465 #endif
1466 }
1467 if (nd & NETDEV_LOOPBACK) {
1468 iff |= IFF_LOOPBACK;
1469 }
1470 return iff;
1471 }
1472
1473 static int
1474 iff_to_nd_flags(int iff)
1475 {
1476 enum netdev_flags nd = 0;
1477 if (iff & IFF_UP) {
1478 nd |= NETDEV_UP;
1479 }
1480 if (iff & IFF_PROMISC) {
1481 nd |= NETDEV_PROMISC;
1482 }
1483 if (iff & IFF_LOOPBACK) {
1484 nd |= NETDEV_LOOPBACK;
1485 }
1486 return nd;
1487 }
1488
1489 static int
1490 netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
1491 enum netdev_flags on, enum netdev_flags *old_flagsp)
1492 {
1493 int old_flags, new_flags;
1494 int error;
1495
1496 error = get_flags(netdev_, &old_flags);
1497 if (!error) {
1498 *old_flagsp = iff_to_nd_flags(old_flags);
1499 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1500 if (new_flags != old_flags) {
1501 error = set_flags(netdev_get_kernel_name(netdev_), new_flags);
1502 seq_change(connectivity_seq_get());
1503 }
1504 }
1505 return error;
1506 }
1507
1508 /* Linux has also different GET_STATS, SET_STATS,
1509 * GET_STATUS)
1510 */
1511 #define NETDEV_BSD_CLASS(NAME, CONSTRUCT, \
1512 GET_FEATURES) \
1513 { \
1514 NAME, \
1515 \
1516 NULL, /* init */ \
1517 netdev_bsd_run, \
1518 netdev_bsd_wait, \
1519 netdev_bsd_alloc, \
1520 CONSTRUCT, \
1521 netdev_bsd_destruct, \
1522 netdev_bsd_dealloc, \
1523 NULL, /* get_config */ \
1524 NULL, /* set_config */ \
1525 NULL, /* get_tunnel_config */ \
1526 \
1527 netdev_bsd_send, \
1528 netdev_bsd_send_wait, \
1529 \
1530 netdev_bsd_set_etheraddr, \
1531 netdev_bsd_get_etheraddr, \
1532 netdev_bsd_get_mtu, \
1533 NULL, /* set_mtu */ \
1534 netdev_bsd_get_ifindex, \
1535 netdev_bsd_get_carrier, \
1536 NULL, /* get_carrier_resets */ \
1537 NULL, /* set_miimon_interval */ \
1538 netdev_bsd_get_stats, \
1539 NULL, /* set_stats */ \
1540 \
1541 GET_FEATURES, \
1542 NULL, /* set_advertisement */ \
1543 NULL, /* set_policing */ \
1544 NULL, /* get_qos_type */ \
1545 NULL, /* get_qos_capabilities */ \
1546 NULL, /* get_qos */ \
1547 NULL, /* set_qos */ \
1548 NULL, /* get_queue */ \
1549 NULL, /* set_queue */ \
1550 NULL, /* delete_queue */ \
1551 NULL, /* get_queue_stats */ \
1552 NULL, /* queue_dump_start */ \
1553 NULL, /* queue_dump_next */ \
1554 NULL, /* queue_dump_done */ \
1555 NULL, /* dump_queue_stats */ \
1556 \
1557 netdev_bsd_get_in4, \
1558 netdev_bsd_set_in4, \
1559 netdev_bsd_get_in6, \
1560 NULL, /* add_router */ \
1561 netdev_bsd_get_next_hop, \
1562 NULL, /* get_status */ \
1563 netdev_bsd_arp_lookup, /* arp_lookup */ \
1564 \
1565 netdev_bsd_update_flags, \
1566 \
1567 netdev_bsd_rx_alloc, \
1568 netdev_bsd_rx_construct, \
1569 netdev_bsd_rx_destruct, \
1570 netdev_bsd_rx_dealloc, \
1571 netdev_bsd_rx_recv, \
1572 netdev_bsd_rx_wait, \
1573 netdev_bsd_rx_drain, \
1574 }
1575
1576 const struct netdev_class netdev_bsd_class =
1577 NETDEV_BSD_CLASS(
1578 "system",
1579 netdev_bsd_construct_system,
1580 netdev_bsd_get_features);
1581
1582 const struct netdev_class netdev_tap_class =
1583 NETDEV_BSD_CLASS(
1584 "tap",
1585 netdev_bsd_construct_tap,
1586 netdev_bsd_get_features);
1587 \f
1588
1589 static void
1590 destroy_tap(int fd, const char *name)
1591 {
1592 struct ifreq ifr;
1593
1594 close(fd);
1595 strcpy(ifr.ifr_name, name);
1596 /* XXX What to do if this call fails? */
1597 af_inet_ioctl(SIOCIFDESTROY, &ifr);
1598 }
1599
1600 static int
1601 get_flags(const struct netdev *netdev, int *flags)
1602 {
1603 struct ifreq ifr;
1604 int error;
1605
1606 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr,
1607 SIOCGIFFLAGS, "SIOCGIFFLAGS");
1608
1609 *flags = ifr_get_flags(&ifr);
1610
1611 return error;
1612 }
1613
1614 static int
1615 set_flags(const char *name, int flags)
1616 {
1617 struct ifreq ifr;
1618
1619 ifr_set_flags(&ifr, flags);
1620
1621 return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1622 }
1623
1624 static int
1625 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1626 {
1627 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1628 *ifindexp = 0;
1629 if (!(netdev->cache_valid & VALID_IFINDEX)) {
1630 int ifindex = if_nametoindex(netdev_get_name(netdev_));
1631 if (ifindex <= 0) {
1632 return errno;
1633 }
1634 netdev->cache_valid |= VALID_IFINDEX;
1635 netdev->ifindex = ifindex;
1636 }
1637 *ifindexp = netdev->ifindex;
1638 return 0;
1639 }
1640
1641 static int
1642 get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1643 {
1644 struct ifaddrs *head;
1645 struct ifaddrs *ifa;
1646 struct sockaddr_dl *sdl;
1647
1648 if (getifaddrs(&head) != 0) {
1649 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1650 ovs_strerror(errno));
1651 return errno;
1652 }
1653
1654 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1655 if (ifa->ifa_addr->sa_family == AF_LINK) {
1656 if (!strcmp(ifa->ifa_name, netdev_name)) {
1657 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1658 if (sdl) {
1659 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
1660 freeifaddrs(head);
1661 return 0;
1662 }
1663 }
1664 }
1665 }
1666
1667 VLOG_ERR("could not find ethernet address for %s device", netdev_name);
1668 freeifaddrs(head);
1669 return ENODEV;
1670 }
1671
1672 static int
1673 set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED,
1674 int hwaddr_len OVS_UNUSED,
1675 const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
1676 {
1677 #if defined(__FreeBSD__)
1678 struct ifreq ifr;
1679 int error;
1680
1681 memset(&ifr, 0, sizeof ifr);
1682 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1683 ifr.ifr_addr.sa_family = hwaddr_family;
1684 ifr.ifr_addr.sa_len = hwaddr_len;
1685 memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
1686 error = af_inet_ioctl(SIOCSIFLLADDR, &ifr);
1687 if (error) {
1688 VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
1689 netdev_name, ovs_strerror(error));
1690 return error;
1691 }
1692 return 0;
1693 #elif defined(__NetBSD__)
1694 struct if_laddrreq req;
1695 struct sockaddr_dl *sdl;
1696 struct sockaddr_storage oldaddr;
1697 int error;
1698
1699 /*
1700 * get the old address, add new one, and then remove old one.
1701 */
1702
1703 if (hwaddr_len != ETH_ADDR_LEN) {
1704 /* just to be safe about sockaddr storage size */
1705 return EOPNOTSUPP;
1706 }
1707 memset(&req, 0, sizeof(req));
1708 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1709 req.addr.ss_len = sizeof(req.addr);
1710 req.addr.ss_family = hwaddr_family;
1711 sdl = (struct sockaddr_dl *)&req.addr;
1712 sdl->sdl_alen = hwaddr_len;
1713
1714 error = af_link_ioctl(SIOCGLIFADDR, &req);
1715 if (error) {
1716 return error;
1717 }
1718 if (!memcmp(&sdl->sdl_data[sdl->sdl_nlen], mac, hwaddr_len)) {
1719 return 0;
1720 }
1721 oldaddr = req.addr;
1722
1723 memset(&req, 0, sizeof(req));
1724 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1725 req.flags = IFLR_ACTIVE;
1726 sdl = (struct sockaddr_dl *)&req.addr;
1727 sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data) + hwaddr_len;
1728 sdl->sdl_alen = hwaddr_len;
1729 sdl->sdl_family = hwaddr_family;
1730 memcpy(sdl->sdl_data, mac, hwaddr_len);
1731 error = af_link_ioctl(SIOCALIFADDR, &req);
1732 if (error) {
1733 return error;
1734 }
1735
1736 memset(&req, 0, sizeof(req));
1737 strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1738 req.addr = oldaddr;
1739 return af_link_ioctl(SIOCDLIFADDR, &req);
1740 #else
1741 #error not implemented
1742 #endif
1743 }
1744
1745 static int
1746 ifr_get_flags(const struct ifreq *ifr)
1747 {
1748 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1749 return (ifr->ifr_flagshigh << 16) | ifr->ifr_flags;
1750 #else
1751 return ifr->ifr_flags;
1752 #endif
1753 }
1754
1755 static void
1756 ifr_set_flags(struct ifreq *ifr, int flags)
1757 {
1758 ifr->ifr_flags = flags;
1759 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1760 ifr->ifr_flagshigh = flags >> 16;
1761 #endif
1762 }
1763
1764 /* Calls ioctl() on an AF_LINK sock, passing the specified 'command' and
1765 * 'arg'. Returns 0 if successful, otherwise a positive errno value. */
1766 int
1767 af_link_ioctl(unsigned long command, const void *arg)
1768 {
1769 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1770 static int sock;
1771
1772 if (ovsthread_once_start(&once)) {
1773 sock = socket(AF_LINK, SOCK_DGRAM, 0);
1774 if (sock < 0) {
1775 sock = -errno;
1776 VLOG_ERR("failed to create link socket: %s", ovs_strerror(errno));
1777 }
1778 ovsthread_once_done(&once);
1779 }
1780
1781 return (sock < 0 ? -sock
1782 : ioctl(sock, command, arg) == -1 ? errno
1783 : 0);
1784 }