summaryrefslogtreecommitdiff
path: root/lib/netdev-bsd.c
blob: 7875636cc3ccd10341bed904315f18a6e660d22b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
/*
 * Copyright (c) 2011, 2013, 2014 Gaetano Catalli.
 * Copyright (c) 2013, 2014 YAMAMOTO Takashi.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#if !defined(__MACH__)
#include <config.h>

#include "netdev-provider.h"
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <net/bpf.h>
#include <ifaddrs.h>
#include <pcap/pcap.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_tap.h>
#include <netinet/in.h>
#ifdef HAVE_NET_IF_MIB_H
#include <net/if_mib.h>
#endif
#include <poll.h>
#include <string.h>
#include <unistd.h>
#include <sys/sysctl.h>
#if defined(__NetBSD__)
#include <net/route.h>
#include <netinet/if_inarp.h>
#endif

#include "rtbsd.h"
#include "coverage.h"
#include "dp-packet.h"
#include "dpif-netdev.h"
#include "openvswitch/dynamic-string.h"
#include "fatal-signal.h"
#include "openflow/openflow.h"
#include "ovs-thread.h"
#include "packets.h"
#include "openvswitch/poll-loop.h"
#include "openvswitch/shash.h"
#include "socket-util.h"
#include "svec.h"
#include "util.h"
#include "openvswitch/vlog.h"

VLOG_DEFINE_THIS_MODULE(netdev_bsd);

struct netdev_rxq_bsd {
    struct netdev_rxq up;

    /* Packet capture descriptor for a system network device.
     * For a tap device this is NULL. */
    pcap_t *pcap_handle;

    /* Selectable file descriptor for the network device.
     * This descriptor will be used for polling operations. */
    int fd;
};

struct netdev_bsd {
    struct netdev up;

    /* Never changes after initialization. */
    char *kernel_name;

    /* Protects all members below. */
    struct ovs_mutex mutex;

    unsigned int cache_valid;

    int ifindex;
    struct eth_addr etheraddr;
    int mtu;
    int carrier;

    int tap_fd;         /* TAP character device, if any, otherwise -1. */

    /* Used for sending packets on non-tap devices. */
    pcap_t *pcap;
    int fd;
};


enum {
    VALID_IFINDEX   = 1 << 0,
    VALID_ETHERADDR = 1 << 1,
    VALID_IN        = 1 << 2,
    VALID_MTU       = 1 << 3,
    VALID_CARRIER   = 1 << 4
};

#define PCAP_SNAPLEN 2048


/*
 * Notifier used to invalidate device informations in case of status change.
 *
 * It will be registered with a 'rtbsd_notifier_register()' when the first
 * device will be created with the call of either 'netdev_bsd_tap_create()' or
 * 'netdev_bsd_system_create()'.
 *
 * The callback associated with this notifier ('netdev_bsd_cache_cb()') will
 * invalidate cached information about the device.
 */
static struct rtbsd_notifier netdev_bsd_cache_notifier;
static int cache_notifier_refcount;

static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);

static void destroy_tap(int fd, const char *name);
static int get_flags(const struct netdev *, int *flagsp);
static int set_flags(const char *, int flags);
static int do_set_addr(struct netdev *netdev,
                       unsigned long ioctl_nr, const char *ioctl_name,
                       struct in_addr addr);
static int get_etheraddr(const char *netdev_name, struct eth_addr *ea);
static int set_etheraddr(const char *netdev_name, int hwaddr_family,
                         int hwaddr_len, const struct eth_addr);
static int get_ifindex(const struct netdev *, int *ifindexp);

static int ifr_get_flags(const struct ifreq *);
static void ifr_set_flags(struct ifreq *, int flags);

#ifdef __NetBSD__
static int af_link_ioctl(unsigned long command, const void *arg);
#endif

static void netdev_bsd_run(const struct netdev_class *);
static int netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup);

static bool
is_netdev_bsd_class(const struct netdev_class *netdev_class)
{
    return netdev_class->run == netdev_bsd_run;
}

static struct netdev_bsd *
netdev_bsd_cast(const struct netdev *netdev)
{
    ovs_assert(is_netdev_bsd_class(netdev_get_class(netdev)));
    return CONTAINER_OF(netdev, struct netdev_bsd, up);
}

static struct netdev_rxq_bsd *
netdev_rxq_bsd_cast(const struct netdev_rxq *rxq)
{
    ovs_assert(is_netdev_bsd_class(netdev_get_class(rxq->netdev)));
    return CONTAINER_OF(rxq, struct netdev_rxq_bsd, up);
}

static const char *
netdev_get_kernel_name(const struct netdev *netdev)
{
    return netdev_bsd_cast(netdev)->kernel_name;
}

/*
 * Perform periodic work needed by netdev. In BSD netdevs it checks for any
 * interface status changes, and eventually calls all the user callbacks.
 */
static void
netdev_bsd_run(const struct netdev_class *netdev_class OVS_UNUSED)
{
    rtbsd_notifier_run();
}

/*
 * Arranges for poll_block() to wake up if the "run" member function needs to
 * be called.
 */
static void
netdev_bsd_wait(const struct netdev_class *netdev_class OVS_UNUSED)
{
    rtbsd_notifier_wait();
}

/* Invalidate cache in case of interface status change. */
static void
netdev_bsd_cache_cb(const struct rtbsd_change *change,
                    void *aux OVS_UNUSED)
{
    struct netdev_bsd *dev;

    if (change) {
        struct netdev *base_dev = netdev_from_name(change->if_name);

        if (base_dev) {
            const struct netdev_class *netdev_class =
                                                netdev_get_class(base_dev);

            if (is_netdev_bsd_class(netdev_class)) {
                dev = netdev_bsd_cast(base_dev);
                dev->cache_valid = 0;
                netdev_change_seq_changed(base_dev);
            }
            netdev_close(base_dev);
        }
    } else {
        /*
         * XXX the API is lacking, we should be able to iterate on the list of
         * netdevs without having to store the info in a temp shash.
         */
        struct shash device_shash;
        struct shash_node *node;

        shash_init(&device_shash);
        netdev_get_devices(&netdev_bsd_class, &device_shash);
        SHASH_FOR_EACH (node, &device_shash) {
            struct netdev *netdev = node->data;
            dev = netdev_bsd_cast(netdev);
            dev->cache_valid = 0;
            netdev_change_seq_changed(netdev);
            netdev_close(netdev);
        }
        shash_destroy(&device_shash);
    }
}

