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