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