static int
cache_notifier_ref(void)
{
    int ret = 0;

    if (!cache_notifier_refcount) {
        ret = rtbsd_notifier_register(&netdev_bsd_cache_notifier,
                                      netdev_bsd_cache_cb, NULL);
        if (ret) {
            return ret;
        }
    }
    cache_notifier_refcount++;
    return 0;
}

static int
cache_notifier_unref(void)
{
    cache_notifier_refcount--;
    if (cache_notifier_refcount == 0) {
        rtbsd_notifier_unregister(&netdev_bsd_cache_notifier);
    }
    return 0;
}

static struct netdev *
netdev_bsd_alloc(void)
{
    struct netdev_bsd *netdev = xzalloc(sizeof *netdev);
    return &netdev->up;
}

static int
netdev_bsd_construct_system(struct netdev *netdev_)
{
    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
    enum netdev_flags flags;
    int error;

    error = cache_notifier_ref();
    if (error) {
        return error;
    }

    ovs_mutex_init(&netdev->mutex);
    netdev->tap_fd = -1;
    netdev->kernel_name = xstrdup(netdev_->name);

    /* Verify that the netdev really exists by attempting to read its flags */
    error = netdev_get_flags(netdev_, &flags);
    if (error == ENXIO) {
        free(netdev->kernel_name);
        cache_notifier_unref();
        ovs_mutex_destroy(&netdev->mutex);
        return error;
    }

    return 0;
}

static int
netdev_bsd_construct_tap(struct netdev *netdev_)
{
    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
    const char *name = netdev_->name;
    int error = 0;
    struct ifreq ifr;
    char *kernel_name = NULL;

    error = cache_notifier_ref();
    if (error) {
        goto error;
    }

    memset(&ifr, 0, sizeof(ifr));

    /* Create a tap device by opening /dev/tap.  The TAPGIFNAME ioctl is used
     * to retrieve the name of the tap device. */
    ovs_mutex_init(&netdev->mutex);
    netdev->tap_fd = open("/dev/tap", O_RDWR);
    if (netdev->tap_fd < 0) {
        error = errno;
        VLOG_WARN("opening \"/dev/tap\" failed: %s", ovs_strerror(error));
        goto error_unref_notifier;
    }

    /* Retrieve tap name (e.g. tap0) */
    if (ioctl(netdev->tap_fd, TAPGIFNAME, &ifr) == -1) {
        /* XXX Need to destroy the device? */
        error = errno;
        close(netdev->tap_fd);
        goto error_unref_notifier;
    }

    /* Change the name of the tap device */
#if defined(SIOCSIFNAME)
    ifr.ifr_data = (void *)name;
    error = af_inet_ioctl(SIOCSIFNAME, &ifr);
    if (error) {
        destroy_tap(netdev->tap_fd, ifr.ifr_name);
        goto error_unref_notifier;
    }
    kernel_name = xstrdup(name);
#else
    /*
     * NetBSD doesn't support inteface renaming.
     */
    VLOG_INFO("tap %s is created for bridge %s", ifr.ifr_name, name);
    kernel_name = xstrdup(ifr.ifr_name);
#endif

    /* set non-blocking. */
    error = set_nonblocking(netdev->tap_fd);
    if (error) {
        destroy_tap(netdev->tap_fd, kernel_name);
        goto error_unref_notifier;
    }

    /* Turn device UP */
    ifr_set_flags(&ifr, IFF_UP);
    ovs_strlcpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
    error = af_inet_ioctl(SIOCSIFFLAGS, &ifr);
    if (error) {
        destroy_tap(netdev->tap_fd, kernel_name);
        goto error_unref_notifier;
    }

    netdev->kernel_name = kernel_name;

    return 0;

error_unref_notifier:
    ovs_mutex_destroy(&netdev->mutex);
    cache_notifier_unref();
error:
    free(kernel_name);
    return error;
}

static void
netdev_bsd_destruct(struct netdev *netdev_)
{
    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);

    cache_notifier_unref();

    if (netdev->tap_fd >= 0) {
        destroy_tap(netdev->tap_fd, netdev_get_kernel_name(netdev_));
    }
    if (netdev->pcap) {
        pcap_close(netdev->pcap);
    }
    free(netdev->kernel_name);
    ovs_mutex_destroy(&netdev->mutex);
}

static void
netdev_bsd_dealloc(struct netdev *netdev_)
{
    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);

    free(netdev);
}

static int
netdev_bsd_open_pcap(const char *name, pcap_t **pcapp, int *fdp)
{
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_t *pcap = NULL;
    int one = 1;
    int error;
    int fd;

    /* Open the pcap device.  The device is opened in non-promiscuous mode
     * because the interface flags are manually set by the caller. */
    errbuf[0] = '\0';
    pcap = pcap_open_live(name, PCAP_SNAPLEN, 0, 1000, errbuf);
    if (!pcap) {
        VLOG_ERR_RL(&rl, "%s: pcap_open_live failed: %s", name, errbuf);
        error = EIO;
        goto error;
    }
    if (errbuf[0] != '\0') {
        VLOG_WARN_RL(&rl, "%s: pcap_open_live: %s", name, errbuf);
    }

    /* Get the underlying fd. */
    fd = pcap_get_selectable_fd(pcap);
    if (fd == -1) {
        VLOG_WARN_RL(&rl, "%s: no selectable file descriptor", name);
        error = errno;
        goto error;
    }

    /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
     * on the file descriptor returned by pcap_get_selectable_fd to achieve
     * a real non-blocking behaviour.*/
    error = pcap_setnonblock(pcap, 1, errbuf);
    if (error == -1) {
        error = errno;
        goto error;
    }

    /* This call assure that reads return immediately upon packet
     * reception.  Otherwise, a read will block until either the kernel
     * buffer becomes full or a timeout occurs. */
    if (ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) {
        VLOG_ERR_RL(&rl, "ioctl(BIOCIMMEDIATE) on %s device failed: %s",
                    name, ovs_strerror(errno));
        error = errno;
        goto error;
    }

    /* Capture only incoming packets. */
    error = pcap_setdirection(pcap, PCAP_D_IN);
    if (error == -1) {
        error = errno;
        goto error;
    }

    *pcapp = pcap;
    *fdp = fd;
    return 0;

error:
    if (pcap) {
        pcap_close(pcap);
    }
    *pcapp = NULL;
    *fdp = -1;
    return error;
}

