]> git.proxmox.com Git - mirror_ovs.git/blob - lib/netdev-bsd.c
tunneling: Handle multiple ip address for given device.
[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 "poll-loop.h"
60 #include "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 \f
69 struct netdev_rxq_bsd {
70 struct netdev_rxq up;
71
72 /* Packet capture descriptor for a system network device.
73 * For a tap device this is NULL. */
74 pcap_t *pcap_handle;
75
76 /* Selectable file descriptor for the network device.
77 * This descriptor will be used for polling operations. */
78 int fd;
79 };
80
81 struct netdev_bsd {
82 struct netdev up;
83
84 /* Never changes after initialization. */
85 char *kernel_name;
86
87 /* Protects all members below. */
88 struct ovs_mutex mutex;
89
90 unsigned int cache_valid;
91
92 int ifindex;
93 struct eth_addr etheraddr;
94 struct in_addr in4;
95 struct in_addr netmask;
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, struct eth_addr *ea);
141 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
142 int hwaddr_len, const struct eth_addr);
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 ovs_mutex_destroy(&netdev->mutex);
298 return error;
299 }
300
301 return 0;
302 }
303
304 static int
305 netdev_bsd_construct_tap(struct netdev *netdev_)
306 {
307 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
308 const char *name = netdev_->name;
309 int error = 0;
310 struct ifreq ifr;
311 char *kernel_name = NULL;
312
313 error = cache_notifier_ref();
314 if (error) {
315 goto error;
316 }
317
318 memset(&ifr, 0, sizeof(ifr));
319
320 /* Create a tap device by opening /dev/tap. The TAPGIFNAME ioctl is used
321 * to retrieve the name of the tap device. */
322 ovs_mutex_init(&netdev->mutex);
323 netdev->tap_fd = open("/dev/tap", O_RDWR);
324 if (netdev->tap_fd < 0) {
325 error = errno;
326 VLOG_WARN("opening \"/dev/tap\" failed: %s", ovs_strerror(error));
327 goto error_unref_notifier;
328 }
329
330 /* Retrieve tap name (e.g. tap0) */
331 if (ioctl(netdev->tap_fd, TAPGIFNAME, &ifr) == -1) {
332 /* XXX Need to destroy the device? */
333 error = errno;
334 close(netdev->tap_fd);
335 goto error_unref_notifier;
336 }
337
338 /* Change the name of the tap device */
339 #if defined(SIOCSIFNAME)
340 ifr.ifr_data = (void *)name;
341 error = af_inet_ioctl(SIOCSIFNAME, &ifr);
342 if (error) {
343 destroy_tap(netdev->tap_fd, ifr.ifr_name);
344 goto error_unref_notifier;
345 }
346 kernel_name = xstrdup(name);
347 #else
348 /*
349 * NetBSD doesn't support inteface renaming.
350 */
351 VLOG_INFO("tap %s is created for bridge %s", ifr.ifr_name, name);
352 kernel_name = xstrdup(ifr.ifr_name);
353 #endif
354
355 /* set non-blocking. */
356 error = set_nonblocking(netdev->tap_fd);
357 if (error) {
358 destroy_tap(netdev->tap_fd, kernel_name);
359 goto error_unref_notifier;
360 }
361
362 /* Turn device UP */
363 ifr_set_flags(&ifr, IFF_UP);
364 ovs_strlcpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
365 error = af_inet_ioctl(SIOCSIFFLAGS, &ifr);
366 if (error) {
367 destroy_tap(netdev->tap_fd, kernel_name);
368 goto error_unref_notifier;
369 }
370
371 netdev->kernel_name = kernel_name;
372
373 return 0;
374
375 error_unref_notifier:
376 ovs_mutex_destroy(&netdev->mutex);
377 cache_notifier_unref();
378 error:
379 free(kernel_name);
380 return error;
381 }
382
383 static void
384 netdev_bsd_destruct(struct netdev *netdev_)
385 {
386 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
387
388 cache_notifier_unref();
389
390 if (netdev->tap_fd >= 0) {
391 destroy_tap(netdev->tap_fd, netdev_get_kernel_name(netdev_));
392 }
393 if (netdev->pcap) {
394 pcap_close(netdev->pcap);
395 }
396 free(netdev->kernel_name);
397 ovs_mutex_destroy(&netdev->mutex);
398 }
399
400 static void
401 netdev_bsd_dealloc(struct netdev *netdev_)
402 {
403 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
404
405 free(netdev);
406 }
407
408 static int
409 netdev_bsd_open_pcap(const char *name, pcap_t **pcapp, int *fdp)
410 {
411 char errbuf[PCAP_ERRBUF_SIZE];
412 pcap_t *pcap = NULL;
413 int one = 1;
414 int error;
415 int fd;
416
417 /* Open the pcap device. The device is opened in non-promiscuous mode
418 * because the interface flags are manually set by the caller. */
419 errbuf[0] = '\0';
420 pcap = pcap_open_live(name, PCAP_SNAPLEN, 0, 1000, errbuf);
421 if (!pcap) {
422 VLOG_ERR_RL(&rl, "%s: pcap_open_live failed: %s", name, errbuf);
423 error = EIO;
424 goto error;
425 }
426 if (errbuf[0] != '\0') {
427 VLOG_WARN_RL(&rl, "%s: pcap_open_live: %s", name, errbuf);
428 }
429
430 /* Get the underlying fd. */
431 fd = pcap_get_selectable_fd(pcap);
432 if (fd == -1) {
433 VLOG_WARN_RL(&rl, "%s: no selectable file descriptor", name);
434 error = errno;
435 goto error;
436 }
437
438 /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
439 * on the file descriptor returned by pcap_get_selectable_fd to achieve
440 * a real non-blocking behaviour.*/
441 error = pcap_setnonblock(pcap, 1, errbuf);
442 if (error == -1) {
443 error = errno;
444 goto error;
445 }
446
447 /* This call assure that reads return immediately upon packet
448 * reception. Otherwise, a read will block until either the kernel
449 * buffer becomes full or a timeout occurs. */
450 if (ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) {
451 VLOG_ERR_RL(&rl, "ioctl(BIOCIMMEDIATE) on %s device failed: %s",
452 name, ovs_strerror(errno));
453 error = errno;
454 goto error;
455 }
456
457 /* Capture only incoming packets. */
458 error = pcap_setdirection(pcap, PCAP_D_IN);
459 if (error == -1) {
460 error = errno;
461 goto error;
462 }
463
464 *pcapp = pcap;
465 *fdp = fd;
466 return 0;
467
468 error:
469 if (pcap) {
470 pcap_close(pcap);
471 }
472 *pcapp = NULL;
473 *fdp = -1;
474 return error;
475 }
476
477 static struct netdev_rxq *
478 netdev_bsd_rxq_alloc(void)
479 {
480 struct netdev_rxq_bsd *rxq = xzalloc(sizeof *rxq);
481 return &rxq->up;
482 }
483
484 static int
485 netdev_bsd_rxq_construct(struct netdev_rxq *rxq_)
486 {
487 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
488 struct netdev *netdev_ = rxq->up.netdev;
489 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
490 int error;
491
492 if (!strcmp(netdev_get_type(netdev_), "tap")) {
493 rxq->pcap_handle = NULL;
494 rxq->fd = netdev->tap_fd;
495 error = 0;
496 } else {
497 ovs_mutex_lock(&netdev->mutex);
498 error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_),
499 &rxq->pcap_handle, &rxq->fd);
500 ovs_mutex_unlock(&netdev->mutex);
501 }
502
503 return error;
504 }
505
506 static void
507 netdev_bsd_rxq_destruct(struct netdev_rxq *rxq_)
508 {
509 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
510
511 if (rxq->pcap_handle) {
512 pcap_close(rxq->pcap_handle);
513 }
514 }
515
516 static void
517 netdev_bsd_rxq_dealloc(struct netdev_rxq *rxq_)
518 {
519 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
520
521 free(rxq);
522 }
523
524 /* The recv callback of the netdev class returns the number of bytes of the
525 * received packet.
526 *
527 * This can be done by the pcap_next() function. Unfortunately pcap_next() does
528 * not make difference between a missing packet on the capture interface and
529 * an error during the file capture. We can use the pcap_dispatch() function
530 * instead, which is able to distinguish between errors and null packet.
531 *
532 * To make pcap_dispatch() returns the number of bytes read from the interface
533 * we need to define the following callback and argument.
534 */
535 struct pcap_arg {
536 void *data;
537 int size;
538 int retval;
539 };
540
541 /*
542 * This callback will be executed on every captured packet.
543 *
544 * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
545 * pcap returns a truncated packet and we follow this behavior.
546 *
547 * The argument args->retval is the packet size in bytes.
548 */
549 static void
550 proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
551 {
552 struct pcap_arg *args = ALIGNED_CAST(struct pcap_arg *, args_);
553
554 if (args->size < hdr->len) {
555 VLOG_WARN_RL(&rl, "packet truncated");
556 args->retval = args->size;
557 } else {
558 args->retval = hdr->len;
559 }
560
561 /* copy the packet to our buffer */
562 memcpy(args->data, packet, args->retval);
563 }
564
565 /*
566 * This function attempts to receive a packet from the specified network
567 * device. It is assumed that the network device is a system device or a tap
568 * device opened as a system one. In this case the read operation is performed
569 * from rxq->pcap.
570 */
571 static int
572 netdev_rxq_bsd_recv_pcap(struct netdev_rxq_bsd *rxq, struct dp_packet *buffer)
573 {
574 struct pcap_arg arg;
575 int ret;
576
577 /* prepare the pcap argument to store the packet */
578 arg.size = dp_packet_tailroom(buffer);
579 arg.data = dp_packet_data(buffer);
580
581 for (;;) {
582 ret = pcap_dispatch(rxq->pcap_handle, 1, proc_pkt, (u_char *) &arg);
583
584 if (ret > 0) {
585 dp_packet_set_size(buffer, dp_packet_size(buffer) + arg.retval);
586 return 0;
587 }
588 if (ret == -1) {
589 if (errno == EINTR) {
590 continue;
591 }
592 }
593
594 return EAGAIN;
595 }
596 }
597
598 /*
599 * This function attempts to receive a packet from the specified network
600 * device. It is assumed that the network device is a tap device and
601 * 'rxq->fd' is initialized with the tap file descriptor.
602 */
603 static int
604 netdev_rxq_bsd_recv_tap(struct netdev_rxq_bsd *rxq, struct dp_packet *buffer)
605 {
606 size_t size = dp_packet_tailroom(buffer);
607
608 for (;;) {
609 ssize_t retval = read(rxq->fd, dp_packet_data(buffer), size);
610 if (retval >= 0) {
611 dp_packet_set_size(buffer, dp_packet_size(buffer) + retval);
612 return 0;
613 } else if (errno != EINTR) {
614 if (errno != EAGAIN) {
615 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
616 ovs_strerror(errno), netdev_rxq_get_name(&rxq->up));
617 }
618 return errno;
619 }
620 }
621 }
622
623 static int
624 netdev_bsd_rxq_recv(struct netdev_rxq *rxq_, struct dp_packet **packets,
625 int *c)
626 {
627 struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
628 struct netdev *netdev = rxq->up.netdev;
629 struct dp_packet *packet;
630 ssize_t retval;
631 int mtu;
632
633 if (netdev_bsd_get_mtu(netdev, &mtu)) {
634 mtu = ETH_PAYLOAD_MAX;
635 }
636
637 packet = dp_packet_new_with_headroom(VLAN_ETH_HEADER_LEN + mtu,
638 DP_NETDEV_HEADROOM);
639 retval = (rxq->pcap_handle
640 ? netdev_rxq_bsd_recv_pcap(rxq, packet)
641 : netdev_rxq_bsd_recv_tap(rxq, packet));
642
643 if (retval) {
644 dp_packet_delete(packet);
645 } else {
646 dp_packet_pad(packet);
647 dp_packet_rss_invalidate(packet);
648 packets[0] = packet;
649 *c = 1;
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 **pkts, int cnt, bool may_steal)
689 {
690 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
691 const char *name = netdev_get_name(netdev_);
692 int error;
693 int i;
694
695 ovs_mutex_lock(&dev->mutex);
696 if (dev->tap_fd < 0 && !dev->pcap) {
697 error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd);
698 } else {
699 error = 0;
700 }
701
702 for (i = 0; i < cnt; i++) {
703 const void *data = dp_packet_data(pkts[i]);
704 size_t size = dp_packet_size(pkts[i]);
705
706 while (!error) {
707 ssize_t retval;
708 if (dev->tap_fd >= 0) {
709 retval = write(dev->tap_fd, data, size);
710 } else {
711 retval = pcap_inject(dev->pcap, data, size);
712 }
713 if (retval < 0) {
714 if (errno == EINTR) {
715 continue;
716 } else {
717 error = errno;
718 if (error != EAGAIN) {
719 VLOG_WARN_RL(&rl, "error sending Ethernet packet on"
720 " %s: %s", name, ovs_strerror(error));
721 }
722 }
723 } else if (retval != size) {
724 VLOG_WARN_RL(&rl, "sent partial Ethernet packet "
725 "(%"PRIuSIZE" bytes of "
726 "%"PRIuSIZE") on %s", retval, size, name);
727 error = EMSGSIZE;
728 } else {
729 break;
730 }
731 }
732 }
733
734 ovs_mutex_unlock(&dev->mutex);
735 if (may_steal) {
736 for (i = 0; i < cnt; i++) {
737 dp_packet_delete(pkts[i]);
738 }
739 }
740
741 return error;
742 }
743
744 /*
745 * Registers with the poll loop to wake up from the next call to poll_block()
746 * when the packet transmission queue has sufficient room to transmit a packet
747 * with netdev_send().
748 */
749 static void
750 netdev_bsd_send_wait(struct netdev *netdev_, int qid OVS_UNUSED)
751 {
752 struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
753
754 ovs_mutex_lock(&dev->mutex);
755 if (dev->tap_fd >= 0) {
756 /* TAP device always accepts packets. */
757 poll_immediate_wake();
758 } else if (dev->pcap) {
759 poll_fd_wait(dev->fd, POLLOUT);
760 } else {
761 /* We haven't even tried to send a packet yet. */
762 poll_immediate_wake();
763 }
764 ovs_mutex_unlock(&dev->mutex);
765 }
766
767 /*
768 * Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
769 * otherwise a positive errno value.
770 */
771 static int
772 netdev_bsd_set_etheraddr(struct netdev *netdev_,
773 const struct eth_addr mac)
774 {
775 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
776 int error = 0;
777
778 ovs_mutex_lock(&netdev->mutex);
779 if (!(netdev->cache_valid & VALID_ETHERADDR)
780 || !eth_addr_equals(netdev->etheraddr, mac)) {
781 error = set_etheraddr(netdev_get_kernel_name(netdev_), AF_LINK,
782 ETH_ADDR_LEN, mac);
783 if (!error) {
784 netdev->cache_valid |= VALID_ETHERADDR;
785 netdev->etheraddr = mac;
786 netdev_change_seq_changed(netdev_);
787 }
788 }
789 ovs_mutex_unlock(&netdev->mutex);
790
791 return error;
792 }
793
794 /*
795 * Returns a pointer to 'netdev''s MAC address. The caller must not modify or
796 * free the returned buffer.
797 */
798 static int
799 netdev_bsd_get_etheraddr(const struct netdev *netdev_, struct eth_addr *mac)
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 *mac = netdev->etheraddr;
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_addr_list(const struct netdev *netdev_,
1248 struct in6_addr **addr, struct in6_addr **mask, int *n_cnt)
1249 {
1250 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1251 int error;
1252
1253 if (!(netdev->cache_valid & VALID_IN6)) {
1254 netdev_get_addrs_list_flush();
1255 }
1256 error = netdev_get_addrs(netdev_get_name(netdev_), addr, mask, n_cnt);
1257 if (!error) {
1258 netdev->cache_valid |= VALID_IN6;
1259 }
1260 return error;
1261 }
1262
1263 #if defined(__NetBSD__)
1264 static char *
1265 netdev_bsd_kernel_name_to_ovs_name(const char *kernel_name)
1266 {
1267 char *ovs_name = NULL;
1268 struct shash device_shash;
1269 struct shash_node *node;
1270
1271 shash_init(&device_shash);
1272 netdev_get_devices(&netdev_tap_class, &device_shash);
1273 SHASH_FOR_EACH(node, &device_shash) {
1274 struct netdev *netdev = node->data;
1275 struct netdev_bsd * const dev = netdev_bsd_cast(netdev);
1276
1277 if (!strcmp(dev->kernel_name, kernel_name)) {
1278 free(ovs_name);
1279 ovs_name = xstrdup(netdev_get_name(&dev->up));
1280 }
1281 netdev_close(netdev);
1282 }
1283 shash_destroy(&device_shash);
1284
1285 return ovs_name ? ovs_name : xstrdup(kernel_name);
1286 }
1287 #endif
1288
1289 static int
1290 netdev_bsd_get_next_hop(const struct in_addr *host OVS_UNUSED,
1291 struct in_addr *next_hop OVS_UNUSED,
1292 char **netdev_name OVS_UNUSED)
1293 {
1294 #if defined(__NetBSD__)
1295 static int seq = 0;
1296 struct sockaddr_in sin;
1297 struct sockaddr_dl sdl;
1298 int s;
1299 int i;
1300 struct {
1301 struct rt_msghdr h;
1302 char space[512];
1303 } buf;
1304 struct rt_msghdr *rtm = &buf.h;
1305 const pid_t pid = getpid();
1306 char *cp;
1307 ssize_t ssz;
1308 bool gateway = false;
1309 char *ifname = NULL;
1310 int saved_errno;
1311
1312 memset(next_hop, 0, sizeof(*next_hop));
1313 *netdev_name = NULL;
1314
1315 memset(&sin, 0, sizeof(sin));
1316 sin.sin_len = sizeof(sin);
1317 sin.sin_family = AF_INET;
1318 sin.sin_port = 0;
1319 sin.sin_addr = *host;
1320
1321 memset(&sdl, 0, sizeof(sdl));
1322 sdl.sdl_len = sizeof(sdl);
1323 sdl.sdl_family = AF_LINK;
1324
1325 s = socket(PF_ROUTE, SOCK_RAW, 0);
1326 memset(&buf, 0, sizeof(buf));
1327 rtm->rtm_flags = RTF_HOST|RTF_UP;
1328 rtm->rtm_version = RTM_VERSION;
1329 rtm->rtm_addrs = RTA_DST|RTA_IFP;
1330 cp = (void *)&buf.space;
1331 memcpy(cp, &sin, sizeof(sin));
1332 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sin);
1333 memcpy(cp, &sdl, sizeof(sdl));
1334 RT_ADVANCE(cp, (struct sockaddr *)(void *)&sdl);
1335 rtm->rtm_msglen = cp - (char *)(void *)rtm;
1336 rtm->rtm_seq = ++seq;
1337 rtm->rtm_type = RTM_GET;
1338 rtm->rtm_pid = pid;
1339 write(s, rtm, rtm->rtm_msglen);
1340 memset(&buf, 0, sizeof(buf));
1341 do {
1342 ssz = read(s, &buf, sizeof(buf));
1343 } while (ssz > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
1344 saved_errno = errno;
1345 close(s);
1346 if (ssz <= 0) {
1347 if (ssz < 0) {
1348 return saved_errno;
1349 }
1350 return EPIPE; /* XXX */
1351 }
1352 cp = (void *)&buf.space;
1353 for (i = 1; i; i <<= 1) {
1354 if ((rtm->rtm_addrs & i) != 0) {
1355 const struct sockaddr *sa = (const void *)cp;
1356
1357 if ((i == RTA_GATEWAY) && sa->sa_family == AF_INET) {
1358 const struct sockaddr_in * const sin =
1359 ALIGNED_CAST(const struct sockaddr_in *, sa);
1360
1361 *next_hop = sin->sin_addr;
1362 gateway = true;
1363 }
1364 if ((i == RTA_IFP) && sa->sa_family == AF_LINK) {
1365 const struct sockaddr_dl * const sdl =
1366 ALIGNED_CAST(const struct sockaddr_dl *, sa);
1367 char *kernel_name;
1368
1369 kernel_name = xmemdup0(sdl->sdl_data, sdl->sdl_nlen);
1370 ifname = netdev_bsd_kernel_name_to_ovs_name(kernel_name);
1371 free(kernel_name);
1372 }
1373 RT_ADVANCE(cp, sa);
1374 }
1375 }
1376 if (ifname == NULL) {
1377 return ENXIO;
1378 }
1379 if (!gateway) {
1380 *next_hop = *host;
1381 }
1382 *netdev_name = ifname;
1383 VLOG_DBG("host " IP_FMT " next-hop " IP_FMT " if %s",
1384 IP_ARGS(host->s_addr), IP_ARGS(next_hop->s_addr), *netdev_name);
1385 return 0;
1386 #else
1387 return EOPNOTSUPP;
1388 #endif
1389 }
1390
1391 static int
1392 netdev_bsd_arp_lookup(const struct netdev *netdev OVS_UNUSED,
1393 ovs_be32 ip OVS_UNUSED,
1394 struct eth_addr *mac OVS_UNUSED)
1395 {
1396 #if defined(__NetBSD__)
1397 const struct rt_msghdr *rtm;
1398 size_t needed;
1399 char *buf;
1400 const char *cp;
1401 const char *ep;
1402 int mib[6];
1403 int error;
1404
1405 buf = NULL;
1406 mib[0] = CTL_NET;
1407 mib[1] = PF_ROUTE;
1408 mib[2] = 0;
1409 mib[3] = AF_INET;
1410 mib[4] = NET_RT_FLAGS;
1411 mib[5] = RTF_LLINFO;
1412 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) {
1413 error = errno;
1414 goto error;
1415 }
1416 buf = xmalloc(needed);
1417 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) {
1418 error = errno;
1419 goto error;
1420 }
1421 ep = buf + needed;
1422 for (cp = buf; cp < ep; cp += rtm->rtm_msglen) {
1423 const struct sockaddr_inarp *sina;
1424 const struct sockaddr_dl *sdl;
1425
1426 rtm = (const void *)cp;
1427 sina = (const void *)(rtm + 1);
1428 if (ip != sina->sin_addr.s_addr) {
1429 continue;
1430 }
1431 sdl = (const void *)
1432 ((const char *)(const void *)sina + RT_ROUNDUP(sina->sin_len));
1433 if (sdl->sdl_alen == ETH_ADDR_LEN) {
1434 memcpy(mac, &sdl->sdl_data[sdl->sdl_nlen], ETH_ADDR_LEN);
1435 error = 0;
1436 goto error;
1437 }
1438 }
1439 error = ENXIO;
1440 error:
1441 free(buf);
1442 return error;
1443 #else
1444 return EOPNOTSUPP;
1445 #endif
1446 }
1447
1448 static void
1449 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1450 {
1451 struct sockaddr_in sin;
1452 memset(&sin, 0, sizeof sin);
1453 sin.sin_family = AF_INET;
1454 sin.sin_addr = addr;
1455 sin.sin_port = 0;
1456
1457 memset(sa, 0, sizeof *sa);
1458 memcpy(sa, &sin, sizeof sin);
1459 }
1460
1461 static int
1462 do_set_addr(struct netdev *netdev,
1463 unsigned long ioctl_nr, const char *ioctl_name,
1464 struct in_addr addr)
1465 {
1466 struct ifreq ifr;
1467 make_in4_sockaddr(&ifr.ifr_addr, addr);
1468 return af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr,
1469 ioctl_name);
1470 }
1471
1472 static int
1473 nd_to_iff_flags(enum netdev_flags nd)
1474 {
1475 int iff = 0;
1476 if (nd & NETDEV_UP) {
1477 iff |= IFF_UP;
1478 }
1479 if (nd & NETDEV_PROMISC) {
1480 iff |= IFF_PROMISC;
1481 #if defined(IFF_PPROMISC)
1482 iff |= IFF_PPROMISC;
1483 #endif
1484 }
1485 if (nd & NETDEV_LOOPBACK) {
1486 iff |= IFF_LOOPBACK;
1487 }
1488 return iff;
1489 }
1490
1491 static int
1492 iff_to_nd_flags(int iff)
1493 {
1494 enum netdev_flags nd = 0;
1495 if (iff & IFF_UP) {
1496 nd |= NETDEV_UP;
1497 }
1498 if (iff & IFF_PROMISC) {
1499 nd |= NETDEV_PROMISC;
1500 }
1501 if (iff & IFF_LOOPBACK) {
1502 nd |= NETDEV_LOOPBACK;
1503 }
1504 return nd;
1505 }
1506
1507 static int
1508 netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
1509 enum netdev_flags on, enum netdev_flags *old_flagsp)
1510 {
1511 int old_flags, new_flags;
1512 int error;
1513
1514 error = get_flags(netdev_, &old_flags);
1515 if (!error) {
1516 *old_flagsp = iff_to_nd_flags(old_flags);
1517 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1518 if (new_flags != old_flags) {
1519 error = set_flags(netdev_get_kernel_name(netdev_), new_flags);
1520 netdev_change_seq_changed(netdev_);
1521 }
1522 }
1523 return error;
1524 }
1525
1526 /* Linux has also different GET_STATS, SET_STATS,
1527 * GET_STATUS)
1528 */
1529 #define NETDEV_BSD_CLASS(NAME, CONSTRUCT, \
1530 GET_FEATURES) \
1531 { \
1532 NAME, \
1533 false, /* is_pmd */ \
1534 \
1535 NULL, /* init */ \
1536 netdev_bsd_run, \
1537 netdev_bsd_wait, \
1538 netdev_bsd_alloc, \
1539 CONSTRUCT, \
1540 netdev_bsd_destruct, \
1541 netdev_bsd_dealloc, \
1542 NULL, /* get_config */ \
1543 NULL, /* set_config */ \
1544 NULL, /* get_tunnel_config */ \
1545 NULL, /* build header */ \
1546 NULL, /* push header */ \
1547 NULL, /* pop header */ \
1548 NULL, /* get_numa_id */ \
1549 NULL, /* set_multiq */ \
1550 \
1551 netdev_bsd_send, \
1552 netdev_bsd_send_wait, \
1553 \
1554 netdev_bsd_set_etheraddr, \
1555 netdev_bsd_get_etheraddr, \
1556 netdev_bsd_get_mtu, \
1557 NULL, /* set_mtu */ \
1558 netdev_bsd_get_ifindex, \
1559 netdev_bsd_get_carrier, \
1560 NULL, /* get_carrier_resets */ \
1561 NULL, /* set_miimon_interval */ \
1562 netdev_bsd_get_stats, \
1563 \
1564 GET_FEATURES, \
1565 NULL, /* set_advertisement */ \
1566 NULL, /* set_policing */ \
1567 NULL, /* get_qos_type */ \
1568 NULL, /* get_qos_capabilities */ \
1569 NULL, /* get_qos */ \
1570 NULL, /* set_qos */ \
1571 NULL, /* get_queue */ \
1572 NULL, /* set_queue */ \
1573 NULL, /* delete_queue */ \
1574 NULL, /* get_queue_stats */ \
1575 NULL, /* queue_dump_start */ \
1576 NULL, /* queue_dump_next */ \
1577 NULL, /* queue_dump_done */ \
1578 NULL, /* dump_queue_stats */ \
1579 \
1580 netdev_bsd_get_in4, \
1581 netdev_bsd_set_in4, \
1582 netdev_bsd_get_addr_list, \
1583 NULL, /* add_router */ \
1584 netdev_bsd_get_next_hop, \
1585 NULL, /* get_status */ \
1586 netdev_bsd_arp_lookup, /* arp_lookup */ \
1587 \
1588 netdev_bsd_update_flags, \
1589 \
1590 netdev_bsd_rxq_alloc, \
1591 netdev_bsd_rxq_construct, \
1592 netdev_bsd_rxq_destruct, \
1593 netdev_bsd_rxq_dealloc, \
1594 netdev_bsd_rxq_recv, \
1595 netdev_bsd_rxq_wait, \
1596 netdev_bsd_rxq_drain, \
1597 }
1598
1599 const struct netdev_class netdev_bsd_class =
1600 NETDEV_BSD_CLASS(
1601 "system",
1602 netdev_bsd_construct_system,
1603 netdev_bsd_get_features);
1604
1605 const struct netdev_class netdev_tap_class =
1606 NETDEV_BSD_CLASS(
1607 "tap",
1608 netdev_bsd_construct_tap,
1609 netdev_bsd_get_features);
1610 \f
1611
1612 static void
1613 destroy_tap(int fd, const char *name)
1614 {
1615 struct ifreq ifr;
1616
1617 close(fd);
1618 strcpy(ifr.ifr_name, name);
1619 /* XXX What to do if this call fails? */
1620 af_inet_ioctl(SIOCIFDESTROY, &ifr);
1621 }
1622
1623 static int
1624 get_flags(const struct netdev *netdev, int *flags)
1625 {
1626 struct ifreq ifr;
1627 int error;
1628
1629 error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr,
1630 SIOCGIFFLAGS, "SIOCGIFFLAGS");
1631
1632 *flags = ifr_get_flags(&ifr);
1633
1634 return error;
1635 }
1636
1637 static int
1638 set_flags(const char *name, int flags)
1639 {
1640 struct ifreq ifr;
1641
1642 ifr_set_flags(&ifr, flags);
1643
1644 return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
1645 }
1646
1647 static int
1648 get_ifindex(const struct netdev *netdev_, int *ifindexp)
1649 {
1650 struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
1651 *ifindexp = 0;
1652 if (!(netdev->cache_valid & VALID_IFINDEX)) {
1653 int ifindex = if_nametoindex(netdev_get_name(netdev_));
1654 if (ifindex <= 0) {
1655 return errno;
1656 }
1657 netdev->cache_valid |= VALID_IFINDEX;
1658 netdev->ifindex = ifindex;
1659 }
1660 *ifindexp = netdev->ifindex;
1661 return 0;
1662 }
1663
1664 static int
1665 get_etheraddr(const char *netdev_name, struct eth_addr *ea)
1666 {
1667 struct ifaddrs *head;
1668 struct ifaddrs *ifa;
1669 struct sockaddr_dl *sdl;
1670
1671 if (getifaddrs(&head) != 0) {
1672 VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
1673 ovs_strerror(errno));
1674 return errno;
1675 }
1676
1677 for (ifa = head; ifa; ifa = ifa->ifa_next) {
1678 if (ifa->ifa_addr->sa_family == AF_LINK) {
1679 if (!strcmp(ifa->ifa_name, netdev_name)) {
1680 sdl = ALIGNED_CAST(struct sockaddr_dl *, ifa->ifa_addr);
1681 if (sdl) {
1682 memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
1683 freeifaddrs(head);
1684 return 0;
1685 }
1686 }
1687 }
1688 }
1689
1690 VLOG_ERR("could not find ethernet address for %s device", netdev_name);
1691 freeifaddrs(head);
1692 return ENODEV;
1693 }
1694
1695 static int
1696 set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED,
1697 int hwaddr_len OVS_UNUSED,
1698 const struct eth_addr mac OVS_UNUSED)
1699 {
1700 #if defined(__FreeBSD__)
1701 struct ifreq ifr;
1702 int error;
1703
1704 memset(&ifr, 0, sizeof ifr);
1705 ovs_strlcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1706 ifr.ifr_addr.sa_family = hwaddr_family;
1707 ifr.ifr_addr.sa_len = hwaddr_len;
1708 memcpy(ifr.ifr_addr.sa_data, &mac, hwaddr_len);
1709 error = af_inet_ioctl(SIOCSIFLLADDR, &ifr);
1710 if (error) {
1711 VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
1712 netdev_name, ovs_strerror(error));
1713 return error;
1714 }
1715 return 0;
1716 #elif defined(__NetBSD__)
1717 struct if_laddrreq req;
1718 struct sockaddr_dl *sdl;
1719 struct sockaddr_storage oldaddr;
1720 int error;
1721
1722 /*
1723 * get the old address, add new one, and then remove old one.
1724 */
1725
1726 if (hwaddr_len != ETH_ADDR_LEN) {
1727 /* just to be safe about sockaddr storage size */
1728 return EOPNOTSUPP;
1729 }
1730 memset(&req, 0, sizeof(req));
1731 ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1732 req.addr.ss_len = sizeof(req.addr);
1733 req.addr.ss_family = hwaddr_family;
1734 sdl = (struct sockaddr_dl *)&req.addr;
1735 sdl->sdl_alen = hwaddr_len;
1736
1737 error = af_link_ioctl(SIOCGLIFADDR, &req);
1738 if (error) {
1739 return error;
1740 }
1741 if (!memcmp(&sdl->sdl_data[sdl->sdl_nlen], &mac, hwaddr_len)) {
1742 return 0;
1743 }
1744 oldaddr = req.addr;
1745
1746 memset(&req, 0, sizeof(req));
1747 ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1748 req.flags = IFLR_ACTIVE;
1749 sdl = (struct sockaddr_dl *)&req.addr;
1750 sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data) + hwaddr_len;
1751 sdl->sdl_alen = hwaddr_len;
1752 sdl->sdl_family = hwaddr_family;
1753 memcpy(sdl->sdl_data, &mac, hwaddr_len);
1754 error = af_link_ioctl(SIOCALIFADDR, &req);
1755 if (error) {
1756 return error;
1757 }
1758
1759 memset(&req, 0, sizeof(req));
1760 ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
1761 req.addr = oldaddr;
1762 return af_link_ioctl(SIOCDLIFADDR, &req);
1763 #else
1764 #error not implemented
1765 #endif
1766 }
1767
1768 static int
1769 ifr_get_flags(const struct ifreq *ifr)
1770 {
1771 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1772 return (ifr->ifr_flagshigh << 16) | (ifr->ifr_flags & 0xffff);
1773 #else
1774 return ifr->ifr_flags;
1775 #endif
1776 }
1777
1778 static void
1779 ifr_set_flags(struct ifreq *ifr, int flags)
1780 {
1781 #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
1782 ifr->ifr_flags = flags & 0xffff;
1783 ifr->ifr_flagshigh = flags >> 16;
1784 #else
1785 ifr->ifr_flags = flags;
1786 #endif
1787 }
1788
1789 #if defined(__NetBSD__)
1790 /* Calls ioctl() on an AF_LINK sock, passing the specified 'command' and
1791 * 'arg'. Returns 0 if successful, otherwise a positive errno value. */
1792 int
1793 af_link_ioctl(unsigned long command, const void *arg)
1794 {
1795 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1796 static int sock;
1797
1798 if (ovsthread_once_start(&once)) {
1799 sock = socket(AF_LINK, SOCK_DGRAM, 0);
1800 if (sock < 0) {
1801 sock = -errno;
1802 VLOG_ERR("failed to create link socket: %s", ovs_strerror(errno));
1803 }
1804 ovsthread_once_done(&once);
1805 }
1806
1807 return (sock < 0 ? -sock
1808 : ioctl(sock, command, arg) == -1 ? errno
1809 : 0);
1810 }
1811 #endif
1812 #endif /* !defined(__MACH__) */