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