static struct netdev_rxq *
netdev_bsd_rxq_alloc(void)
{
    struct netdev_rxq_bsd *rxq = xzalloc(sizeof *rxq);
    return &rxq->up;
}

static int
netdev_bsd_rxq_construct(struct netdev_rxq *rxq_)
{
    struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
    struct netdev *netdev_ = rxq->up.netdev;
    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
    int error;

    if (!strcmp(netdev_get_type(netdev_), "tap")) {
        rxq->pcap_handle = NULL;
        rxq->fd = netdev->tap_fd;
        error = 0;
    } else {
        ovs_mutex_lock(&netdev->mutex);
        error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_),
                                     &rxq->pcap_handle, &rxq->fd);
        ovs_mutex_unlock(&netdev->mutex);
    }

    return error;
}

static void
netdev_bsd_rxq_destruct(struct netdev_rxq *rxq_)
{
    struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);

    if (rxq->pcap_handle) {
        pcap_close(rxq->pcap_handle);
    }
}

static void
netdev_bsd_rxq_dealloc(struct netdev_rxq *rxq_)
{
    struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);

    free(rxq);
}

/* The recv callback of the netdev class returns the number of bytes of the
 * received packet.
 *
 * This can be done by the pcap_next() function. Unfortunately pcap_next() does
 * not make difference between a missing packet on the capture interface and
 * an error during the file capture.  We can use the pcap_dispatch() function
 * instead, which is able to distinguish between errors and null packet.
 *
 * To make pcap_dispatch() returns the number of bytes read from the interface
 * we need to define the following callback and argument.
 */
struct pcap_arg {
    void *data;
    int size;
    int retval;
};

/*
 * This callback will be executed on every captured packet.
 *
 * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
 * pcap returns a truncated packet and we follow this behavior.
 *
 * The argument args->retval is the packet size in bytes.
 */
static void
proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
{
    struct pcap_arg *args = ALIGNED_CAST(struct pcap_arg *, args_);

    if (args->size < hdr->len) {
        VLOG_WARN_RL(&rl, "packet truncated");
        args->retval = args->size;
    } else {
        args->retval = hdr->len;
    }

    /* copy the packet to our buffer */
    memcpy(args->data, packet, args->retval);
}

/*
 * This function attempts to receive a packet from the specified network
 * device. It is assumed that the network device is a system device or a tap
 * device opened as a system one. In this case the read operation is performed
 * from rxq->pcap.
 */
static int
netdev_rxq_bsd_recv_pcap(struct netdev_rxq_bsd *rxq, struct dp_packet *buffer)
{
    struct pcap_arg arg;
    int ret;

    /* prepare the pcap argument to store the packet */
    arg.size = dp_packet_tailroom(buffer);
    arg.data = dp_packet_data(buffer);

    for (;;) {
        ret = pcap_dispatch(rxq->pcap_handle, 1, proc_pkt, (u_char *) &arg);

        if (ret > 0) {
            dp_packet_set_size(buffer, dp_packet_size(buffer) + arg.retval);
            return 0;
        }
        if (ret == -1) {
            if (errno == EINTR) {
                 continue;
            }
        }

        return EAGAIN;
    }
}

/*
 * This function attempts to receive a packet from the specified network
 * device. It is assumed that the network device is a tap device and
 * 'rxq->fd' is initialized with the tap file descriptor.
 */
static int
netdev_rxq_bsd_recv_tap(struct netdev_rxq_bsd *rxq, struct dp_packet *buffer)
{
    size_t size = dp_packet_tailroom(buffer);

    for (;;) {
        ssize_t retval = read(rxq->fd, dp_packet_data(buffer), size);
        if (retval >= 0) {
            dp_packet_set_size(buffer, dp_packet_size(buffer) + retval);
            return 0;
        } else if (errno != EINTR) {
            if (errno != EAGAIN) {
                VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
                             ovs_strerror(errno), netdev_rxq_get_name(&rxq->up));
            }
            return errno;
        }
    }
}

static int
netdev_bsd_rxq_recv(struct netdev_rxq *rxq_, struct dp_packet_batch *batch,
                    int *qfill)
{
    struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
    struct netdev *netdev = rxq->up.netdev;
    struct dp_packet *packet;
    ssize_t retval;
    int mtu;

    if (netdev_bsd_get_mtu(netdev, &mtu)) {
        mtu = ETH_PAYLOAD_MAX;
    }

    /* Assume Ethernet port. No need to set packet_type. */
    packet = dp_packet_new_with_headroom(VLAN_ETH_HEADER_LEN + mtu,
                                           DP_NETDEV_HEADROOM);
    retval = (rxq->pcap_handle
            ? netdev_rxq_bsd_recv_pcap(rxq, packet)
            : netdev_rxq_bsd_recv_tap(rxq, packet));

    if (retval) {
        dp_packet_delete(packet);
    } else {
        dp_packet_batch_init_packet(batch, packet);
    }

    if (qfill) {
        *qfill = -ENOTSUP;
    }

    return retval;
}

/*
 * Registers with the poll loop to wake up from the next call to poll_block()
 * when a packet is ready to be received with netdev_rxq_recv() on 'rxq'.
 */
static void
netdev_bsd_rxq_wait(struct netdev_rxq *rxq_)
{
    struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);

    poll_fd_wait(rxq->fd, POLLIN);
}

/* Discards all packets waiting to be received from 'rxq'. */
static int
netdev_bsd_rxq_drain(struct netdev_rxq *rxq_)
{
    struct ifreq ifr;
    struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);

    strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rxq_get_netdev(rxq_)));
    if (ioctl(rxq->fd, BIOCFLUSH, &ifr) == -1) {
        VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
                    netdev_rxq_get_name(rxq_), ovs_strerror(errno));
        return errno;
    }
    return 0;
}

