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