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