/*
 * Send a packet on the specified network device. The device could be either a
 * system or a tap device.
 */
static int
netdev_bsd_send(struct netdev *netdev_, int qid OVS_UNUSED,
                struct dp_packet_batch *batch,
                bool concurrent_txq OVS_UNUSED)
{
    struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
    const char *name = netdev_get_name(netdev_);
    struct dp_packet *packet;
    int error;

    ovs_mutex_lock(&dev->mutex);
    if (dev->tap_fd < 0 && !dev->pcap) {
        error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd);
    } else {
        error = 0;
    }

    DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
        const void *data = dp_packet_data(packet);
        size_t size = dp_packet_size(packet);

        while (!error) {
            ssize_t retval;
            if (dev->tap_fd >= 0) {
                retval = write(dev->tap_fd, data, size);
            } else {
                retval = pcap_inject(dev->pcap, data, size);
            }
            if (retval < 0) {
                if (errno == EINTR) {
                    continue;
                } else {
                    error = errno;
                    if (error != EAGAIN) {
                        VLOG_WARN_RL(&rl, "error sending Ethernet packet on"
                                     " %s: %s", name, ovs_strerror(error));
                    }
                }
            } else if (retval != size) {
                VLOG_WARN_RL(&rl, "sent partial Ethernet packet "
                                  "(%"PRIuSIZE" bytes of "
                                  "%"PRIuSIZE") on %s", retval, size, name);
                error = EMSGSIZE;
            } else {
                break;
            }
        }
    }

    ovs_mutex_unlock(&dev->mutex);
    dp_packet_delete_batch(batch, true);

    return error;
}

/*
 * Registers with the poll loop to wake up from the next call to poll_block()
 * when the packet transmission queue has sufficient room to transmit a packet
 * with netdev_send().
 */
static void
netdev_bsd_send_wait(struct netdev *netdev_, int qid OVS_UNUSED)
{
    struct netdev_bsd *dev = netdev_bsd_cast(netdev_);

    ovs_mutex_lock(&dev->mutex);
    if (dev->tap_fd >= 0) {
        /* TAP device always accepts packets. */
        poll_immediate_wake();
    } else if (dev->pcap) {
        poll_fd_wait(dev->fd, POLLOUT);
    } else {
        /* We haven't even tried to send a packet yet. */
        poll_immediate_wake();
    }
    ovs_mutex_unlock(&dev->mutex);
}

/*
 * Attempts to set 'netdev''s MAC address to 'mac'.  Returns 0 if successful,
 * otherwise a positive errno value.
 */
static int
netdev_bsd_set_etheraddr(struct netdev *netdev_,
                         const struct eth_addr mac)
{
    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
    int error = 0;

    ovs_mutex_lock(&netdev->mutex);
    if (!(netdev->cache_valid & VALID_ETHERADDR)
        || !eth_addr_equals(netdev->etheraddr, mac)) {
        error = set_etheraddr(netdev_get_kernel_name(netdev_), AF_LINK,
                              ETH_ADDR_LEN, mac);
        if (!error) {
            netdev->cache_valid |= VALID_ETHERADDR;
            netdev->etheraddr = mac;
            netdev_change_seq_changed(netdev_);
        }
    }
    ovs_mutex_unlock(&netdev->mutex);

    return error;
}

/*
 * Returns a pointer to 'netdev''s MAC address.  The caller must not modify or
 * free the returned buffer.
 */
static int
netdev_bsd_get_etheraddr(const struct netdev *netdev_, struct eth_addr *mac)
{
    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
    int error = 0;

    ovs_mutex_lock(&netdev->mutex);
    if (!(netdev->cache_valid & VALID_ETHERADDR)) {
        error = get_etheraddr(netdev_get_kernel_name(netdev_),
                              &netdev->etheraddr);
        if (!error) {
            netdev->cache_valid |= VALID_ETHERADDR;
        }
    }
    if (!error) {
        *mac = netdev->etheraddr;
    }
    ovs_mutex_unlock(&netdev->mutex);

    return error;
}

/*
 * Returns the maximum size of transmitted (and received) packets on 'netdev',
 * in bytes, not including the hardware header; thus, this is typically 1500
 * bytes for Ethernet devices.
 */
static int
netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
{
    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
    int error = 0;

    ovs_mutex_lock(&netdev->mutex);
    if (!(netdev->cache_valid & VALID_MTU)) {
        struct ifreq ifr;

        error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
                                    SIOCGIFMTU, "SIOCGIFMTU");
        if (!error) {
            netdev->mtu = ifr.ifr_mtu;
            netdev->cache_valid |= VALID_MTU;
        }
    }
    if (!error) {
        *mtup = netdev->mtu;
    }
    ovs_mutex_unlock(&netdev->mutex);

    return error;
}

static int
netdev_bsd_get_ifindex(const struct netdev *netdev_)
{
    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
    int ifindex, error;

    ovs_mutex_lock(&netdev->mutex);
    error = get_ifindex(netdev_, &ifindex);
    ovs_mutex_unlock(&netdev->mutex);

    return error ? -error : ifindex;
}

static int
netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
{
    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
    int error = 0;

    ovs_mutex_lock(&netdev->mutex);
    if (!(netdev->cache_valid & VALID_CARRIER)) {
        struct ifmediareq ifmr;

        memset(&ifmr, 0, sizeof(ifmr));
        ovs_strlcpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
                    sizeof ifmr.ifm_name);

        error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
        if (!error) {
            netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
            netdev->cache_valid |= VALID_CARRIER;

            /* If the interface doesn't report whether the media is active,
             * just assume it is active. */
            if ((ifmr.ifm_status & IFM_AVALID) == 0) {
                netdev->carrier = true;
            }
        } else {
            VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
                        netdev_get_name(netdev_), ovs_strerror(error));
        }
    }
    if (!error) {
        *carrier = netdev->carrier;
    }
    ovs_mutex_unlock(&netdev->mutex);

    return error;
}

