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