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