static void
convert_stats_system(struct netdev_stats *stats, const struct if_data *ifd)
{
    /*
     * note: UINT64_MAX means unsupported
     */
    stats->rx_packets = ifd->ifi_ipackets;
    stats->tx_packets = ifd->ifi_opackets;
    stats->rx_bytes = ifd->ifi_obytes;
    stats->tx_bytes = ifd->ifi_ibytes;
    stats->rx_errors = ifd->ifi_ierrors;
    stats->tx_errors = ifd->ifi_oerrors;
    stats->rx_dropped = ifd->ifi_iqdrops;
    stats->tx_dropped = UINT64_MAX;
    stats->multicast = ifd->ifi_imcasts;
    stats->collisions = ifd->ifi_collisions;
    stats->rx_length_errors = UINT64_MAX;
    stats->rx_over_errors = UINT64_MAX;
    stats->rx_crc_errors = UINT64_MAX;
    stats->rx_frame_errors = UINT64_MAX;
    stats->rx_fifo_errors = UINT64_MAX;
    stats->rx_missed_errors = UINT64_MAX;
    stats->tx_aborted_errors = UINT64_MAX;
    stats->tx_carrier_errors = UINT64_MAX;
    stats->tx_fifo_errors = UINT64_MAX;
    stats->tx_heartbeat_errors = UINT64_MAX;
    stats->tx_window_errors = UINT64_MAX;
}

static void
convert_stats_tap(struct netdev_stats *stats, const struct if_data *ifd)
{
    /*
     * Similar to convert_stats_system but swapping rxq and tx
     * because 'ifd' is stats for the network interface side of the
     * tap device and what the caller wants is one for the character
     * device side.
     *
     * note: UINT64_MAX means unsupported
     */
    stats->rx_packets = ifd->ifi_opackets;
    stats->tx_packets = ifd->ifi_ipackets;
    stats->rx_bytes = ifd->ifi_ibytes;
    stats->tx_bytes = ifd->ifi_obytes;
    stats->rx_errors = ifd->ifi_oerrors;
    stats->tx_errors = ifd->ifi_ierrors;
    stats->rx_dropped = UINT64_MAX;
    stats->tx_dropped = ifd->ifi_iqdrops;
    stats->multicast = ifd->ifi_omcasts;
    stats->collisions = UINT64_MAX;
    stats->rx_length_errors = UINT64_MAX;
    stats->rx_over_errors = UINT64_MAX;
    stats->rx_crc_errors = UINT64_MAX;
    stats->rx_frame_errors = UINT64_MAX;
    stats->rx_fifo_errors = UINT64_MAX;
    stats->rx_missed_errors = UINT64_MAX;
    stats->tx_aborted_errors = UINT64_MAX;
    stats->tx_carrier_errors = UINT64_MAX;
    stats->tx_fifo_errors = UINT64_MAX;
    stats->tx_heartbeat_errors = UINT64_MAX;
    stats->tx_window_errors = UINT64_MAX;
}

static void
convert_stats(const struct netdev *netdev, struct netdev_stats *stats,
              const struct if_data *ifd)
{
    if (netdev_bsd_cast(netdev)->tap_fd == -1) {
        convert_stats_system(stats, ifd);
    } else {
        convert_stats_tap(stats, ifd);
    }
}

/* Retrieves current device stats for 'netdev'. */
static int
netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
{
#if defined(__FreeBSD__)
    int if_count, i;
    int mib[6];
    size_t len;
    struct ifmibdata ifmd;


    mib[0] = CTL_NET;
    mib[1] = PF_LINK;
    mib[2] = NETLINK_GENERIC;
    mib[3] = IFMIB_SYSTEM;
    mib[4] = IFMIB_IFCOUNT;

    len = sizeof(if_count);

    if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) {
        VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
                    netdev_get_name(netdev_), ovs_strerror(errno));
        return errno;
    }

    mib[5] = IFDATA_GENERAL;
    mib[3] = IFMIB_IFDATA;
    len = sizeof(ifmd);
    for (i = 1; i <= if_count; i++) {
        mib[4] = i; /* row */
        if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) {
            VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
                        netdev_get_name(netdev_), ovs_strerror(errno));
            return errno;
        } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
            convert_stats(netdev_, stats, &ifmd.ifmd_data);
            break;
        }
    }

    return 0;
#elif defined(__NetBSD__)
    struct ifdatareq ifdr;
    int error;

    memset(&ifdr, 0, sizeof(ifdr));
    ovs_strlcpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_),
                sizeof(ifdr.ifdr_name));
    error = af_link_ioctl(SIOCGIFDATA, &ifdr);
    if (!error) {
        convert_stats(netdev_, stats, &ifdr.ifdr_data);
    }
    return error;
#else
#error not implemented
#endif
}

static uint32_t
netdev_bsd_parse_media(int media)
{
    uint32_t supported = 0;
    bool half_duplex = media & IFM_HDX ? true : false;

    switch (IFM_SUBTYPE(media)) {
    case IFM_10_2:
    case IFM_10_5:
    case IFM_10_STP:
    case IFM_10_T:
        supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
        supported |= NETDEV_F_COPPER;
        break;

    case IFM_10_FL:
        supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
        supported |= NETDEV_F_FIBER;
        break;

    case IFM_100_T2:
    case IFM_100_T4:
    case IFM_100_TX:
    case IFM_100_VG:
        supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
        supported |= NETDEV_F_COPPER;
        break;

    case IFM_100_FX:
        supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
        supported |= NETDEV_F_FIBER;
        break;

    case IFM_1000_CX:
    case IFM_1000_T:
        supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
        supported |= NETDEV_F_COPPER;
        break;

    case IFM_1000_LX:
    case IFM_1000_SX:
        supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
        supported |= NETDEV_F_FIBER;
        break;

    case IFM_10G_CX4:
        supported |= NETDEV_F_10GB_FD;
        supported |= NETDEV_F_COPPER;
        break;

    case IFM_10G_LR:
    case IFM_10G_SR:
        supported |= NETDEV_F_10GB_FD;
        supported |= NETDEV_F_FIBER;
        break;

    default:
        return 0;
    }

    if (IFM_SUBTYPE(media) == IFM_AUTO) {
        supported |= NETDEV_F_AUTONEG;
    }
    /*
    if (media & IFM_ETH_FMASK) {
        supported |= NETDEV_F_PAUSE;
    }
    */

    return supported;
}

/*
 * Stores the features supported by 'netdev' into each of '*current',
 * '*advertised', '*supported', and '*peer' that are non-null.  Each value is a
 * bitmap of "enum ofp_port_features" bits, in host byte order.  Returns 0 if
 * successful, otherwise a positive errno value.  On failure, all of the
 * passed-in values are set to 0.
 */
static int
netdev_bsd_get_features(const struct netdev *netdev,
                        enum netdev_features *current, uint32_t *advertised,
                        enum netdev_features *supported, uint32_t *peer)
{
    struct ifmediareq ifmr;
    int *media_list;
    int i;
    int error;


    /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */

    memset(&ifmr, 0, sizeof(ifmr));
    ovs_strlcpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);

    /* We make two SIOCGIFMEDIA ioctl calls.  The first to determine the
     * number of supported modes, and a second with a buffer to retrieve
     * them. */
    error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
    if (error) {
        VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
                    netdev_get_name(netdev), ovs_strerror(error));
        return error;
    }

    media_list = xcalloc(ifmr.ifm_count, sizeof(int));
    ifmr.ifm_ulist = media_list;

    if (IFM_TYPE(ifmr.ifm_current) != IFM_ETHER) {
        VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet",
                    netdev_get_name(netdev));
        error = EINVAL;
        goto cleanup;
    }

    error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
    if (error) {
        VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
                    netdev_get_name(netdev), ovs_strerror(error));
        goto cleanup;
    }

    /* Current settings. */
    *current = netdev_bsd_parse_media(ifmr.ifm_active);

    /* Advertised features. */
    *advertised = netdev_bsd_parse_media(ifmr.ifm_current);

    /* Supported features. */
    *supported = 0;
    for (i = 0; i < ifmr.ifm_count; i++) {
        *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]);
    }

    /* Peer advertisements. */
    *peer = 0;                  /* XXX */

    error = 0;
cleanup:
    free(media_list);
    return error;
}

/*
 * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask.  If
 * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared.  Returns a
 * positive errno value.
 */
static int
netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
                   struct in_addr mask)
{
    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
    int error;

    ovs_mutex_lock(&netdev->mutex);
    error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
    if (!error) {
        if (addr.s_addr != INADDR_ANY) {
            error = do_set_addr(netdev_, SIOCSIFNETMASK,
                                "SIOCSIFNETMASK", mask);
        }
        netdev_change_seq_changed(netdev_);
    }
    ovs_mutex_unlock(&netdev->mutex);

    return error;
}

static int
netdev_bsd_get_addr_list(const struct netdev *netdev_,
                         struct in6_addr **addr, struct in6_addr **mask, int *n_cnt)
{
    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
    int error;

    if (!(netdev->cache_valid & VALID_IN)) {
        netdev_get_addrs_list_flush();
    }
    error = netdev_get_addrs(netdev_get_name(netdev_), addr, mask, n_cnt);
    if (!error) {
        netdev->cache_valid |= VALID_IN;
    }
    return error;
}

#if defined(__NetBSD__)
static char *
netdev_bsd_kernel_name_to_ovs_name(const char *kernel_name)
{
    char *ovs_name = NULL;
    struct shash device_shash;
    struct shash_node *node;

    shash_init(&device_shash);
    netdev_get_devices(&netdev_tap_class, &device_shash);
    SHASH_FOR_EACH(node, &device_shash) {
        struct netdev *netdev = node->data;
        struct netdev_bsd * const dev = netdev_bsd_cast(netdev);

        if (!strcmp(dev->kernel_name, kernel_name)) {
            free(ovs_name);
            ovs_name = xstrdup(netdev_get_name(&dev->up));
        }
        netdev_close(netdev);
    }
    shash_destroy(&device_shash);

    return ovs_name ? ovs_name : xstrdup(kernel_name);
}
#endif

static int
netdev_bsd_get_next_hop(const struct in_addr *host OVS_UNUSED,
                        struct in_addr *next_hop OVS_UNUSED,
                        char **netdev_name OVS_UNUSED)
{
#if defined(__NetBSD__)
    static int seq = 0;
    struct sockaddr_in sin;
    struct sockaddr_dl sdl;
    int s;
    int i;
    struct {
        struct rt_msghdr h;
        char space[512];
    } buf;
    struct rt_msghdr *rtm = &buf.h;
    const pid_t pid = getpid();
    char *cp;
    ssize_t ssz;
    bool gateway = false;
    char *ifname = NULL;
    int saved_errno;

    memset(next_hop, 0, sizeof(*next_hop));
    *netdev_name = NULL;

    memset(&sin, 0, sizeof(sin));
    sin.sin_len = sizeof(sin);
    sin.sin_family = AF_INET;
    sin.sin_port = 0;
    sin.sin_addr = *host;

    memset(&sdl, 0, sizeof(sdl));
    sdl.sdl_len = sizeof(sdl);
    sdl.sdl_family = AF_LINK;

    s = socket(PF_ROUTE, SOCK_RAW, 0);
    memset(&buf, 0, sizeof(buf));
    rtm->rtm_flags = RTF_HOST|RTF_UP;
    rtm->rtm_version = RTM_VERSION;
    rtm->rtm_addrs = RTA_DST|RTA_IFP;
    cp = (void *)&buf.space;
    memcpy(cp, &sin, sizeof(sin));
    RT_ADVANCE(cp, (struct sockaddr *)(void *)&sin);
    memcpy(cp, &sdl, sizeof(sdl));
    RT_ADVANCE(cp, (struct sockaddr *)(void *)&sdl);
    rtm->rtm_msglen = cp - (char *)(void *)rtm;
    rtm->rtm_seq = ++seq;
    rtm->rtm_type = RTM_GET;
    rtm->rtm_pid = pid;
    write(s, rtm, rtm->rtm_msglen);
    memset(&buf, 0, sizeof(buf));
    do {
        ssz = read(s, &buf, sizeof(buf));
    } while (ssz > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    saved_errno = errno;
    close(s);
    if (ssz <= 0) {
        if (ssz < 0) {
            return saved_errno;
        }
        return EPIPE; /* XXX */
    }
    cp = (void *)&buf.space;
    for (i = 1; i; i <<= 1) {
        if ((rtm->rtm_addrs & i) != 0) {
            const struct sockaddr *sa = (const void *)cp;

            if ((i == RTA_GATEWAY) && sa->sa_family == AF_INET) {
                const struct sockaddr_in * const sin =
                  ALIGNED_CAST(const struct sockaddr_in *, sa);

                *next_hop = sin->sin_addr;
                gateway = true;
            }
            if ((i == RTA_IFP) && sa->sa_family == AF_LINK) {
                const struct sockaddr_dl * const sdl =
                  ALIGNED_CAST(const struct sockaddr_dl *, sa);
                char *kernel_name;

                kernel_name = xmemdup0(sdl->sdl_data, sdl->sdl_nlen);
                ifname = netdev_bsd_kernel_name_to_ovs_name(kernel_name);
                free(kernel_name);
            }
            RT_ADVANCE(cp, sa);
        }
    }
    if (ifname == NULL) {
        return ENXIO;
    }
    if (!gateway) {
        *next_hop = *host;
    }
    *netdev_name = ifname;
    VLOG_DBG("host " IP_FMT " next-hop " IP_FMT " if %s",
      IP_ARGS(host->s_addr), IP_ARGS(next_hop->s_addr), *netdev_name);
    return 0;
#else
    return EOPNOTSUPP;
#endif
}

static int
netdev_bsd_arp_lookup(const struct netdev *netdev OVS_UNUSED,
                      ovs_be32 ip OVS_UNUSED,
                      struct eth_addr *mac OVS_UNUSED)
{
#if defined(__NetBSD__)
    const struct rt_msghdr *rtm;
    size_t needed;
    char *buf;
    const char *cp;
    const char *ep;
    int mib[6];
    int error;

    buf = NULL;
    mib[0] = CTL_NET;
    mib[1] = PF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_INET;
    mib[4] = NET_RT_FLAGS;
#ifdef RTF_LLINFO
    mib[5] = RTF_LLINFO;
#else
    mib[5] = 0;
#endif
    if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) {
        error = errno;
        goto error;
    }
    buf = xmalloc(needed);
    if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) {
        error = errno;
        goto error;
    }
    ep = buf + needed;
    for (cp = buf; cp < ep; cp += rtm->rtm_msglen) {
        const struct sockaddr_inarp *sina;
        const struct sockaddr_dl *sdl;

        rtm = (const void *)cp;
        sina = (const void *)(rtm + 1);
        if (ip != sina->sin_addr.s_addr) {
            continue;
        }
        sdl = (const void *)
           ((const char *)(const void *)sina + RT_ROUNDUP(sina->sin_len));
        if (sdl->sdl_alen == ETH_ADDR_LEN) {
            memcpy(mac, &sdl->sdl_data[sdl->sdl_nlen], ETH_ADDR_LEN);
            error = 0;
            goto error;
        }
    }
    error = ENXIO;
error:
    free(buf);
    return error;
#else
    return EOPNOTSUPP;
#endif
}

static void
make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
{
    struct sockaddr_in sin;
    memset(&sin, 0, sizeof sin);
    sin.sin_family = AF_INET;
    sin.sin_addr = addr;
    sin.sin_port = 0;

    memset(sa, 0, sizeof *sa);
    memcpy(sa, &sin, sizeof sin);
}

static int
do_set_addr(struct netdev *netdev,
            unsigned long ioctl_nr, const char *ioctl_name,
            struct in_addr addr)
{
    struct ifreq ifr;
    make_in4_sockaddr(&ifr.ifr_addr, addr);
    return af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr,
                               ioctl_name);
}

static int
nd_to_iff_flags(enum netdev_flags nd)
{
    int iff = 0;
    if (nd & NETDEV_UP) {
        iff |= IFF_UP;
    }
    if (nd & NETDEV_PROMISC) {
        iff |= IFF_PROMISC;
#if defined(IFF_PPROMISC)
        iff |= IFF_PPROMISC;
#endif
    }
    if (nd & NETDEV_LOOPBACK) {
        iff |= IFF_LOOPBACK;
    }
    return iff;
}

static int
iff_to_nd_flags(int iff)
{
    enum netdev_flags nd = 0;
    if (iff & IFF_UP) {
        nd |= NETDEV_UP;
    }
    if (iff & IFF_PROMISC) {
        nd |= NETDEV_PROMISC;
    }
    if (iff & IFF_LOOPBACK) {
        nd |= NETDEV_LOOPBACK;
    }
    return nd;
}

static int
netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
                        enum netdev_flags on, enum netdev_flags *old_flagsp)
{
    int old_flags, new_flags;
    int error;

    error = get_flags(netdev_, &old_flags);
    if (!error) {
        *old_flagsp = iff_to_nd_flags(old_flags);
        new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
        if (new_flags != old_flags) {
            error = set_flags(netdev_get_kernel_name(netdev_), new_flags);
            netdev_change_seq_changed(netdev_);
        }
    }
    return error;
}

#define NETDEV_BSD_CLASS_COMMON                      \
    .run = netdev_bsd_run,                           \
    .wait = netdev_bsd_wait,                         \
    .alloc = netdev_bsd_alloc,                       \
    .destruct = netdev_bsd_destruct,                 \
    .dealloc = netdev_bsd_dealloc,                   \
    .send = netdev_bsd_send,                         \
    .send_wait = netdev_bsd_send_wait,               \
    .set_etheraddr = netdev_bsd_set_etheraddr,       \
    .get_etheraddr = netdev_bsd_get_etheraddr,       \
    .get_mtu = netdev_bsd_get_mtu,                   \
    .get_ifindex = netdev_bsd_get_ifindex,           \
    .get_carrier = netdev_bsd_get_carrier,           \
    .get_stats = netdev_bsd_get_stats,               \
    .get_features = netdev_bsd_get_features,         \
    .set_in4 = netdev_bsd_set_in4,                   \
    .get_addr_list = netdev_bsd_get_addr_list,       \
    .get_next_hop = netdev_bsd_get_next_hop,         \
    .arp_lookup = netdev_bsd_arp_lookup,             \
    .update_flags = netdev_bsd_update_flags,         \
    .rxq_alloc = netdev_bsd_rxq_alloc,               \
    .rxq_construct = netdev_bsd_rxq_construct,       \
    .rxq_destruct = netdev_bsd_rxq_destruct,         \
    .rxq_dealloc = netdev_bsd_rxq_dealloc,           \
    .rxq_recv = netdev_bsd_rxq_recv,                 \
    .rxq_wait = netdev_bsd_rxq_wait,                 \
    .rxq_drain = netdev_bsd_rxq_drain

const struct netdev_class netdev_bsd_class = {
    NETDEV_BSD_CLASS_COMMON,
    .type = "system",
    .construct = netdev_bsd_construct_system,
};

const struct netdev_class netdev_tap_class = {
    NETDEV_BSD_CLASS_COMMON,
    .type = "tap",
    .construct = netdev_bsd_construct_tap,
};


static void
destroy_tap(int fd, const char *name)
{
    struct ifreq ifr;

    close(fd);
    strcpy(ifr.ifr_name, name);
    /* XXX What to do if this call fails? */
    af_inet_ioctl(SIOCIFDESTROY, &ifr);
}

static int
get_flags(const struct netdev *netdev, int *flags)
{
    struct ifreq ifr;
    int error;

    error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr,
                                SIOCGIFFLAGS, "SIOCGIFFLAGS");

    *flags = ifr_get_flags(&ifr);

    return error;
}

static int
set_flags(const char *name, int flags)
{
    struct ifreq ifr;

    ifr_set_flags(&ifr, flags);

    return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
}

static int
get_ifindex(const struct netdev *netdev_, int *ifindexp)
{
    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
    *ifindexp = 0;
    if (!(netdev->cache_valid & VALID_IFINDEX)) {
        int ifindex = if_nametoindex(netdev_get_name(netdev_));
        if (ifindex <= 0) {
            return errno;
        }
        netdev->cache_valid |= VALID_IFINDEX;
        netdev->ifindex = ifindex;
    }
    *ifindexp = netdev->ifindex;
    return 0;
}

static int
get_etheraddr(const char *netdev_name, struct eth_addr *ea)
{
    struct ifaddrs *head;
    struct ifaddrs *ifa;
    struct sockaddr_dl *sdl;

    if (getifaddrs(&head) != 0) {
        VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
                ovs_strerror(errno));
        return errno;
    }

    for (ifa = head; ifa; ifa = ifa->ifa_next) {
        if (ifa->ifa_addr->sa_family == AF_LINK) {
            if (!strcmp(ifa->ifa_name, netdev_name)) {
                sdl = ALIGNED_CAST(struct sockaddr_dl *, ifa->ifa_addr);
                if (sdl) {
                    memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
                    freeifaddrs(head);
                    return 0;
                }
            }
        }
    }

    VLOG_ERR("could not find ethernet address for %s device", netdev_name);
    freeifaddrs(head);
    return ENODEV;
}

static int
set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED,
              int hwaddr_len OVS_UNUSED,
              const struct eth_addr mac OVS_UNUSED)
{
#if defined(__FreeBSD__)
    struct ifreq ifr;
    int error;

    memset(&ifr, 0, sizeof ifr);
    ovs_strlcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
    ifr.ifr_addr.sa_family = hwaddr_family;
    ifr.ifr_addr.sa_len = hwaddr_len;
    memcpy(ifr.ifr_addr.sa_data, &mac, hwaddr_len);
    error = af_inet_ioctl(SIOCSIFLLADDR, &ifr);
    if (error) {
        VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
                 netdev_name, ovs_strerror(error));
        return error;
    }
    return 0;
#elif defined(__NetBSD__)
    struct if_laddrreq req;
    struct sockaddr_dl *sdl;
    struct sockaddr_storage oldaddr;
    int error;

    /*
     * get the old address, add new one, and then remove old one.
     */

    if (hwaddr_len != ETH_ADDR_LEN) {
        /* just to be safe about sockaddr storage size */
        return EOPNOTSUPP;
    }
    memset(&req, 0, sizeof(req));
    ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
    req.addr.ss_len = sizeof(req.addr);
    req.addr.ss_family = hwaddr_family;
    sdl = (struct sockaddr_dl *)&req.addr;
    sdl->sdl_alen = hwaddr_len;

    error = af_link_ioctl(SIOCGLIFADDR, &req);
    if (error) {
        return error;
    }
    if (!memcmp(&sdl->sdl_data[sdl->sdl_nlen], &mac, hwaddr_len)) {
        return 0;
    }
    oldaddr = req.addr;

    memset(&req, 0, sizeof(req));
    ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
    req.flags = IFLR_ACTIVE;
    sdl = (struct sockaddr_dl *)&req.addr;
    sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data) + hwaddr_len;
    sdl->sdl_alen = hwaddr_len;
    sdl->sdl_family = hwaddr_family;
    memcpy(sdl->sdl_data, &mac, hwaddr_len);
    error = af_link_ioctl(SIOCALIFADDR, &req);
    if (error) {
        return error;
    }

    memset(&req, 0, sizeof(req));
    ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
    req.addr = oldaddr;
    return af_link_ioctl(SIOCDLIFADDR, &req);
#else
#error not implemented
#endif
}

static int
ifr_get_flags(const struct ifreq *ifr)
{
#ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
    return (ifr->ifr_flagshigh << 16) | (ifr->ifr_flags & 0xffff);
#else
    return ifr->ifr_flags;
#endif
}

static void
ifr_set_flags(struct ifreq *ifr, int flags)
{
#ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
    ifr->ifr_flags = flags & 0xffff;
    ifr->ifr_flagshigh = flags >> 16;
#else
    ifr->ifr_flags = flags;
#endif
}

#if defined(__NetBSD__)
/* Calls ioctl() on an AF_LINK sock, passing the specified 'command' and
 * 'arg'.  Returns 0 if successful, otherwise a positive errno value. */
int
af_link_ioctl(unsigned long command, const void *arg)
{
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
    static int sock;

    if (ovsthread_once_start(&once)) {
        sock = socket(AF_LINK, SOCK_DGRAM, 0);
        if (sock < 0) {
            sock = -errno;
            VLOG_ERR("failed to create link socket: %s", ovs_strerror(errno));
        }
        ovsthread_once_done(&once);
    }

    return (sock < 0 ? -sock
            : ioctl(sock, command, arg) == -1 ? errno
            : 0);
}
#endif
#endif /* !defined(__MACH__) */