summaryrefslogtreecommitdiff
path: root/sys/net/if_geneve.c
blob: ab8b313e860a494d389747cc98322eaf1483910f (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
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2025-2026 Pouria Mousavizadeh Tehrani <pouria@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "opt_inet.h"
#include "opt_inet6.h"

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/hash.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/module.h>
#include <sys/refcount.h>
#include <sys/rmlock.h>
#include <sys/priv.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/sdt.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sockio.h>
#include <sys/sx.h>
#include <sys/systm.h>
#include <sys/counter.h>
#include <sys/jail.h>

#include <net/bpf.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_private.h>
#include <net/if_arp.h>
#include <net/if_clone.h>
#include <net/if_media.h>
#include <net/if_types.h>
#include <net/netisr.h>
#include <net/route.h>
#include <net/route/nhop.h>

#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/in_pcb.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet6/in6_var.h>
#include <netinet6/scope6_var.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#include <netinet/in_fib.h>
#include <netinet6/in6_fib.h>
#include <netinet/ip_ecn.h>
#include <net/if_geneve.h>

#include <netlink/netlink.h>
#include <netlink/netlink_ctl.h>
#include <netlink/netlink_var.h>
#include <netlink/netlink_route.h>
#include <netlink/route/route_var.h>

#include <security/mac/mac_framework.h>

SDT_PROVIDER_DEFINE(if_geneve);

struct geneve_softc;
LIST_HEAD(geneve_softc_head, geneve_softc);

static struct sx geneve_sx;
SX_SYSINIT(geneve, &geneve_sx, "GENEVE global start/stop lock");

static unsigned geneve_osd_jail_slot;

union sockaddr_union {
	struct sockaddr		sa;
	struct sockaddr_in	sin;
	struct sockaddr_in6	sin6;
};

struct geneve_socket_mc_info {
	union sockaddr_union	gnvsomc_saddr;
	union sockaddr_union	gnvsomc_gaddr;
	int			gnvsomc_ifidx;
	int			gnvsomc_users;
};

/* The maximum MTU of encapsulated geneve packet. */
#define GENEVE_MAX_L3MTU	(IP_MAXPACKET - \
	    60 /* Maximum IPv4 header len */ - \
	    sizeof(struct udphdr) - \
	    sizeof(struct genevehdr))
#define GENEVE_MAX_MTU		(GENEVE_MAX_L3MTU - \
	    ETHER_HDR_LEN - ETHER_VLAN_ENCAP_LEN)

#define GENEVE_BASIC_IFCAPS (IFCAP_LINKSTATE | IFCAP_JUMBO_MTU | IFCAP_NV)

#define GENEVE_VERSION	0
#define GENEVE_VNI_MASK	(GENEVE_VNI_MAX - 1)

#define GENEVE_HDR_VNI_SHIFT	8

#define GENEVE_SO_MC_MAX_GROUPS		32

#define GENEVE_SO_VNI_HASH_SHIFT	6
#define GENEVE_SO_VNI_HASH_SIZE		(1 << GENEVE_SO_VNI_HASH_SHIFT)
#define GENEVE_SO_VNI_HASH(_vni)	((_vni) % GENEVE_SO_VNI_HASH_SIZE)

struct geneve_socket {
	struct socket			*gnvso_sock;
	struct rmlock			gnvso_lock;
	u_int				gnvso_refcnt;
	union sockaddr_union		gnvso_laddr;
	LIST_ENTRY(geneve_socket)	gnvso_entry;
	struct geneve_softc_head	gnvso_vni_hash[GENEVE_SO_VNI_HASH_SIZE];
	struct geneve_socket_mc_info	gnvso_mc[GENEVE_SO_MC_MAX_GROUPS];
};

#define GENEVE_SO_RLOCK(_gnvso, _p)	rm_rlock(&(_gnvso)->gnvso_lock, (_p))
#define GENEVE_SO_RUNLOCK(_gnvso, _p)	rm_runlock(&(_gnvso)->gnvso_lock, (_p))
#define GENEVE_SO_WLOCK(_gnvso)		rm_wlock(&(_gnvso)->gnvso_lock)
#define GENEVE_SO_WUNLOCK(_gnvso)		rm_wunlock(&(_gnvso)->gnvso_lock)
#define GENEVE_SO_LOCK_ASSERT(_gnvso) \
    rm_assert(&(_gnvso)->gnvso_lock, RA_LOCKED)
#define GENEVE_SO_LOCK_WASSERT(_gnvso) \
    rm_assert(&(_gnvso)->gnvso_lock, RA_WLOCKED)

#define GENEVE_SO_ACQUIRE(_gnvso)		refcount_acquire(&(_gnvso)->gnvso_refcnt)
#define GENEVE_SO_RELEASE(_gnvso)		refcount_release(&(_gnvso)->gnvso_refcnt)

struct gnv_ftable_entry {
	LIST_ENTRY(gnv_ftable_entry)	gnvfe_hash;
	uint16_t			gnvfe_flags;
	uint8_t				gnvfe_mac[ETHER_ADDR_LEN];
	union sockaddr_union		gnvfe_raddr;
	time_t				gnvfe_expire;
};

#define GENEVE_FE_FLAG_DYNAMIC		0x01
#define GENEVE_FE_FLAG_STATIC		0x02

#define GENEVE_FE_IS_DYNAMIC(_fe) \
    ((_fe)->gnvfe_flags & GENEVE_FE_FLAG_DYNAMIC)

#define GENEVE_SC_FTABLE_SHIFT		9
#define GENEVE_SC_FTABLE_SIZE		(1 << GENEVE_SC_FTABLE_SHIFT)
#define GENEVE_SC_FTABLE_MASK		(GENEVE_SC_FTABLE_SIZE - 1)
#define GENEVE_SC_FTABLE_HASH(_sc, _mac)	\
    (geneve_mac_hash(_sc, _mac) % GENEVE_SC_FTABLE_SIZE)

LIST_HEAD(geneve_ftable_head, gnv_ftable_entry);

struct geneve_statistics {
	uint32_t	ftable_nospace;
	uint32_t	ftable_lock_upgrade_failed;
	counter_u64_t	txcsum;
	counter_u64_t	tso;
	counter_u64_t	rxcsum;
};

struct geneve_softc {
	LIST_ENTRY(geneve_softc)	gnv_entry;

	struct ifnet			*gnv_ifp;
	uint32_t			gnv_flags;
#define GENEVE_FLAG_INIT		0x0001
#define GENEVE_FLAG_RUNNING		0x0002
#define GENEVE_FLAG_TEARDOWN		0x0004
#define GENEVE_FLAG_LEARN		0x0008
#define GENEVE_FLAG_USER_MTU		0x0010
#define GENEVE_FLAG_TTL_INHERIT		0x0020
#define GENEVE_FLAG_DSCP_INHERIT	0x0040
#define GENEVE_FLAG_COLLECT_METADATA	0x0080

	int				gnv_reqcap;
	int				gnv_reqcap2;
	struct geneve_socket		*gnv_sock;
	union sockaddr_union		gnv_src_addr;
	union sockaddr_union		gnv_dst_addr;
	uint32_t			gnv_fibnum;
	uint32_t			gnv_vni;
	uint32_t			gnv_port_hash_key;
	uint16_t			gnv_proto;
	uint16_t			gnv_min_port;
	uint16_t			gnv_max_port;
	uint8_t				gnv_ttl;
	enum ifla_geneve_df		gnv_df;

	/* Lookup table from MAC address to forwarding entry. */
	uint32_t			gnv_ftable_cnt;
	uint32_t			gnv_ftable_max;
	uint32_t			gnv_ftable_timeout;
	uint32_t			gnv_ftable_hash_key;
	struct geneve_ftable_head	*gnv_ftable;

	/* Derived from gnv_dst_addr. */
	struct gnv_ftable_entry		gnv_default_fe;

	struct ip_moptions		*gnv_im4o;
	struct ip6_moptions		*gnv_im6o;

	struct rmlock			gnv_lock;
	volatile u_int			gnv_refcnt;

	int				gnv_so_mc_index;
	struct geneve_statistics	gnv_stats;
	struct callout			gnv_callout;
	struct ether_addr		gnv_hwaddr;
	int				gnv_mc_ifindex;
	struct ifnet			*gnv_mc_ifp;
	struct ifmedia			gnv_media;
	char				gnv_mc_ifname[IFNAMSIZ];

	/* For rate limiting errors on the tx fast path. */
	struct timeval			err_time;
	int				err_pps;
};

#define GENEVE_RLOCK(_sc, _p)	rm_rlock(&(_sc)->gnv_lock, (_p))
#define GENEVE_RUNLOCK(_sc, _p)	rm_runlock(&(_sc)->gnv_lock, (_p))
#define GENEVE_WLOCK(_sc)	rm_wlock(&(_sc)->gnv_lock)
#define GENEVE_WUNLOCK(_sc)	rm_wunlock(&(_sc)->gnv_lock)
#define GENEVE_LOCK_WOWNED(_sc)	rm_wowned(&(_sc)->gnv_lock)
#define GENEVE_LOCK_ASSERT(_sc)	rm_assert(&(_sc)->gnv_lock, RA_LOCKED)
#define GENEVE_LOCK_WASSERT(_sc) rm_assert(&(_sc)->gnv_lock, RA_WLOCKED)
#define GENEVE_UNLOCK(_sc, _p) do {		\
    if (GENEVE_LOCK_WOWNED(_sc))		\
	GENEVE_WUNLOCK(_sc);			\
    else					\
	GENEVE_RUNLOCK(_sc, _p);		\
} while (0)

#define GENEVE_ACQUIRE(_sc)	refcount_acquire(&(_sc)->gnv_refcnt)
#define GENEVE_RELEASE(_sc)	refcount_release(&(_sc)->gnv_refcnt)

#define	SATOCONSTSIN(sa)	((const struct sockaddr_in *)(sa))
#define	SATOCONSTSIN6(sa)	((const struct sockaddr_in6 *)(sa))

struct geneve_pkt_info {
	u_int		isr;
	uint16_t	ethertype;
	uint8_t		ecn;
	uint8_t		ttl;
};

struct nl_parsed_geneve {
	/* essential */
	uint32_t			ifla_vni;
	uint16_t			ifla_proto;
	struct sockaddr			*ifla_local;
	struct sockaddr			*ifla_remote;
	uint16_t			ifla_local_port;
	uint16_t			ifla_remote_port;

	/* optional */
	struct ifla_geneve_port_range	ifla_port_range;
	enum ifla_geneve_df		ifla_df;
	uint8_t				ifla_ttl;
	bool				ifla_ttl_inherit;
	bool				ifla_dscp_inherit;
	bool				ifla_external;

	/* l2 specific */
	bool				ifla_ftable_learn;
	bool				ifla_ftable_flush;
	uint32_t			ifla_ftable_max;
	uint32_t			ifla_ftable_timeout;
	uint32_t			ifla_ftable_count;	/* read-only */

	/* multicast specific */
	char				*ifla_mc_ifname;
	uint32_t			ifla_mc_ifindex;	/* read-only */
};

/* The multicast-based learning parts of the code are taken from if_vxlan */
static int	geneve_ftable_addr_cmp(const uint8_t *, const uint8_t *);
static void	geneve_ftable_init(struct geneve_softc *);
static void	geneve_ftable_fini(struct geneve_softc *);
static void	geneve_ftable_flush(struct geneve_softc *, int);
static void	geneve_ftable_expire(struct geneve_softc *);
static int	geneve_ftable_update_locked(struct geneve_softc *,
		    const union sockaddr_union *, const uint8_t *,
		    struct rm_priotracker *);
static int	geneve_ftable_learn(struct geneve_softc *,
		    const struct sockaddr *, const uint8_t *);

static struct gnv_ftable_entry *
		geneve_ftable_entry_alloc(void);
static void	geneve_ftable_entry_free(struct gnv_ftable_entry *);
static void	geneve_ftable_entry_init(struct geneve_softc *,
		    struct gnv_ftable_entry *, const uint8_t *,
		    const struct sockaddr *, uint32_t);
static void	geneve_ftable_entry_destroy(struct geneve_softc *,
		    struct gnv_ftable_entry *);
static int	geneve_ftable_entry_insert(struct geneve_softc *,
		    struct gnv_ftable_entry *);
static struct gnv_ftable_entry *
		geneve_ftable_entry_lookup(struct geneve_softc *,
		    const uint8_t *);

static struct geneve_socket *
		geneve_socket_alloc(union sockaddr_union *laddr);
static void	geneve_socket_destroy(struct geneve_socket *);
static void	geneve_socket_release(struct geneve_socket *);
static struct geneve_socket *
		geneve_socket_lookup(union sockaddr_union *);
static void	geneve_socket_insert(struct geneve_socket *);
static int	geneve_socket_init(struct geneve_socket *, struct ifnet *);
static int	geneve_socket_bind(struct geneve_socket *, struct ifnet *);
static int	geneve_socket_create(struct ifnet *, int,
		    const union sockaddr_union *, struct geneve_socket **);
static int	geneve_socket_set_df(struct geneve_socket *, bool);

static struct geneve_socket *
		geneve_socket_mc_lookup(const union sockaddr_union *);
static int	geneve_sockaddr_mc_info_match(
		    const struct geneve_socket_mc_info *,
		    const union sockaddr_union *,
		    const union sockaddr_union *, int);
static int	geneve_socket_mc_join_group(struct geneve_socket *,
		    const union sockaddr_union *, const union sockaddr_union *,
		    int *, union sockaddr_union *);
static int	geneve_socket_mc_leave_group(struct geneve_socket *,
		    const union sockaddr_union *,
		    const union sockaddr_union *, int);
static int	geneve_socket_mc_add_group(struct geneve_socket *,
		    const union sockaddr_union *,
		    const union sockaddr_union *, int, int *);
static void	geneve_socket_mc_release_group(struct geneve_socket *, int);

static struct geneve_softc *
		geneve_socket_lookup_softc_locked(struct geneve_socket *,
		    uint32_t);
static struct geneve_softc *
		geneve_socket_lookup_softc(struct geneve_socket *, uint32_t);
static int	geneve_socket_insert_softc(struct geneve_socket *,
		    struct geneve_softc *);
static void	geneve_socket_remove_softc(struct geneve_socket *,
		    struct geneve_softc *);

static struct ifnet *
		geneve_multicast_if_ref(struct geneve_softc *, uint32_t);
static void	geneve_free_multicast(struct geneve_softc *);
static int	geneve_setup_multicast_interface(struct geneve_softc *);

static int	geneve_setup_multicast(struct geneve_softc *);
static int	geneve_setup_socket(struct geneve_softc *);
static void	geneve_setup_interface_hdrlen(struct geneve_softc *);
static int	geneve_valid_init_config(struct geneve_softc *);
static void	geneve_init_complete(struct geneve_softc *);
static void	geneve_init(void *);
static void	geneve_release(struct geneve_softc *);
static void	geneve_teardown_wait(struct geneve_softc *);
static void	geneve_teardown_locked(struct geneve_softc *);
static void	geneve_teardown(struct geneve_softc *);
static void	geneve_timer(void *);

static int	geneve_flush_ftable(struct geneve_softc *, bool);
static uint16_t	geneve_get_local_port(struct geneve_softc *);
static uint16_t	geneve_get_remote_port(struct geneve_softc *);

static int	geneve_set_vni_nl(struct geneve_softc *, struct nl_pstate *,
		    uint32_t);
static int	geneve_set_local_addr_nl(struct geneve_softc *, struct nl_pstate *,
		    struct sockaddr *);
static int	geneve_set_remote_addr_nl(struct geneve_softc *, struct nl_pstate *,
		    struct sockaddr *);
static int	geneve_set_local_port_nl(struct geneve_softc *, struct nl_pstate *,
		    uint16_t);
static int	geneve_set_remote_port_nl(struct geneve_softc *, struct nl_pstate *,
		    uint16_t);
static int	geneve_set_port_range_nl(struct geneve_softc *, struct nl_pstate *,
		    struct ifla_geneve_port_range);
static int	geneve_set_df_nl(struct geneve_softc *, struct nl_pstate *,
		    enum ifla_geneve_df);
static int	geneve_set_ttl_nl(struct geneve_softc *, struct nl_pstate *,
		    uint8_t);
static int	geneve_set_ttl_inherit_nl(struct geneve_softc *, struct nl_pstate *,
		    bool);
static int	geneve_set_dscp_inherit_nl(struct geneve_softc *, struct nl_pstate *,
		    bool);
static int	geneve_set_collect_metadata_nl(struct geneve_softc *,
		    struct nl_pstate *, bool);
static int	geneve_set_learn_nl(struct geneve_softc *, struct nl_pstate *,
		    bool);
static int	geneve_set_ftable_max_nl(struct geneve_softc *, struct nl_pstate *,
		    uint32_t);
static int	geneve_set_ftable_timeout_nl(struct geneve_softc *,
		    struct nl_pstate *, uint32_t);
static int	geneve_set_mc_if_nl(struct geneve_softc *, struct nl_pstate *,
		    char *);
static int	geneve_flush_ftable_nl(struct geneve_softc *, struct nl_pstate *,
		    bool);
static void	geneve_get_local_addr_nl(struct geneve_softc *, struct nl_writer *);
static void	geneve_get_remote_addr_nl(struct geneve_softc *, struct nl_writer *);

static int	geneve_ioctl_ifflags(struct geneve_softc *);
static int	geneve_ioctl(struct ifnet *, u_long, caddr_t);

static uint16_t geneve_pick_source_port(struct geneve_softc *, struct mbuf *);
static void	geneve_encap_header(struct geneve_softc *, struct mbuf *,
		    int, uint16_t, uint16_t, uint16_t);
static uint16_t	geneve_get_ethertype(struct mbuf *);
static int	geneve_inherit_l3_hdr(struct mbuf *, struct geneve_softc *,
		    uint16_t, uint8_t *, uint8_t *, u_short *);
#ifdef INET
static int	geneve_encap4(struct geneve_softc *,
		    const union sockaddr_union *, struct mbuf *);
#endif
#ifdef INET6
static int	geneve_encap6(struct geneve_softc *,
		    const union sockaddr_union *, struct mbuf *);
#endif
static int	geneve_transmit(struct ifnet *, struct mbuf *);
static void	geneve_qflush(struct ifnet *);
static int	geneve_output(struct ifnet *, struct mbuf *,
		    const struct sockaddr *, struct route *);
static uint32_t	geneve_map_etype_to_af(uint32_t);
static bool	geneve_udp_input(struct mbuf *, int, struct inpcb *,
		    const struct sockaddr *, void *);
static int	geneve_input_ether(struct geneve_softc *, struct mbuf **,
		    const struct sockaddr *, struct geneve_pkt_info *);
static int	geneve_input_inherit(struct geneve_softc *,
		    struct mbuf **, int, struct geneve_pkt_info *);
static int	geneve_next_option(struct geneve_socket *, struct genevehdr *,
		    struct mbuf **);
static void	geneve_input_csum(struct mbuf *m, struct ifnet *ifp,
		    counter_u64_t rxcsum);

static void	geneve_stats_alloc(struct geneve_softc *);
static void	geneve_stats_free(struct geneve_softc *);
static void	geneve_set_default_config(struct geneve_softc *);
static int	geneve_set_reqcap(struct geneve_softc *, struct ifnet *, int,
		    int);
static void	geneve_set_hwcaps(struct geneve_softc *);
static int	geneve_clone_create(struct if_clone *, char *, size_t,
		    struct ifc_data *, struct ifnet **);
static int	geneve_clone_destroy(struct if_clone *, struct ifnet *,
		    uint32_t);
static int	geneve_clone_create_nl(struct if_clone *, char *, size_t,
		    struct ifc_data_nl *);
static int	geneve_clone_modify_nl(struct ifnet *, struct ifc_data_nl *);
static void	geneve_clone_dump_nl(struct ifnet *, struct nl_writer *);

static uint32_t geneve_mac_hash(struct geneve_softc *, const uint8_t *);
static int	geneve_media_change(struct ifnet *);
static void	geneve_media_status(struct ifnet *, struct ifmediareq *);

static int	geneve_sockaddr_cmp(const union sockaddr_union *,
		    const struct sockaddr *);
static void	geneve_sockaddr_copy(union sockaddr_union *,
		    const struct sockaddr *);
static int	geneve_sockaddr_in_equal(const union sockaddr_union *,
		    const struct sockaddr *);
static void	geneve_sockaddr_in_copy(union sockaddr_union *,
		    const struct sockaddr *);
static int	geneve_sockaddr_supported(const union sockaddr_union *, int);
static int	geneve_sockaddr_in_any(const union sockaddr_union *);

static int	geneve_can_change_config(struct geneve_softc *);
static int	geneve_check_proto(uint16_t);
static int	geneve_check_multicast_addr(const union sockaddr_union *);
static int	geneve_check_sockaddr(const union sockaddr_union *, const int);

static int	geneve_prison_remove(void *, void *);
static void	vnet_geneve_load(void);
static void	vnet_geneve_unload(void);
static void	geneve_module_init(void);
static void	geneve_module_deinit(void);
static int	geneve_modevent(module_t, int, void *);


static const char geneve_name[] = "geneve";
static MALLOC_DEFINE(M_GENEVE, geneve_name,
    "Generic Network Virtualization Encapsulation Interface");
#define MTAG_GENEVE_LOOP	0x93d66dc0 /* geneve mtag */

VNET_DEFINE_STATIC(struct if_clone *, geneve_cloner);
#define	V_geneve_cloner	VNET(geneve_cloner)

static struct mtx geneve_list_mtx;
#define GENEVE_LIST_LOCK()	mtx_lock(&geneve_list_mtx)
#define GENEVE_LIST_UNLOCK()	mtx_unlock(&geneve_list_mtx)

static LIST_HEAD(, geneve_socket) geneve_socket_list = LIST_HEAD_INITIALIZER(geneve_socket_list);

/* Default maximum number of addresses in the forwarding table. */
#define GENEVE_FTABLE_MAX	2000

/* Timeout (in seconds) of addresses learned in the forwarding table. */
#define GENEVE_FTABLE_TIMEOUT	(20 * 60)

/* Maximum timeout (in seconds) of addresses learned in the forwarding table. */
#define GENEVE_FTABLE_MAX_TIMEOUT	(60 * 60 * 24)

/* Number of seconds between pruning attempts of the forwarding table. */
#define GENEVE_FTABLE_PRUNE	(5 * 60)

static int geneve_ftable_prune_period = GENEVE_FTABLE_PRUNE;

#define _OUT(_field)	offsetof(struct nl_parsed_geneve, _field)
static const struct nlattr_parser nla_p_geneve_create[] = {
	{ .type = IFLA_GENEVE_PROTOCOL, .off = _OUT(ifla_proto), .cb = nlattr_get_uint16 },
};
#undef _OUT
NL_DECLARE_ATTR_PARSER(geneve_create_parser, nla_p_geneve_create);

#define _OUT(_field)	offsetof(struct nl_parsed_geneve, _field)
static const struct nlattr_parser nla_p_geneve[] = {
	{ .type = IFLA_GENEVE_ID, .off = _OUT(ifla_vni), .cb = nlattr_get_uint32 },
	{ .type = IFLA_GENEVE_PROTOCOL, .off = _OUT(ifla_proto), .cb = nlattr_get_uint16 },
	{ .type = IFLA_GENEVE_LOCAL, .off = _OUT(ifla_local), .cb = nlattr_get_ip },
	{ .type = IFLA_GENEVE_REMOTE, .off = _OUT(ifla_remote), .cb = nlattr_get_ip },
	{ .type = IFLA_GENEVE_LOCAL_PORT, .off = _OUT(ifla_local_port), .cb = nlattr_get_uint16 },
	{ .type = IFLA_GENEVE_PORT, .off = _OUT(ifla_remote_port), .cb = nlattr_get_uint16 },
	{ .type = IFLA_GENEVE_PORT_RANGE, .off = _OUT(ifla_port_range),
		.arg = (void *)sizeof(struct ifla_geneve_port_range), .cb = nlattr_get_bytes },
	{ .type = IFLA_GENEVE_DF, .off = _OUT(ifla_df), .cb = nlattr_get_uint8 },
	{ .type = IFLA_GENEVE_TTL, .off = _OUT(ifla_ttl), .cb = nlattr_get_uint8 },
	{ .type = IFLA_GENEVE_TTL_INHERIT, .off = _OUT(ifla_ttl_inherit), .cb = nlattr_get_bool },
	{ .type = IFLA_GENEVE_DSCP_INHERIT, .off = _OUT(ifla_dscp_inherit), .cb = nlattr_get_bool },
	{ .type = IFLA_GENEVE_COLLECT_METADATA, .off = _OUT(ifla_external), .cb = nlattr_get_bool },
	{ .type = IFLA_GENEVE_FTABLE_LEARN, .off = _OUT(ifla_ftable_learn), .cb = nlattr_get_bool },
	{ .type = IFLA_GENEVE_FTABLE_FLUSH, .off = _OUT(ifla_ftable_flush), .cb = nlattr_get_bool },
	{ .type = IFLA_GENEVE_FTABLE_MAX, .off = _OUT(ifla_ftable_max), .cb = nlattr_get_uint32 },
	{ .type = IFLA_GENEVE_FTABLE_TIMEOUT, .off = _OUT(ifla_ftable_timeout), .cb = nlattr_get_uint32 },
	{ .type = IFLA_GENEVE_MC_IFNAME, .off = _OUT(ifla_mc_ifname), .cb = nlattr_get_string },
};
#undef _OUT
NL_DECLARE_ATTR_PARSER(geneve_modify_parser, nla_p_geneve);

static const struct nlhdr_parser *all_parsers[] = {
	&geneve_create_parser, &geneve_modify_parser,
};

static int
geneve_ftable_addr_cmp(const uint8_t *a, const uint8_t *b)
{
	int i, d;

	for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++)
		d = (int)a[i] - (int)b[i];

	return (d);
}

static void
geneve_ftable_init(struct geneve_softc *sc)
{
	int i;

	sc->gnv_ftable = malloc(sizeof(struct geneve_ftable_head) *
	    GENEVE_SC_FTABLE_SIZE, M_GENEVE, M_ZERO | M_WAITOK);

	for (i = 0; i < GENEVE_SC_FTABLE_SIZE; i++)
		LIST_INIT(&sc->gnv_ftable[i]);
	sc->gnv_ftable_hash_key = arc4random();
}

static void
geneve_ftable_fini(struct geneve_softc *sc)
{
	int i;

	for (i = 0; i < GENEVE_SC_FTABLE_SIZE; i++) {
		KASSERT(LIST_EMPTY(&sc->gnv_ftable[i]),
		    ("%s: geneve %p ftable[%d] not empty", __func__, sc, i));
	}
	MPASS(sc->gnv_ftable_cnt == 0);

	free(sc->gnv_ftable, M_GENEVE);
	sc->gnv_ftable = NULL;
}

static void
geneve_ftable_flush(struct geneve_softc *sc, int all)
{
	struct gnv_ftable_entry *fe, *tfe;

	for (int i = 0; i < GENEVE_SC_FTABLE_SIZE; i++) {
		LIST_FOREACH_SAFE(fe, &sc->gnv_ftable[i], gnvfe_hash, tfe) {
			if (all || GENEVE_FE_IS_DYNAMIC(fe))
				geneve_ftable_entry_destroy(sc, fe);
		}
	}
}

static void
geneve_ftable_expire(struct geneve_softc *sc)
{
	struct gnv_ftable_entry *fe, *tfe;

	GENEVE_LOCK_WASSERT(sc);

	for (int i = 0; i < GENEVE_SC_FTABLE_SIZE; i++) {
		LIST_FOREACH_SAFE(fe, &sc->gnv_ftable[i], gnvfe_hash, tfe) {
			if (GENEVE_FE_IS_DYNAMIC(fe) &&
			    time_uptime >= fe->gnvfe_expire)
				geneve_ftable_entry_destroy(sc, fe);
		}
	}
}

static int
geneve_ftable_update_locked(struct geneve_softc *sc,
    const union sockaddr_union *unsa, const uint8_t *mac,
    struct rm_priotracker *tracker)
{
	struct gnv_ftable_entry *fe;
	int error;

	GENEVE_LOCK_ASSERT(sc);

again:
	/*
	 * A forwarding entry for this MAC address might already exist. If
	 * so, update it, otherwise create a new one. We may have to upgrade
	 * the lock if we have to change or create an entry.
	 */
	fe = geneve_ftable_entry_lookup(sc, mac);
	if (fe != NULL) {
		fe->gnvfe_expire = time_uptime + sc->gnv_ftable_timeout;

		if (!GENEVE_FE_IS_DYNAMIC(fe) ||
		    geneve_sockaddr_in_equal(&fe->gnvfe_raddr, &unsa->sa))
			return (0);
		if (!GENEVE_LOCK_WOWNED(sc)) {
			GENEVE_RUNLOCK(sc, tracker);
			GENEVE_WLOCK(sc);
			sc->gnv_stats.ftable_lock_upgrade_failed++;
			goto again;
		}
		geneve_sockaddr_in_copy(&fe->gnvfe_raddr, &unsa->sa);
		return (0);
	}

	if (!GENEVE_LOCK_WOWNED(sc)) {
		GENEVE_RUNLOCK(sc, tracker);
		GENEVE_WLOCK(sc);
		sc->gnv_stats.ftable_lock_upgrade_failed++;
		goto again;
	}

	if (sc->gnv_ftable_cnt >= sc->gnv_ftable_max) {
		sc->gnv_stats.ftable_nospace++;
		return (ENOSPC);
	}

	fe = geneve_ftable_entry_alloc();
	if (fe == NULL)
		return (ENOMEM);

	geneve_ftable_entry_init(sc, fe, mac, &unsa->sa, GENEVE_FE_FLAG_DYNAMIC);

	/* The prior lookup failed, so the insert should not. */
	error = geneve_ftable_entry_insert(sc, fe);
	MPASS(error == 0);

	return (error);
}

static int
geneve_ftable_learn(struct geneve_softc *sc, const struct sockaddr *sa,
    const uint8_t *mac)
{
	struct rm_priotracker tracker;
	union sockaddr_union unsa;
	int error;

	/*
	 * The source port may be randomly selected by the remote host, so
	 * use the port of the default destination address.
	 */
	geneve_sockaddr_copy(&unsa, sa);
	unsa.sin.sin_port = sc->gnv_dst_addr.sin.sin_port;

#ifdef INET6
	if (unsa.sa.sa_family == AF_INET6) {
		error = sa6_embedscope(&unsa.sin6, V_ip6_use_defzone);
		if (error)
			return (error);
	}
#endif

	GENEVE_RLOCK(sc, &tracker);
	error = geneve_ftable_update_locked(sc, &unsa, mac, &tracker);
	GENEVE_UNLOCK(sc, &tracker);

	return (error);
}

static struct gnv_ftable_entry *
geneve_ftable_entry_alloc(void)
{
	struct gnv_ftable_entry *fe;

	fe = malloc(sizeof(*fe), M_GENEVE, M_ZERO | M_NOWAIT);

	return (fe);
}

static void
geneve_ftable_entry_free(struct gnv_ftable_entry *fe)
{

	free(fe, M_GENEVE);
}

static void
geneve_ftable_entry_init(struct geneve_softc *sc, struct gnv_ftable_entry *fe,
    const uint8_t *mac, const struct sockaddr *sa, uint32_t flags)
{

	fe->gnvfe_flags = flags;
	fe->gnvfe_expire = time_uptime + sc->gnv_ftable_timeout;
	memcpy(fe->gnvfe_mac, mac, ETHER_ADDR_LEN);
	geneve_sockaddr_copy(&fe->gnvfe_raddr, sa);
}

static void
geneve_ftable_entry_destroy(struct geneve_softc *sc,
    struct gnv_ftable_entry *fe)
{

	sc->gnv_ftable_cnt--;
	LIST_REMOVE(fe, gnvfe_hash);
	geneve_ftable_entry_free(fe);
}

static int
geneve_ftable_entry_insert(struct geneve_softc *sc,
    struct gnv_ftable_entry *fe)
{
	struct gnv_ftable_entry *lfe;
	uint32_t hash;
	int dir;

	GENEVE_LOCK_WASSERT(sc);
	hash = GENEVE_SC_FTABLE_HASH(sc, fe->gnvfe_mac);

	lfe = LIST_FIRST(&sc->gnv_ftable[hash]);
	if (lfe == NULL) {
		LIST_INSERT_HEAD(&sc->gnv_ftable[hash], fe, gnvfe_hash);
		goto out;
	}

	do {
		dir = geneve_ftable_addr_cmp(fe->gnvfe_mac, lfe->gnvfe_mac);
		if (dir == 0)
			return (EEXIST);
		if (dir > 0) {
			LIST_INSERT_BEFORE(lfe, fe, gnvfe_hash);
			goto out;
		} else if (LIST_NEXT(lfe, gnvfe_hash) == NULL) {
			LIST_INSERT_AFTER(lfe, fe, gnvfe_hash);
			goto out;
		} else
			lfe = LIST_NEXT(lfe, gnvfe_hash);
	} while (lfe != NULL);

out:
	sc->gnv_ftable_cnt++;

	return (0);
}

static struct gnv_ftable_entry *
geneve_ftable_entry_lookup(struct geneve_softc *sc, const uint8_t *mac)
{
	struct gnv_ftable_entry *fe;
	uint32_t hash;
	int dir;

	GENEVE_LOCK_ASSERT(sc);

	hash = GENEVE_SC_FTABLE_HASH(sc, mac);
	LIST_FOREACH(fe, &sc->gnv_ftable[hash], gnvfe_hash) {
		dir = geneve_ftable_addr_cmp(mac, fe->gnvfe_mac);
		if (dir == 0)
			return (fe);
		if (dir > 0)
			break;
	}

	return (NULL);
}

static struct geneve_socket *
geneve_socket_alloc(union sockaddr_union *laddr)
{
	struct geneve_socket *gnvso;

	gnvso = malloc(sizeof(*gnvso), M_GENEVE, M_WAITOK | M_ZERO);
	rm_init(&gnvso->gnvso_lock, "genevesorm");
	refcount_init(&gnvso->gnvso_refcnt, 0);
	for (int i = 0; i < GENEVE_SO_VNI_HASH_SIZE; i++)
		LIST_INIT(&gnvso->gnvso_vni_hash[i]);
	gnvso->gnvso_laddr = *laddr;

	return (gnvso);
}

static void
geneve_socket_destroy(struct geneve_socket *gnvso)
{
	struct socket *so;

	so = gnvso->gnvso_sock;
	if (so != NULL) {
		gnvso->gnvso_sock = NULL;
		soclose(so);
	}

	rm_destroy(&gnvso->gnvso_lock);
	free(gnvso, M_GENEVE);
}

static void
geneve_socket_release(struct geneve_socket *gnvso)
{
	int destroy;

	GENEVE_LIST_LOCK();
	destroy = GENEVE_SO_RELEASE(gnvso);
	if (destroy != 0)
		LIST_REMOVE(gnvso, gnvso_entry);
	GENEVE_LIST_UNLOCK();

	if (destroy != 0)
		geneve_socket_destroy(gnvso);
}

static struct geneve_socket *
geneve_socket_lookup(union sockaddr_union *unsa)
{
	struct geneve_socket *gnvso;

	GENEVE_LIST_LOCK();
	LIST_FOREACH(gnvso, &geneve_socket_list, gnvso_entry) {
		if (geneve_sockaddr_cmp(&gnvso->gnvso_laddr, &unsa->sa) == 0) {
			GENEVE_SO_ACQUIRE(gnvso);
			break;
		}
	}
	GENEVE_LIST_UNLOCK();

	return (gnvso);
}

static void
geneve_socket_insert(struct geneve_socket *gnvso)
{

	GENEVE_LIST_LOCK();
	GENEVE_SO_ACQUIRE(gnvso);
	LIST_INSERT_HEAD(&geneve_socket_list, gnvso, gnvso_entry);
	GENEVE_LIST_UNLOCK();
}

static int
geneve_socket_init(struct geneve_socket *gnvso, struct ifnet *ifp)
{
	struct thread *td;
	int error;

	td = curthread;
	error = socreate(gnvso->gnvso_laddr.sa.sa_family, &gnvso->gnvso_sock,
	    SOCK_DGRAM, IPPROTO_UDP, td->td_ucred, td);
	if (error) {
		if_printf(ifp, "cannot create socket: %d\n", error);
		return (error);
	}

	/*
	 * XXX: If Geneve traffic is shared with other UDP listeners on
	 * the same IP address, tunnel endpoints SHOULD implement a mechanism
	 * to ensure ICMP return traffic arising from network errors is
	 * directed to the correct listener. Unfortunately,
	 * udp_set_kernel_tunneling does not handle icmp errors from transit
	 * devices other than specified source.
	 */
	error = udp_set_kernel_tunneling(gnvso->gnvso_sock,
	    geneve_udp_input, NULL, gnvso);
	if (error)
		if_printf(ifp, "cannot set tunneling function: %d\n", error);

	return (error);
}

static int
geneve_socket_bind(struct geneve_socket *gnvso, struct ifnet *ifp)
{
	union sockaddr_union laddr;
	int error;

	laddr = gnvso->gnvso_laddr;
	error = sobind(gnvso->gnvso_sock, &laddr.sa, curthread);
	if (error)
		return (error);

	return (0);
}

static int
geneve_socket_create(struct ifnet *ifp, int multicast,
    const union sockaddr_union *unsa, struct geneve_socket **xgnvso)
{
	union sockaddr_union laddr;
	struct geneve_socket *gnvso;
	int error;

	laddr = *unsa;

	/*
	 * If this socket will be multicast, then only the local port
	 * must be specified when binding.
	 */
	if (multicast != 0) {
		switch (laddr.sa.sa_family) {
#ifdef INET
		case AF_INET:
			laddr.sin.sin_addr.s_addr = INADDR_ANY;
			break;
#endif
#ifdef INET6
		case AF_INET6:
			laddr.sin6.sin6_addr = in6addr_any;
			break;
#endif
		default:
			return (EAFNOSUPPORT);
		}
	}
	gnvso = geneve_socket_alloc(&laddr);
	if (gnvso == NULL)
		return (ENOMEM);

	error = geneve_socket_init(gnvso, ifp);
	if (error)
		goto fail;

	error = geneve_socket_bind(gnvso, ifp);
	if (error)
		goto fail;

	/*
	 * There is a small window between the bind completing and
	 * inserting the socket, so that a concurrent create may fail.
	 * Let's not worry about that for now.
	 */
	if_printf(ifp, "new geneve socket inserted to socket list\n");
	geneve_socket_insert(gnvso);
	*xgnvso = gnvso;

	return (0);

fail:
	if_printf(ifp, "can't create new socket (error: %d)\n", error);
	geneve_socket_destroy(gnvso);

	return (error);
}

static struct geneve_socket *
geneve_socket_mc_lookup(const union sockaddr_union *unsa)
{
	union sockaddr_union laddr;

	laddr = *unsa;

	switch (laddr.sa.sa_family) {
#ifdef INET
	case AF_INET:
		laddr.sin.sin_addr.s_addr = INADDR_ANY;
		break;
#endif
#ifdef INET6
	case AF_INET6:
		laddr.sin6.sin6_addr = in6addr_any;
		break;
#endif
	default:
		return (NULL);
	}

	return (geneve_socket_lookup(&laddr));
}

static int
geneve_sockaddr_mc_info_match(const struct geneve_socket_mc_info *mc,
    const union sockaddr_union *group, const union sockaddr_union *local,
    int ifidx)
{

	if (!geneve_sockaddr_in_any(local) &&
	    !geneve_sockaddr_in_equal(&mc->gnvsomc_saddr, &local->sa))
		return (0);
	if (!geneve_sockaddr_in_equal(&mc->gnvsomc_gaddr, &group->sa))
		return (0);
	if (ifidx != 0 && ifidx != mc->gnvsomc_ifidx)
		return (0);

	return (1);
}

static int
geneve_socket_mc_join_group(struct geneve_socket *gnvso,
    const union sockaddr_union *group, const union sockaddr_union *local,
    int *ifidx, union sockaddr_union *source)
{
	struct sockopt sopt;
	int error;

	*source = *local;

	if (group->sa.sa_family == AF_INET) {
		struct ip_mreq mreq;

		mreq.imr_multiaddr = group->sin.sin_addr;
		mreq.imr_interface = local->sin.sin_addr;

		memset(&sopt, 0, sizeof(sopt));
		sopt.sopt_dir = SOPT_SET;
		sopt.sopt_level = IPPROTO_IP;
		sopt.sopt_name = IP_ADD_MEMBERSHIP;
		sopt.sopt_val = &mreq;
		sopt.sopt_valsize = sizeof(mreq);
		error = sosetopt(gnvso->gnvso_sock, &sopt);
		if (error)
			return (error);

		/*
		 * BMV: Ideally, there would be a formal way for us to get
		 * the local interface that was selected based on the
		 * imr_interface address. We could then update *ifidx so
		 * geneve_sockaddr_mc_info_match() would return a match for
		 * later creates that explicitly set the multicast interface.
		 *
		 * If we really need to, we can of course look in the INP's
		 * membership list:
		 *     sotoinpcb(gnvso->gnvso_sock)->inp_moptions->
		 *         imo_head[]->imf_inm->inm_ifp
		 * similarly to imo_match_group().
		 */
		source->sin.sin_addr = local->sin.sin_addr;

	} else if (group->sa.sa_family == AF_INET6) {
		struct ipv6_mreq mreq;

		mreq.ipv6mr_multiaddr = group->sin6.sin6_addr;
		mreq.ipv6mr_interface = *ifidx;

		memset(&sopt, 0, sizeof(sopt));
		sopt.sopt_dir = SOPT_SET;
		sopt.sopt_level = IPPROTO_IPV6;
		sopt.sopt_name = IPV6_JOIN_GROUP;
		sopt.sopt_val = &mreq;
		sopt.sopt_valsize = sizeof(mreq);
		error = sosetopt(gnvso->gnvso_sock, &sopt);

		/*
		 * BMV: As with IPv4, we would really like to know what
		 * interface in6p_lookup_mcast_ifp() selected.
		 */
	} else
		error = EAFNOSUPPORT;

	return (error);
}

static int
geneve_socket_mc_leave_group(struct geneve_socket *gnvso,
    const union sockaddr_union *group, const union sockaddr_union *source,
    int ifidx)
{
	struct sockopt sopt;
	int error;

	memset(&sopt, 0, sizeof(sopt));
	sopt.sopt_dir = SOPT_SET;

	if (group->sa.sa_family == AF_INET) {
		struct ip_mreq mreq;

		mreq.imr_multiaddr = group->sin.sin_addr;
		mreq.imr_interface = source->sin.sin_addr;

		sopt.sopt_level = IPPROTO_IP;
		sopt.sopt_name = IP_DROP_MEMBERSHIP;
		sopt.sopt_val = &mreq;
		sopt.sopt_valsize = sizeof(mreq);
		error = sosetopt(gnvso->gnvso_sock, &sopt);
	} else if (group->sa.sa_family == AF_INET6) {
		struct ipv6_mreq mreq;

		mreq.ipv6mr_multiaddr = group->sin6.sin6_addr;
		mreq.ipv6mr_interface = ifidx;

		sopt.sopt_level = IPPROTO_IPV6;
		sopt.sopt_name = IPV6_LEAVE_GROUP;
		sopt.sopt_val = &mreq;
		sopt.sopt_valsize = sizeof(mreq);
		error = sosetopt(gnvso->gnvso_sock, &sopt);
	} else
		error = EAFNOSUPPORT;

	return (error);
}

static int
geneve_socket_mc_add_group(struct geneve_socket *gnvso,
    const union sockaddr_union *group, const union sockaddr_union *local,
    int ifidx, int *idx)
{
	union sockaddr_union source;
	struct geneve_socket_mc_info *mc;
	int i, empty, error;

	/*
	 * Within a socket, the same multicast group may be used by multiple
	 * interfaces, each with a different network identifier. But a socket
	 * may only join a multicast group once, so keep track of the users
	 * here.
	 */

	GENEVE_SO_WLOCK(gnvso);
	for (empty = 0, i = 0; i < GENEVE_SO_MC_MAX_GROUPS; i++) {
		mc = &gnvso->gnvso_mc[i];

		if (mc->gnvsomc_gaddr.sa.sa_family == AF_UNSPEC) {
			empty++;
			continue;
		}
		if (geneve_sockaddr_mc_info_match(mc, group, local, ifidx))
			goto out;
	}
	GENEVE_SO_WUNLOCK(gnvso);

	if (empty == 0)
		return (ENOSPC);

	error = geneve_socket_mc_join_group(gnvso, group, local, &ifidx, &source);
	if (error)
		return (error);

	GENEVE_SO_WLOCK(gnvso);
	for (i = 0; i < GENEVE_SO_MC_MAX_GROUPS; i++) {
		mc = &gnvso->gnvso_mc[i];

		if (mc->gnvsomc_gaddr.sa.sa_family == AF_UNSPEC) {
			geneve_sockaddr_copy(&mc->gnvsomc_gaddr, &group->sa);
			geneve_sockaddr_copy(&mc->gnvsomc_saddr, &source.sa);
			mc->gnvsomc_ifidx = ifidx;
			goto out;
		}
	}
	GENEVE_SO_WUNLOCK(gnvso);

	error = geneve_socket_mc_leave_group(gnvso, group, &source, ifidx);
	MPASS(error == 0);

	return (ENOSPC);

out:
	mc->gnvsomc_users++;
	GENEVE_SO_WUNLOCK(gnvso);
	*idx = i;

	return (0);
}

static void
geneve_socket_mc_release_group(struct geneve_socket *vso, int idx)
{
	union sockaddr_union group, source;
	struct geneve_socket_mc_info *mc;
	int ifidx, leave;

	KASSERT(idx >= 0 && idx < GENEVE_SO_MC_MAX_GROUPS,
	    ("%s: vso %p idx %d out of bounds", __func__, vso, idx));

	leave = 0;
	mc = &vso->gnvso_mc[idx];

	GENEVE_SO_WLOCK(vso);
	mc->gnvsomc_users--;
	if (mc->gnvsomc_users == 0) {
		group = mc->gnvsomc_gaddr;
		source = mc->gnvsomc_saddr;
		ifidx = mc->gnvsomc_ifidx;
		memset(mc, 0, sizeof(*mc));
		leave = 1;
	}
	GENEVE_SO_WUNLOCK(vso);

	if (leave != 0) {
		/*
		 * Our socket's membership in this group may have already
		 * been removed if we joined through an interface that's
		 * been detached.
		 */
		geneve_socket_mc_leave_group(vso, &group, &source, ifidx);
	}
}

static struct geneve_softc *
geneve_socket_lookup_softc_locked(struct geneve_socket *gnvso, uint32_t vni)
{
	struct geneve_softc *sc;
	uint32_t hash;

	GENEVE_SO_LOCK_ASSERT(gnvso);
	hash = GENEVE_SO_VNI_HASH(vni);

	LIST_FOREACH(sc, &gnvso->gnvso_vni_hash[hash], gnv_entry) {
		if (sc->gnv_vni == vni) {
			GENEVE_ACQUIRE(sc);
			break;
		}
	}

	return (sc);
}

static struct geneve_softc *
geneve_socket_lookup_softc(struct geneve_socket *gnvso, uint32_t vni)
{
	struct rm_priotracker tracker;
	struct geneve_softc *sc;

	GENEVE_SO_RLOCK(gnvso, &tracker);
	sc = geneve_socket_lookup_softc_locked(gnvso, vni);
	GENEVE_SO_RUNLOCK(gnvso, &tracker);

	return (sc);
}

static int
geneve_socket_insert_softc(struct geneve_socket *gnvso, struct geneve_softc *sc)
{
	struct geneve_softc *tsc;
	uint32_t vni, hash;

	vni = sc->gnv_vni;
	hash = GENEVE_SO_VNI_HASH(vni);

	GENEVE_SO_WLOCK(gnvso);
	tsc = geneve_socket_lookup_softc_locked(gnvso, vni);
	if (tsc != NULL) {
		GENEVE_SO_WUNLOCK(gnvso);
		geneve_release(tsc);
		return (EEXIST);
	}

	GENEVE_ACQUIRE(sc);
	LIST_INSERT_HEAD(&gnvso->gnvso_vni_hash[hash], sc, gnv_entry);
	GENEVE_SO_WUNLOCK(gnvso);

	return (0);
}

static void
geneve_socket_remove_softc(struct geneve_socket *gnvso, struct geneve_softc *sc)
{

	GENEVE_SO_WLOCK(gnvso);
	LIST_REMOVE(sc, gnv_entry);
	GENEVE_SO_WUNLOCK(gnvso);

	geneve_release(sc);
}

static struct ifnet *
geneve_multicast_if_ref(struct geneve_softc *sc, uint32_t af)
{
	struct ifnet *ifp;

	GENEVE_LOCK_ASSERT(sc);

	ifp = NULL;
	if (af == AF_INET && sc->gnv_im4o != NULL)
		ifp = sc->gnv_im4o->imo_multicast_ifp;
	else if (af == AF_INET6 && sc->gnv_im6o != NULL)
		ifp = sc->gnv_im6o->im6o_multicast_ifp;

	if (ifp != NULL)
		if_ref(ifp);

	return (ifp);
}

static void
geneve_free_multicast(struct geneve_softc *sc)
{

	if (sc->gnv_mc_ifp != NULL) {
		if_rele(sc->gnv_mc_ifp);
		sc->gnv_mc_ifp = NULL;
		sc->gnv_mc_ifindex = 0;
	}

	if (sc->gnv_im4o != NULL) {
		free(sc->gnv_im4o, M_GENEVE);
		sc->gnv_im4o = NULL;
	}

	if (sc->gnv_im6o != NULL) {
		free(sc->gnv_im6o, M_GENEVE);
		sc->gnv_im6o = NULL;
	}
}

static int
geneve_setup_multicast_interface(struct geneve_softc *sc)
{
	struct ifnet *ifp;

	ifp = ifunit_ref(sc->gnv_mc_ifname);
	if (ifp == NULL) {
		if_printf(sc->gnv_ifp, "multicast interface %s does not exist\n",
		    sc->gnv_mc_ifname);
		return (ENOENT);
	}

	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
		if_printf(sc->gnv_ifp, "interface %s does not support multicast\n",
		    sc->gnv_mc_ifname);
		if_rele(ifp);
		return (ENOTSUP);
	}

	sc->gnv_mc_ifp = ifp;
	sc->gnv_mc_ifindex = ifp->if_index;

	return (0);
}

static int
geneve_setup_multicast(struct geneve_softc *sc)
{
	const union sockaddr_union *group;
	int error;

	group = &sc->gnv_dst_addr;
	error = 0;

	if (sc->gnv_mc_ifname[0] != '\0') {
		error = geneve_setup_multicast_interface(sc);
		if (error)
			return (error);
	}

	/*
	 * Initialize an multicast options structure that is sufficiently
	 * populated for use in the respective IP output routine. This
	 * structure is typically stored in the socket, but our sockets
	 * may be shared among multiple interfaces.
	 */
	if (group->sa.sa_family == AF_INET) {
		sc->gnv_im4o = malloc(sizeof(struct ip_moptions), M_GENEVE,
		    M_ZERO | M_WAITOK);
		sc->gnv_im4o->imo_multicast_ifp = sc->gnv_mc_ifp;
		sc->gnv_im4o->imo_multicast_ttl = sc->gnv_ttl;
		sc->gnv_im4o->imo_multicast_vif = -1;
	} else if (group->sa.sa_family == AF_INET6) {
		sc->gnv_im6o = malloc(sizeof(struct ip6_moptions), M_GENEVE,
		    M_ZERO | M_WAITOK);
		sc->gnv_im6o->im6o_multicast_ifp = sc->gnv_mc_ifp;
		sc->gnv_im6o->im6o_multicast_hlim = sc->gnv_ttl;
	}

	return (error);
}

static int
geneve_setup_socket(struct geneve_softc *sc)
{
	struct geneve_socket *gnvso;
	struct ifnet *ifp;
	union sockaddr_union *saddr, *daddr;
	int multicast, error;

	gnvso = NULL;
	ifp = sc->gnv_ifp;
	saddr = &sc->gnv_src_addr;
	daddr = &sc->gnv_dst_addr;
	multicast = geneve_check_multicast_addr(daddr);
	MPASS(multicast != EINVAL);
	sc->gnv_so_mc_index = -1;

	/* Try to create the socket. If that fails, attempt to use an existing one. */
	error = geneve_socket_create(ifp, multicast, saddr, &gnvso);
	if (error) {
		if (multicast != 0)
			gnvso = geneve_socket_mc_lookup(saddr);
		else
			gnvso = geneve_socket_lookup(saddr);

		if (gnvso == NULL) {
			if_printf(ifp, "can't find existing socket\n");
			goto out;
		}
	}

	if (sc->gnv_df == IFLA_GENEVE_DF_SET) {
		error = geneve_socket_set_df(gnvso, true);
		if (error)
			goto out;
	}

	if (multicast != 0) {
		error = geneve_setup_multicast(sc);
		if (error)
			goto out;

		error = geneve_socket_mc_add_group(gnvso, daddr, saddr,
		    sc->gnv_mc_ifindex, &sc->gnv_so_mc_index);
		if (error)
			goto out;
	}

	sc->gnv_sock = gnvso;
	error = geneve_socket_insert_softc(gnvso, sc);
	if (error) {
		sc->gnv_sock = NULL;
		if_printf(ifp, "network identifier %d already exists\n", sc->gnv_vni);
		goto out;
	}

	return (0);

out:
	if (gnvso != NULL) {
		if (sc->gnv_so_mc_index != -1) {
			geneve_socket_mc_release_group(gnvso, sc->gnv_so_mc_index);
			sc->gnv_so_mc_index = -1;
		}
		if (multicast != 0)
			geneve_free_multicast(sc);
		geneve_socket_release(gnvso);
	}

	return (error);
}

static void
geneve_setup_interface_hdrlen(struct geneve_softc *sc)
{
	struct ifnet *ifp;

	GENEVE_LOCK_WASSERT(sc);

	ifp = sc->gnv_ifp;
	ifp->if_hdrlen = ETHER_HDR_LEN + sizeof(struct geneveudphdr);
	if (sc->gnv_proto == GENEVE_PROTO_ETHER)
		ifp->if_hdrlen += ETHER_HDR_LEN;

	if (sc->gnv_dst_addr.sa.sa_family == AF_INET)
		ifp->if_hdrlen += sizeof(struct ip);
	else
		ifp->if_hdrlen += sizeof(struct ip6_hdr);

	if ((sc->gnv_flags & GENEVE_FLAG_USER_MTU) == 0)
		ifp->if_mtu = ETHERMTU - ifp->if_hdrlen;
}

static int
geneve_socket_set_df(struct geneve_socket *gnvso, bool df)
{
	struct sockopt sopt;
	int optval;

	memset(&sopt, 0, sizeof(sopt));
	sopt.sopt_dir = SOPT_SET;

	switch (gnvso->gnvso_laddr.sa.sa_family) {
	case AF_INET:
		sopt.sopt_level = IPPROTO_IP;
		sopt.sopt_name = IP_DONTFRAG;
		break;

	case AF_INET6:
		sopt.sopt_level = IPPROTO_IPV6;
		sopt.sopt_name = IPV6_DONTFRAG;
		break;

	default:
		return (EAFNOSUPPORT);
	}

	optval = df ? 1 : 0;
	sopt.sopt_val = &optval;
	sopt.sopt_valsize = sizeof(optval);

	return (sosetopt(gnvso->gnvso_sock, &sopt));
}

static int
geneve_valid_init_config(struct geneve_softc *sc)
{
	const char *reason;

	if (sc->gnv_vni >= GENEVE_VNI_MAX) {
		if_printf(sc->gnv_ifp, "%u", sc->gnv_vni);
		reason = "invalid virtual network identifier specified";
		goto fail;
	}

	if (geneve_sockaddr_supported(&sc->gnv_src_addr, 1) == 0) {
		reason = "source address type is not supported";
		goto fail;
	}

	if (geneve_sockaddr_supported(&sc->gnv_dst_addr, 0) == 0) {
		reason = "destination address type is not supported";
		goto fail;
	}

	if (geneve_sockaddr_in_any(&sc->gnv_dst_addr) != 0) {
		reason = "no valid destination address specified";
		goto fail;
	}

	if (geneve_check_multicast_addr(&sc->gnv_dst_addr) == 0 &&
	    sc->gnv_mc_ifname[0] != '\0') {
		reason = "can only specify interface with a group address";
		goto fail;
	}

	if (geneve_sockaddr_in_any(&sc->gnv_src_addr) == 0) {
		if (&sc->gnv_src_addr.sa.sa_family ==
		    &sc->gnv_dst_addr.sa.sa_family) {
			reason = "source and destination address must both be either IPv4 or IPv6";
			goto fail;
		}
	}

	if (sc->gnv_src_addr.sin.sin_port == 0) {
		reason = "local port not specified";
		goto fail;
	}

	if (sc->gnv_dst_addr.sin.sin_port == 0) {
		reason = "remote port not specified";
		goto fail;
	}

	return (0);

fail:
	if_printf(sc->gnv_ifp, "cannot initialize interface: %s\n", reason);
	return (EINVAL);
}

static void
geneve_init_complete(struct geneve_softc *sc)
{

	GENEVE_WLOCK(sc);
	sc->gnv_flags |= GENEVE_FLAG_RUNNING;
	sc->gnv_flags &= ~GENEVE_FLAG_INIT;
	wakeup(sc);
	GENEVE_WUNLOCK(sc);
}

static void
geneve_init(void *xsc)
{
	static const uint8_t empty_mac[ETHER_ADDR_LEN];
	struct geneve_softc *sc;
	struct ifnet *ifp;

	sc = xsc;
	sx_xlock(&geneve_sx);
	GENEVE_WLOCK(sc);
	ifp = sc->gnv_ifp;
	if (sc->gnv_flags & GENEVE_FLAG_RUNNING) {
		GENEVE_WUNLOCK(sc);
		sx_xunlock(&geneve_sx);
		return;
	}
	sc->gnv_flags |= GENEVE_FLAG_INIT;
	GENEVE_WUNLOCK(sc);

	if (geneve_valid_init_config(sc) != 0)
		goto out;

	if (geneve_setup_socket(sc) != 0)
		goto out;

	/* Initialize the default forwarding entry. */
	if (sc->gnv_proto == GENEVE_PROTO_ETHER) {
		geneve_ftable_entry_init(sc, &sc->gnv_default_fe, empty_mac,
		    &sc->gnv_dst_addr.sa, GENEVE_FE_FLAG_STATIC);

		GENEVE_WLOCK(sc);
		callout_reset(&sc->gnv_callout, geneve_ftable_prune_period * hz,
		    geneve_timer, sc);
		GENEVE_WUNLOCK(sc);
	}
	ifp->if_drv_flags |= IFF_DRV_RUNNING;
	if_link_state_change(ifp, LINK_STATE_UP);

out:
	geneve_init_complete(sc);
	sx_xunlock(&geneve_sx);
}

static void
geneve_release(struct geneve_softc *sc)
{

	/*
	 * The softc may be destroyed as soon as we release our reference,
	 * so we cannot serialize the wakeup with the softc lock. We use a
	 * timeout in our sleeps so a missed wakeup is unfortunate but not fatal.
	 */
	if (GENEVE_RELEASE(sc) != 0)
		wakeup(sc);
}

static void
geneve_teardown_wait(struct geneve_softc *sc)
{

	GENEVE_LOCK_WASSERT(sc);
	while (sc->gnv_flags & GENEVE_FLAG_TEARDOWN)
		rm_sleep(sc, &sc->gnv_lock, 0, "gnvtrn", hz);
}

static void
geneve_teardown_locked(struct geneve_softc *sc)
{
	struct ifnet *ifp;
	struct geneve_socket *gnvso;

	sx_assert(&geneve_sx, SA_XLOCKED);
	GENEVE_LOCK_WASSERT(sc);
	MPASS(sc->gnv_flags & GENEVE_FLAG_TEARDOWN);

	ifp = sc->gnv_ifp;
	ifp->if_flags &= ~IFF_UP;
	sc->gnv_flags &= ~GENEVE_FLAG_RUNNING;

	if (sc->gnv_proto == GENEVE_PROTO_ETHER)
		callout_stop(&sc->gnv_callout);
	gnvso = sc->gnv_sock;
	sc->gnv_sock = NULL;

	GENEVE_WUNLOCK(sc);
	if_link_state_change(ifp, LINK_STATE_DOWN);

	if (gnvso != NULL) {
		geneve_socket_remove_softc(gnvso, sc);

		if (sc->gnv_so_mc_index != -1) {
			geneve_socket_mc_release_group(gnvso, sc->gnv_so_mc_index);
			sc->gnv_so_mc_index = -1;
		}
	}

	GENEVE_WLOCK(sc);
	while (sc->gnv_refcnt != 0)
		rm_sleep(sc, &sc->gnv_lock, 0, "gnvdrn", hz);
	GENEVE_WUNLOCK(sc);

	if (sc->gnv_proto == GENEVE_PROTO_ETHER)
		callout_drain(&sc->gnv_callout);

	geneve_free_multicast(sc);
	if (gnvso != NULL)
		geneve_socket_release(gnvso);

	GENEVE_WLOCK(sc);
	sc->gnv_flags &= ~GENEVE_FLAG_TEARDOWN;
	wakeup(sc);
	GENEVE_WUNLOCK(sc);
}

static void
geneve_teardown(struct geneve_softc *sc)
{

	sx_xlock(&geneve_sx);
	GENEVE_WLOCK(sc);
	if (sc->gnv_flags & GENEVE_FLAG_TEARDOWN) {
		geneve_teardown_wait(sc);
		GENEVE_WUNLOCK(sc);
		sx_xunlock(&geneve_sx);
		return;
	}

	sc->gnv_flags |= GENEVE_FLAG_TEARDOWN;
	geneve_teardown_locked(sc);
	sx_xunlock(&geneve_sx);
}

static void
geneve_timer(void *xsc)
{
	struct geneve_softc *sc;

	sc = xsc;
	GENEVE_LOCK_WASSERT(sc);

	geneve_ftable_expire(sc);
	callout_schedule(&sc->gnv_callout, geneve_ftable_prune_period * hz);
}

static int
geneve_ioctl_ifflags(struct geneve_softc *sc)
{
	struct ifnet *ifp;

	ifp = sc->gnv_ifp;

	if ((ifp->if_flags & IFF_UP) != 0) {
		if ((sc->gnv_flags & GENEVE_FLAG_RUNNING) == 0)
			geneve_init(sc);
	} else {
		if (sc->gnv_flags & GENEVE_FLAG_RUNNING)
			geneve_teardown(sc);
	}

	return (0);
}

static int
geneve_flush_ftable(struct geneve_softc *sc, bool flush)
{

	GENEVE_WLOCK(sc);
	geneve_ftable_flush(sc, flush);
	GENEVE_WUNLOCK(sc);

	return (0);
}

static uint16_t
geneve_get_local_port(struct geneve_softc *sc)
{
	uint16_t port = 0;

	GENEVE_LOCK_ASSERT(sc);

	switch (sc->gnv_src_addr.sa.sa_family) {
	case AF_INET:
		port = ntohs(sc->gnv_src_addr.sin.sin_port);
		break;
	case AF_INET6:
		port = ntohs(sc->gnv_src_addr.sin6.sin6_port);
		break;
	}

	return (port);
}

static uint16_t
geneve_get_remote_port(struct geneve_softc *sc)
{
	uint16_t port = 0;

	GENEVE_LOCK_ASSERT(sc);

	switch (sc->gnv_dst_addr.sa.sa_family) {
	case AF_INET:
		port = ntohs(sc->gnv_dst_addr.sin.sin_port);
		break;
	case AF_INET6:
		port = ntohs(sc->gnv_dst_addr.sin6.sin6_port);
		break;
	}

	return (port);
}

/* Netlink Helpers */
static int
geneve_set_vni_nl(struct geneve_softc *sc, struct nl_pstate *npt, uint32_t vni)
{
	int error;

	error = 0;
	if (vni >= GENEVE_VNI_MAX) {
		error = EINVAL;
		goto ret;
	}

	GENEVE_WLOCK(sc);
	if (geneve_can_change_config(sc))
		sc->gnv_vni = vni;
	else
		error = EBUSY;
	GENEVE_WUNLOCK(sc);

ret:
	if (error == EINVAL)
		nlmsg_report_err_msg(npt, "geneve vni is invalid: %u", vni);

	if (error == EBUSY)
		nlmsg_report_err_msg(npt, "geneve interface is busy.");

	return (error);
}

static int
geneve_set_local_addr_nl(struct geneve_softc *sc, struct nl_pstate *npt,
    struct sockaddr *sa)
{
	union sockaddr_union *unsa = (union sockaddr_union *)sa;
	int error;

	error = geneve_check_sockaddr(unsa, sa->sa_len);
	if (error != 0)
		goto ret;

	error = geneve_check_multicast_addr(unsa);
	if (error != 0)
		goto ret;

#ifdef INET6
	if (unsa->sa.sa_family == AF_INET6) {
		error = sa6_embedscope(&unsa->sin6, V_ip6_use_defzone);
		if (error != 0)
			goto ret;
	}
#endif

	GENEVE_WLOCK(sc);
	if (geneve_can_change_config(sc)) {
		geneve_sockaddr_in_copy(&sc->gnv_src_addr, &unsa->sa);
		geneve_set_hwcaps(sc);
	} else
		error = EBUSY;
	GENEVE_WUNLOCK(sc);

ret:
	if (error == EINVAL)
		nlmsg_report_err_msg(npt, "local address is invalid.");

	if (error == EAFNOSUPPORT)
		nlmsg_report_err_msg(npt, "address family is not supported.");

	if (error == EBUSY)
		nlmsg_report_err_msg(npt, "geneve interface is busy.");

	return (error);
}

static int
geneve_set_remote_addr_nl(struct geneve_softc *sc, struct nl_pstate *npt,
    struct sockaddr *sa)
{
	union sockaddr_union *unsa = (union sockaddr_union *)sa;
	int error;

	error = geneve_check_sockaddr(unsa, sa->sa_len);
	if (error != 0)
		goto ret;

#ifdef INET6
	if (unsa->sa.sa_family == AF_INET6) {
		error = sa6_embedscope(&unsa->sin6, V_ip6_use_defzone);
		if (error != 0)
			goto ret;
	}
#endif

	GENEVE_WLOCK(sc);
	if (geneve_can_change_config(sc)) {
		geneve_sockaddr_in_copy(&sc->gnv_dst_addr, &unsa->sa);
		geneve_setup_interface_hdrlen(sc);
	} else
		error = EBUSY;
	GENEVE_WUNLOCK(sc);

ret:
	if (error == EINVAL)
		nlmsg_report_err_msg(npt, "remote address is invalid.");

	if (error == EAFNOSUPPORT)
		nlmsg_report_err_msg(npt, "address family is not supported.");

	if (error == EBUSY)
		nlmsg_report_err_msg(npt, "geneve interface is busy.");

	return (error);
}

static int
geneve_set_local_port_nl(struct geneve_softc *sc, struct nl_pstate *npt, uint16_t port)
{
	int error;

	error = 0;
	if (port == 0 || port > UINT16_MAX) {
		error = EINVAL;
		goto ret;
	}

	GENEVE_WLOCK(sc);
	if (geneve_can_change_config(sc) == 0) {
		GENEVE_WUNLOCK(sc);
		error = EBUSY;
		goto ret;
	}

	switch (sc->gnv_src_addr.sa.sa_family) {
	case AF_INET:
		sc->gnv_src_addr.sin.sin_port = htons(port);
		break;
	case AF_INET6:
		sc->gnv_src_addr.sin6.sin6_port = htons(port);
		break;
	}
	GENEVE_WUNLOCK(sc);

ret:
	if (error == EINVAL)
		nlmsg_report_err_msg(npt, "local port is invalid: %u", port);

	if (error == EBUSY)
		nlmsg_report_err_msg(npt, "geneve interface is busy.");

	return (error);
}

static int
geneve_set_remote_port_nl(struct geneve_softc *sc, struct nl_pstate *npt, uint16_t port)
{
	int error;

	error = 0;
	if (port == 0 || port > UINT16_MAX) {
		error = EINVAL;
		goto ret;
	}

	GENEVE_WLOCK(sc);
	if (geneve_can_change_config(sc) == 0) {
		GENEVE_WUNLOCK(sc);
		error = EBUSY;
		goto ret;
	}

	switch (sc->gnv_dst_addr.sa.sa_family) {
	case AF_INET:
		sc->gnv_dst_addr.sin.sin_port = htons(port);
		break;
	case AF_INET6:
		sc->gnv_dst_addr.sin6.sin6_port = htons(port);
		break;
	}
	GENEVE_WUNLOCK(sc);

ret:
	if (error == EINVAL)
		nlmsg_report_err_msg(npt, "remote port is invalid: %u", port);

	if (error == EBUSY)
		nlmsg_report_err_msg(npt, "geneve interface is busy.");

	return (error);
}

static int
geneve_set_port_range_nl(struct geneve_softc *sc, struct nl_pstate *npt,
    struct ifla_geneve_port_range port_range)
{
	int error;

	error = 0;
	if (port_range.low <= 0 || port_range.high > UINT16_MAX ||
	    port_range.high < port_range.low) {
		error = EINVAL;
		goto ret;
	}

	GENEVE_WLOCK(sc);
	if (geneve_can_change_config(sc)) {
		sc->gnv_min_port = port_range.low;
		sc->gnv_max_port = port_range.high;
	} else
		error = EBUSY;
	GENEVE_WUNLOCK(sc);

ret:
	if (error == EINVAL)
		nlmsg_report_err_msg(npt, "port range is invalid: %u-%u",
		    port_range.low, port_range.high);

	if (error == EBUSY)
		nlmsg_report_err_msg(npt, "geneve interface is busy.");

	return (error);
}

static int
geneve_set_df_nl(struct geneve_softc *sc, struct nl_pstate *npt,
    enum ifla_geneve_df df)
{
	int error;

	error = 0;
	GENEVE_WLOCK(sc);
	if (geneve_can_change_config(sc))
		sc->gnv_df = df;
	else
		error = EBUSY;
	GENEVE_WUNLOCK(sc);

	if (error == EBUSY)
		nlmsg_report_err_msg(npt, "geneve interface is busy.");

	return (error);
}

static int
geneve_set_ttl_nl(struct geneve_softc *sc, struct nl_pstate *npt __unused,
    uint8_t ttl)
{

	GENEVE_WLOCK(sc);
	sc->gnv_ttl = ttl;
	if (sc->gnv_im4o != NULL)
		sc->gnv_im4o->imo_multicast_ttl = sc->gnv_ttl;
	if (sc->gnv_im6o != NULL)
		sc->gnv_im6o->im6o_multicast_hlim = sc->gnv_ttl;
	GENEVE_WUNLOCK(sc);

	return (0);
}

static int
geneve_set_ttl_inherit_nl(struct geneve_softc *sc,
    struct nl_pstate *npt __unused, bool inherit)
{

	GENEVE_WLOCK(sc);
	if (inherit)
		sc->gnv_flags |= GENEVE_FLAG_TTL_INHERIT;
	else
		sc->gnv_flags &= ~GENEVE_FLAG_TTL_INHERIT;
	GENEVE_WUNLOCK(sc);

	return (0);
}

static int
geneve_set_dscp_inherit_nl(struct geneve_softc *sc,
    struct nl_pstate *npt __unused, bool inherit)
{

	GENEVE_WLOCK(sc);
	if (inherit)
		sc->gnv_flags |= GENEVE_FLAG_DSCP_INHERIT;
	else
		sc->gnv_flags &= ~GENEVE_FLAG_DSCP_INHERIT;
	GENEVE_WUNLOCK(sc);

	return (0);
}

static int
geneve_set_collect_metadata_nl(struct geneve_softc *sc,
    struct nl_pstate *npt __unused, bool external)
{

	GENEVE_WLOCK(sc);
	if (external)
		sc->gnv_flags |= GENEVE_FLAG_COLLECT_METADATA;
	else
		sc->gnv_flags &= ~GENEVE_FLAG_COLLECT_METADATA;
	GENEVE_WUNLOCK(sc);

	return (0);
}

static int
geneve_set_learn_nl(struct geneve_softc *sc, struct nl_pstate *npt,
    bool learn)
{

	GENEVE_WLOCK(sc);
	if (learn)
		sc->gnv_flags |= GENEVE_FLAG_LEARN;
	else
		sc->gnv_flags &= ~GENEVE_FLAG_LEARN;
	GENEVE_WUNLOCK(sc);

	return (0);
}

static int
geneve_set_ftable_max_nl(struct geneve_softc *sc, struct nl_pstate *npt,
    uint32_t max)
{
	int error;

	error = 0;
	GENEVE_WLOCK(sc);
	if (max <= GENEVE_FTABLE_MAX)
		sc->gnv_ftable_max = max;
	else
		error = EINVAL;
	GENEVE_WUNLOCK(sc);

	if (error == EINVAL)
		nlmsg_report_err_msg(npt,
		    "maximum number of entries in the table can not be more than %u",
		    GENEVE_FTABLE_MAX);

	return (error);
}

static int
geneve_set_ftable_timeout_nl(struct geneve_softc *sc, struct nl_pstate *npt,
    uint32_t timeout)
{
	int error;

	error = 0;
	GENEVE_WLOCK(sc);
	if (timeout <= GENEVE_FTABLE_MAX_TIMEOUT)
		sc->gnv_ftable_timeout = timeout;
	else
		error = EINVAL;
	GENEVE_WUNLOCK(sc);

	if (error == EINVAL)
		nlmsg_report_err_msg(npt,
		    "maximum timeout for stale entries in the table can not be more than %u",
		    GENEVE_FTABLE_MAX_TIMEOUT);

	return (error);
}

static int
geneve_set_mc_if_nl(struct geneve_softc *sc, struct nl_pstate *npt,
    char *ifname)
{
	int error;

	error = 0;
	GENEVE_WLOCK(sc);
	if (geneve_can_change_config(sc)) {
		strlcpy(sc->gnv_mc_ifname, ifname, IFNAMSIZ);
		geneve_set_hwcaps(sc);
	} else
		error = EBUSY;
	GENEVE_WUNLOCK(sc);

	if (error == EBUSY)
		nlmsg_report_err_msg(npt, "geneve interface is busy.");

	return (error);
}

static int
geneve_flush_ftable_nl(struct geneve_softc *sc, struct nl_pstate *npt,
    bool flush)
{

	return (geneve_flush_ftable(sc, flush));
}

static void
geneve_get_local_addr_nl(struct geneve_softc *sc, struct nl_writer *nw)
{
	struct sockaddr *sa;

	GENEVE_LOCK_ASSERT(sc);

	sa = &sc->gnv_src_addr.sa;
	if (sa->sa_family == AF_INET) {
		const struct in_addr *in4 = &SATOCONSTSIN(sa)->sin_addr;
		nlattr_add_in_addr(nw, IFLA_GENEVE_LOCAL, in4);
	} else if (sa->sa_family == AF_INET6) {
		const struct in6_addr *in6 = &SATOCONSTSIN6(sa)->sin6_addr;
		nlattr_add_in6_addr(nw, IFLA_GENEVE_LOCAL, in6);
	}
}

static void
geneve_get_remote_addr_nl(struct geneve_softc *sc, struct nl_writer *nw)
{
	struct sockaddr *sa;

	GENEVE_LOCK_ASSERT(sc);

	sa = &sc->gnv_dst_addr.sa;
	if (sa->sa_family == AF_INET) {
		const struct in_addr *in4 = &SATOCONSTSIN(sa)->sin_addr;
		nlattr_add_in_addr(nw, IFLA_GENEVE_REMOTE, in4);
	} else if (sa->sa_family == AF_INET6) {
		const struct in6_addr *in6 = &SATOCONSTSIN6(sa)->sin6_addr;
		nlattr_add_in6_addr(nw, IFLA_GENEVE_REMOTE, in6);
	}
}

static int
geneve_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
{
	struct rm_priotracker tracker;
	struct geneve_softc *sc;
	struct siocsifcapnv_driver_data *drv_ioctl_data, drv_ioctl_data_d;
	struct ifreq *ifr;
	int max, error;

	CURVNET_ASSERT_SET();

	error = 0;
	sc = ifp->if_softc;
	ifr = (struct ifreq *)data;

	switch (cmd) {
	case SIOCADDMULTI:
	case SIOCDELMULTI:
		break;

	case SIOCGDRVSPEC:
		break;
	case SIOCSDRVSPEC:
		error = priv_check(curthread, PRIV_NET_GENEVE);
		if (error)
			return (error);
		break;
	}

	switch (cmd) {
	case SIOCSIFFLAGS:
		error = geneve_ioctl_ifflags(sc);
		break;

	case SIOCSIFMEDIA:
	case SIOCGIFMEDIA:
		if (sc->gnv_proto == GENEVE_PROTO_ETHER)
			error = ifmedia_ioctl(ifp, ifr, &sc->gnv_media, cmd);
		else
			error = EINVAL;
		break;

	case SIOCSIFMTU:
		if (sc->gnv_proto == GENEVE_PROTO_ETHER)
			max = GENEVE_MAX_MTU;
		else
			max = GENEVE_MAX_L3MTU;

		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > max)
			error = EINVAL;
		else {
			GENEVE_WLOCK(sc);
			ifp->if_mtu = ifr->ifr_mtu;
			sc->gnv_flags |= GENEVE_FLAG_USER_MTU;
			GENEVE_WUNLOCK(sc);
		}
		break;

	case SIOCGIFCAPNV:
		break;
	case SIOCSIFCAP:
		drv_ioctl_data = &drv_ioctl_data_d;
		drv_ioctl_data->reqcap = ifr->ifr_reqcap;
		drv_ioctl_data->reqcap2 = if_getcapenable2(ifp);
		drv_ioctl_data->nvcap = NULL;
		/* FALLTHROUGH */
	case SIOCSIFCAPNV:
		if (cmd == SIOCSIFCAPNV)
			drv_ioctl_data = (struct siocsifcapnv_driver_data *)data;

		GENEVE_WLOCK(sc);
		error = geneve_set_reqcap(sc, ifp, drv_ioctl_data->reqcap,
		    drv_ioctl_data->reqcap2);
		if (error == 0)
			geneve_set_hwcaps(sc);
		GENEVE_WUNLOCK(sc);
		break;

	case SIOCGTUNFIB:
		GENEVE_RLOCK(sc, &tracker);
		ifr->ifr_fib = sc->gnv_fibnum;
		GENEVE_RUNLOCK(sc, &tracker);
		break;

	case SIOCSTUNFIB:
		if ((error = priv_check(curthread, PRIV_NET_GENEVE)) != 0)
			break;

		if (ifr->ifr_fib >= rt_numfibs)
			error = EINVAL;
		else {
			GENEVE_WLOCK(sc);
			sc->gnv_fibnum = ifr->ifr_fib;
			GENEVE_WUNLOCK(sc);
		}
		break;

	case SIOCSIFADDR:
		ifp->if_flags |= IFF_UP;
		/* FALLTHROUGH */
	case SIOCGIFADDR:
		if (sc->gnv_proto == GENEVE_PROTO_ETHER)
			error = ether_ioctl(ifp, cmd, data);
		break;

	default:
		if (sc->gnv_proto == GENEVE_PROTO_ETHER)
			error = ether_ioctl(ifp, cmd, data);
		else
			error = EINVAL;
		break;
	}

	return (error);
}

static uint16_t
geneve_pick_source_port(struct geneve_softc *sc, struct mbuf *m)
{
	int range;
	uint32_t hash;

	range = sc->gnv_max_port - sc->gnv_min_port + 1;

	/* RFC 8926 Section 3.3-2.2.1 */
	if (M_HASHTYPE_ISHASH(m))
		hash = m->m_pkthdr.flowid;
	else
		hash = jenkins_hash(m->m_data, ETHER_HDR_LEN, sc->gnv_port_hash_key);

	return (sc->gnv_min_port + (hash % range));
}

static void
geneve_encap_header(struct geneve_softc *sc, struct mbuf *m, int ipoff,
    uint16_t srcport, uint16_t dstport, uint16_t proto)
{
	struct geneveudphdr *hdr;
	struct udphdr *udph;
	struct genevehdr *gnvh;
	int len;

	len = m->m_pkthdr.len - ipoff;
	MPASS(len >= sizeof(struct geneveudphdr));
	hdr = mtodo(m, ipoff);

	udph = &hdr->geneve_udp;
	udph->uh_sport = srcport;
	udph->uh_dport = dstport;
	udph->uh_ulen = htons(len);
	udph->uh_sum = 0;

	gnvh = &hdr->geneve_hdr;
	gnvh->geneve_ver = 0;
	gnvh->geneve_optlen = 0;
	gnvh->geneve_critical = 0;
	gnvh->geneve_control = 0;
	gnvh->geneve_flags = 0;
	gnvh->geneve_proto = proto;
	gnvh->geneve_vni = htonl(sc->gnv_vni << GENEVE_HDR_VNI_SHIFT);
}

/* Return the CSUM_INNER_* equivalent of CSUM_* caps. */
static uint32_t
csum_flags_to_inner_flags(uint32_t csum_flags_in, const uint32_t encap)
{
	uint32_t csum_flags = encap;
	const uint32_t v4 = CSUM_IP | CSUM_IP_UDP | CSUM_IP_TCP;

	/*
	 * csum_flags can request either v4 or v6 offload but not both.
	 * tcp_output always sets CSUM_TSO (both CSUM_IP_TSO and CSUM_IP6_TSO)
	 * so those bits are no good to detect the IP version.  Other bits are
	 * always set with CSUM_TSO and we use those to figure out the IP
	 * version.
	 */
	if (csum_flags_in & v4) {
		if (csum_flags_in & CSUM_IP)
			csum_flags |= CSUM_INNER_IP;
		if (csum_flags_in & CSUM_IP_UDP)
			csum_flags |= CSUM_INNER_IP_UDP;
		if (csum_flags_in & CSUM_IP_TCP)
			csum_flags |= CSUM_INNER_IP_TCP;
		if (csum_flags_in & CSUM_IP_TSO)
			csum_flags |= CSUM_INNER_IP_TSO;
	} else {
#ifdef INVARIANTS
		const uint32_t v6 = CSUM_IP6_UDP | CSUM_IP6_TCP;
		MPASS((csum_flags_in & v6) != 0);
#endif
		if (csum_flags_in & CSUM_IP6_UDP)
			csum_flags |= CSUM_INNER_IP6_UDP;
		if (csum_flags_in & CSUM_IP6_TCP)
			csum_flags |= CSUM_INNER_IP6_TCP;
		if (csum_flags_in & CSUM_IP6_TSO)
			csum_flags |= CSUM_INNER_IP6_TSO;
	}

	return (csum_flags);
}

static uint16_t
geneve_get_ethertype(struct mbuf *m)
{
	struct ip *ip;
	struct ip6_hdr *ip6;

	/*
	 * We should pullup, but we're only interested in the first byte, so
	 * that'll always be contiguous.
	 */
	ip = mtod(m, struct ip *);
	if (ip->ip_v == IPVERSION)
		return (ETHERTYPE_IP);

	ip6 = mtod(m, struct ip6_hdr *);
	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION)
		return (ETHERTYPE_IPV6);

	return (0);
}

/* RFC 8926 Section 4.4.2. DSCP, ECN, and TTL */
static int
geneve_inherit_l3_hdr(struct mbuf *m, struct geneve_softc *sc, uint16_t proto,
    uint8_t *tos, uint8_t *ttl, u_short *ip_off)
{
	struct ether_header *eh;
	struct ip *ip_inner, iphdr;
	struct ip6_hdr *ip6_inner, ip6hdr;
	int offset;

	*tos = 0;
	*ttl = sc->gnv_ttl;
	if (sc->gnv_df == IFLA_GENEVE_DF_SET)
		*ip_off = htons(IP_DF);
	else
		*ip_off = 0;

	/* Set offset and address family if proto is ethernet */
	if (proto == GENEVE_PROTO_ETHER) {
		eh = mtod(m, struct ether_header *);
		if (eh->ether_type == htons(ETHERTYPE_IP)) {
			if (m->m_pkthdr.len < ETHER_HDR_LEN + sizeof(struct ip)) {
				m_freem(m);
				return (EINVAL);
			}
			proto = ETHERTYPE_IP;
		} else if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
			if (m->m_pkthdr.len < ETHER_HDR_LEN + sizeof(struct ip6_hdr)) {
				m_freem(m);
				return (EINVAL);
			}
			proto = ETHERTYPE_IPV6;
		} else
			return (0);

		offset = ETHER_HDR_LEN;
	} else
		offset = 0;

	switch (proto) {
	case ETHERTYPE_IP:
		if (__predict_false(m->m_len < offset + sizeof(struct ip))) {
			m_copydata(m, offset, sizeof(struct ip), (caddr_t)&iphdr);
			ip_inner = &iphdr;
		} else
			ip_inner = mtodo(m, offset);

		*tos = ip_inner->ip_tos;
		if (sc->gnv_flags & GENEVE_FLAG_TTL_INHERIT)
			*ttl = ip_inner->ip_ttl;
		if (sc->gnv_df == IFLA_GENEVE_DF_INHERIT)
			*ip_off = ip_inner->ip_off;
		break;

	case ETHERTYPE_IPV6:
		if (__predict_false(m->m_len < offset + sizeof(struct ip6_hdr))) {
			m_copydata(m, offset, sizeof(struct ip6_hdr), (caddr_t)&ip6hdr);
			ip6_inner = &ip6hdr;
		} else
			ip6_inner = mtodo(m, offset);

		*tos = IPV6_TRAFFIC_CLASS(ip6_inner);
		if (sc->gnv_flags & GENEVE_FLAG_TTL_INHERIT)
			*ttl = ip6_inner->ip6_hlim;
		break;
	}

	return (0);
}

#ifdef INET
static int
geneve_encap4(struct geneve_softc *sc, const union sockaddr_union *funsa,
    struct mbuf *m)
{
	struct ifnet *ifp;
	struct ip *ip;
	struct in_addr srcaddr, dstaddr;
	struct route route, *ro;
	struct sockaddr_in *sin;
	int plen, error;
	uint32_t csum_flags;
	uint16_t srcport, dstport, proto;
	u_short ip_off;
	uint8_t tos, ecn, ttl;
	bool mcast;

	NET_EPOCH_ASSERT();

	ifp = sc->gnv_ifp;
	srcaddr = sc->gnv_src_addr.sin.sin_addr;
	srcport = htons(geneve_pick_source_port(sc, m));
	dstaddr = funsa->sin.sin_addr;
	dstport = funsa->sin.sin_port;
	plen = m->m_pkthdr.len;

	if (sc->gnv_proto == GENEVE_PROTO_ETHER)
		proto = sc->gnv_proto;
	else
		proto = geneve_get_ethertype(m);

	error = geneve_inherit_l3_hdr(m, sc, proto, &tos, &ttl, &ip_off);
	if (error) {
		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
		return (error);
	}

	M_PREPEND(m, sizeof(struct ip) + sizeof(struct geneveudphdr), M_NOWAIT);
	if (m == NULL) {
		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
		return (ENOBUFS);
	}
	ip = mtod(m, struct ip *);

	ecn = (tos & IPTOS_ECN_MASK);
	ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &ecn);
	if (sc->gnv_flags & GENEVE_FLAG_DSCP_INHERIT)
		ip->ip_tos |= (tos & ~IPTOS_ECN_MASK);

	ip->ip_len = htons(m->m_pkthdr.len);
	ip->ip_off = ip_off;
	ip->ip_ttl = ttl;
	ip->ip_p = IPPROTO_UDP;
	ip->ip_sum = 0;
	ip->ip_src = srcaddr;
	ip->ip_dst = dstaddr;

	geneve_encap_header(sc, m, sizeof(struct ip), srcport, dstport, htons(proto));
	mcast = (m->m_flags & (M_MCAST | M_BCAST));
	m->m_flags &= ~(M_MCAST | M_BCAST);

	m->m_pkthdr.csum_flags &= CSUM_FLAGS_TX;
	if (m->m_pkthdr.csum_flags != 0) {
		/*
		 * HW checksum (L3 and/or L4) or TSO has been requested.
		 * Look up the ifnet for the outbound route and verify that the
		 * outbound ifnet can perform the requested operation on the inner frame.
		 */
		memset(&route, 0, sizeof(route));
		ro = &route;
		sin = (struct sockaddr_in *)&ro->ro_dst;
		sin->sin_family = AF_INET;
		sin->sin_len = sizeof(*sin);
		sin->sin_addr = ip->ip_dst;
		ro->ro_nh = fib4_lookup(M_GETFIB(m), ip->ip_dst, 0, NHR_NONE, 0);
		if (ro->ro_nh == NULL) {
			m_freem(m);
			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
			return (EHOSTUNREACH);
		}

		csum_flags = csum_flags_to_inner_flags(m->m_pkthdr.csum_flags,
		    CSUM_ENCAP_GENEVE);
		if ((csum_flags & ro->ro_nh->nh_ifp->if_hwassist) != csum_flags) {
			if (ppsratecheck(&sc->err_time, &sc->err_pps, 1)) {
				const struct ifnet *nh_ifp = ro->ro_nh->nh_ifp;

				if_printf(ifp, "interface %s is missing hwcaps "
				    "0x%08x, csum_flags 0x%08x -> 0x%08x, "
				    "hwassist 0x%08x\n", nh_ifp->if_xname,
				    csum_flags & ~(uint32_t)nh_ifp->if_hwassist,
				    m->m_pkthdr.csum_flags, csum_flags,
				    (uint32_t)nh_ifp->if_hwassist);
			}
			m_freem(m);
			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
			return (ENXIO);
		}
		m->m_pkthdr.csum_flags = csum_flags;
		if (csum_flags & (CSUM_INNER_IP | CSUM_INNER_IP_UDP |
		    CSUM_INNER_IP6_UDP | CSUM_INNER_IP_TCP | CSUM_INNER_IP6_TCP)) {
			counter_u64_add(sc->gnv_stats.txcsum, 1);
			if (csum_flags & CSUM_INNER_TSO)
				counter_u64_add(sc->gnv_stats.tso, 1);
		}
	} else
		ro = NULL;

	error = ip_output(m, NULL, ro, 0, sc->gnv_im4o, NULL);
	if (error == 0) {
		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
		if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
		if (mcast)
			if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
	} else
		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);

	return (error);
}
#endif

#ifdef INET6
static int
geneve_encap6(struct geneve_softc *sc, const union sockaddr_union *funsa,
    struct mbuf *m)
{
	struct ifnet *ifp;
	struct ip6_hdr *ip6;
	struct ip6_pktopts opts;
	struct sockaddr_in6 *sin6;
	struct route_in6 route, *ro;
	const struct in6_addr *srcaddr, *dstaddr;
	int plen, error;
	uint32_t csum_flags;
	uint16_t srcport, dstport, proto;
	u_short ip6_df;
	uint8_t tos, ecn, etos, ttl;
	bool mcast;

	NET_EPOCH_ASSERT();

	ifp = sc->gnv_ifp;
	srcaddr = &sc->gnv_src_addr.sin6.sin6_addr;
	srcport = htons(geneve_pick_source_port(sc, m));
	dstaddr = &funsa->sin6.sin6_addr;
	dstport = funsa->sin6.sin6_port;
	plen = m->m_pkthdr.len;

	if (sc->gnv_proto == GENEVE_PROTO_ETHER)
		proto = sc->gnv_proto;
	else
		proto = geneve_get_ethertype(m);

	error = geneve_inherit_l3_hdr(m, sc, proto, &tos, &ttl, &ip6_df);
	if (error) {
		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
			return (error);
	}

	ip6_initpktopts(&opts);
	if (ip6_df)
		opts.ip6po_flags = IP6PO_DONTFRAG;

	M_PREPEND(m, sizeof(struct ip6_hdr) + sizeof(struct geneveudphdr), M_NOWAIT);
	if (m == NULL) {
		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
		return (ENOBUFS);
	}

	ip6 = mtod(m, struct ip6_hdr *);
	ip6->ip6_flow = 0;
	ip6->ip6_vfc = IPV6_VERSION;

	ecn = (tos & IPTOS_ECN_MASK);
	ip_ecn_ingress(ECN_ALLOWED, &etos, &ecn);
	ip6->ip6_flow |= htonl((u_int32_t)etos << IPV6_FLOWLABEL_LEN);
	if (sc->gnv_flags & GENEVE_FLAG_DSCP_INHERIT)
		ip6->ip6_flow |= htonl((u_int32_t)tos << IPV6_FLOWLABEL_LEN);

	ip6->ip6_plen = 0;
	ip6->ip6_nxt = IPPROTO_UDP;
	ip6->ip6_hlim = ttl;
	ip6->ip6_src = *srcaddr;
	ip6->ip6_dst = *dstaddr;

	geneve_encap_header(sc, m, sizeof(struct ip6_hdr), srcport, dstport,
	    htons(proto));
	mcast = (m->m_flags & (M_MCAST | M_BCAST));
	m->m_flags &= ~(M_MCAST | M_BCAST);

	ro = NULL;
	m->m_pkthdr.csum_flags &= CSUM_FLAGS_TX;
	if (mcast || m->m_pkthdr.csum_flags != 0) {
		/*
		 * HW checksum (L3 and/or L4) or TSO has been requested.  Look
		 * up the ifnet for the outbound route and verify that the
		 * outbound ifnet can perform the requested operation on the
		 * inner frame.
		 * XXX: There's a rare scenario with ipv6 over multicast
		 * underlay where, when mc_ifname is set, it causes panics
		 * inside a jail. We'll force geneve to select its own outbound
		 * interface to avoid this.
		 */
		memset(&route, 0, sizeof(route));
		ro = &route;
		sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
		sin6->sin6_family = AF_INET6;
		sin6->sin6_len = sizeof(*sin6);
		sin6->sin6_addr = ip6->ip6_dst;
		ro->ro_nh = fib6_lookup(M_GETFIB(m), &ip6->ip6_dst, 0, NHR_NONE, 0);
		if (ro->ro_nh == NULL) {
			m_freem(m);
			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
			return (EHOSTUNREACH);
		}
	}
	if (m->m_pkthdr.csum_flags != 0) {
		csum_flags = csum_flags_to_inner_flags(m->m_pkthdr.csum_flags,
		    CSUM_ENCAP_GENEVE);
		if ((csum_flags & ro->ro_nh->nh_ifp->if_hwassist) != csum_flags) {
			if (ppsratecheck(&sc->err_time, &sc->err_pps, 1)) {
				const struct ifnet *nh_ifp = ro->ro_nh->nh_ifp;

				if_printf(ifp, "interface %s is missing hwcaps "
				    "0x%08x, csum_flags 0x%08x -> 0x%08x, "
				    "hwassist 0x%08x\n", nh_ifp->if_xname,
				    csum_flags & ~(uint32_t)nh_ifp->if_hwassist,
				    m->m_pkthdr.csum_flags, csum_flags,
				    (uint32_t)nh_ifp->if_hwassist);
			}
			m_freem(m);
			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
			return (ENXIO);
		}
		m->m_pkthdr.csum_flags = csum_flags;
		if (csum_flags &
		    (CSUM_INNER_IP | CSUM_INNER_IP_UDP | CSUM_INNER_IP6_UDP |
		    CSUM_INNER_IP_TCP | CSUM_INNER_IP6_TCP)) {
			counter_u64_add(sc->gnv_stats.txcsum, 1);
			if (csum_flags & CSUM_INNER_TSO)
				counter_u64_add(sc->gnv_stats.tso, 1);
		}
	} else if (ntohs(dstport) != V_zero_checksum_port) {
		struct udphdr *hdr = mtodo(m, sizeof(struct ip6_hdr));

		hdr->uh_sum = in6_cksum_pseudo(ip6,
		    m->m_pkthdr.len - sizeof(struct ip6_hdr), IPPROTO_UDP, 0);
		m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
	}
	error = ip6_output(m, &opts, ro, 0, sc->gnv_im6o, NULL, NULL);
	if (error == 0) {
		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
		if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
		if (mcast)
			if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
	} else
		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);

	return (error);
}
#endif

static int
geneve_transmit(struct ifnet *ifp, struct mbuf *m)
{
	struct rm_priotracker tracker;
	union sockaddr_union unsa;
	struct geneve_softc *sc;
	struct gnv_ftable_entry *fe;
	struct ifnet *mcifp;
	struct ether_header *eh;
	uint32_t af;
	int error;

	mcifp = NULL;
	sc = ifp->if_softc;
	GENEVE_RLOCK(sc, &tracker);
	M_SETFIB(m, sc->gnv_fibnum);

	if ((sc->gnv_flags & GENEVE_FLAG_RUNNING) == 0) {
		GENEVE_RUNLOCK(sc, &tracker);
		m_freem(m);
		return (ENETDOWN);
	}
	if (__predict_false(if_tunnel_check_nesting(ifp, m,
	    MTAG_GENEVE_LOOP, 1) != 0)) {
		GENEVE_RUNLOCK(sc, &tracker);
		m_freem(m);
		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
		return (ELOOP);
	}

	if (sc->gnv_proto == GENEVE_PROTO_ETHER) {
		fe = NULL;
		eh = mtod(m, struct ether_header *);

		ETHER_BPF_MTAP(ifp, m);
		if ((m->m_flags & (M_BCAST | M_MCAST)) == 0)
			fe = geneve_ftable_entry_lookup(sc, eh->ether_dhost);
		if (fe == NULL)
			fe = &sc->gnv_default_fe;
		geneve_sockaddr_copy(&unsa, &fe->gnvfe_raddr.sa);
	} else
		geneve_sockaddr_copy(&unsa, &sc->gnv_dst_addr.sa);

	af = unsa.sa.sa_family;
	if (geneve_check_multicast_addr(&unsa) != 0)
		mcifp = geneve_multicast_if_ref(sc, af);

	GENEVE_ACQUIRE(sc);
	GENEVE_RUNLOCK(sc, &tracker);

	switch (af) {
#ifdef INET
	case AF_INET:
		error = geneve_encap4(sc, &unsa, m);
		break;
#endif
#ifdef INET6
	case AF_INET6:
		error = geneve_encap6(sc, &unsa, m);
		break;
#endif
	default:
		error = EAFNOSUPPORT;
	}

	geneve_release(sc);
	if (mcifp != NULL)
		if_rele(mcifp);

	return (error);
}

static int
geneve_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    struct route *ro)
{
	uint32_t af;
	int error;

#ifdef MAC
	error = mac_ifnet_check_transmit(ifp, m);
	if (error) {
		m_freem(m);
		return (error);
	}
#endif

	/* BPF writes need to be handled specially. */
	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
		memmove(&af, dst->sa_data, sizeof(af));
	else
		af = RO_GET_FAMILY(ro, dst);

	BPF_MTAP2(ifp, &af, sizeof(af), m);
	error = (ifp->if_transmit)(ifp, m);
	if (error)
		return (ENOBUFS);
	return (0);
}

static int
geneve_next_option(struct geneve_socket *gnvso, struct genevehdr *gnvh,
	struct mbuf **m0)
{
	int optlen, error;

	error = 0;
	/*
	 * We MUST NOT forward the packet if control (O) bit is set
	 * and currently there is not standard specification for it.
	 * Therefore, we drop it.
	 */
	if (gnvh->geneve_control)
		return (EINVAL);

	optlen = gnvh->geneve_optlen;
	if (optlen == 0)
		return (error);

	/*
	 * XXX: Geneve options processing
	 * We MUST drop the packet if there are options to process
	 * and we are not able to process it.
	 */
	if (gnvh->geneve_critical)
		error = EINVAL;

	return (error);
}

static void
geneve_qflush(struct ifnet *ifp __unused)
{
}

static void
geneve_input_csum(struct mbuf *m, struct ifnet *ifp, counter_u64_t rxcsum)
{
	uint32_t csum_flags;

	if ((((ifp->if_capenable & IFCAP_RXCSUM) != 0 &&
	    (m->m_pkthdr.csum_flags & CSUM_INNER_L3_CALC) != 0) ||
	    ((ifp->if_capenable & IFCAP_RXCSUM_IPV6) != 0 &&
	    (m->m_pkthdr.csum_flags & CSUM_INNER_L3_CALC) == 0))) {
		csum_flags = 0;

		if (m->m_pkthdr.csum_flags & CSUM_INNER_L3_CALC)
			csum_flags |= CSUM_L3_CALC;
		if (m->m_pkthdr.csum_flags & CSUM_INNER_L3_VALID)
			csum_flags |= CSUM_L3_VALID;
		if (m->m_pkthdr.csum_flags & CSUM_INNER_L4_CALC)
			csum_flags |= CSUM_L4_CALC;
		if (m->m_pkthdr.csum_flags & CSUM_INNER_L4_VALID)
			csum_flags |= CSUM_L4_VALID;
		m->m_pkthdr.csum_flags = csum_flags;
		counter_u64_add(rxcsum, 1);
	} else {
		/* clear everything */
		m->m_pkthdr.csum_flags = 0;
		m->m_pkthdr.csum_data = 0;
	}
}

static uint32_t
geneve_map_etype_to_af(uint32_t ethertype)
{

	if (ethertype == ETHERTYPE_IP)
		return (AF_INET);
	if (ethertype == ETHERTYPE_IPV6)
		return (AF_INET6);
	if (ethertype == ETHERTYPE_ARP)
		return (AF_LINK);
	return (0);
}

static bool
geneve_udp_input(struct mbuf *m, int offset, struct inpcb *inpcb,
    const struct sockaddr *srcsa, void *xgnvso)
{
	struct geneve_socket *gnvso;
	struct geneve_pkt_info info;
	struct genevehdr *gnvh, gnvhdr;
	struct geneve_softc *sc;
	struct ip *iphdr;
	struct ip6_hdr *ip6hdr;
	struct ifnet *ifp;
	int32_t plen, af;
	uint32_t vni;
	uint16_t optlen, proto;
	int error;

	M_ASSERTPKTHDR(m);
	plen = m->m_pkthdr.len;
	gnvso = xgnvso;

	if (m->m_pkthdr.len < offset + sizeof(struct geneveudphdr))
		return (false);

	/* Get ECN and TTL values for future processing */
	memset(&info, 0, sizeof(info));
	info.ethertype = geneve_get_ethertype(m);
	if (info.ethertype == ETHERTYPE_IP) {
		iphdr = mtodo(m, offset - sizeof(struct ip));
		info.ecn = (iphdr->ip_tos & IPTOS_ECN_MASK);
		info.ttl = iphdr->ip_ttl;
	} else if (info.ethertype == ETHERTYPE_IPV6) {
		ip6hdr = mtodo(m, offset - sizeof(struct ip6_hdr));
		info.ecn = IPV6_ECN(ip6hdr);
		info.ttl = ip6hdr->ip6_hlim;
	}

	/* Get geneve header */
	offset += sizeof(struct udphdr);
	if (__predict_false(m->m_len < offset + sizeof(struct genevehdr))) {
		m_copydata(m, offset, sizeof(struct genevehdr), (caddr_t)&gnvhdr);
		gnvh = &gnvhdr;
	} else
		gnvh = mtodo(m, offset);

	/*
	 * Drop if there is a reserved bit or unknown version set in the header.
	 * As defined in RFC 8926 3.4
	 */
	if (gnvh->geneve_ver != htons(GENEVE_VERSION) ||
	    gnvh->geneve_vni & ~GENEVE_VNI_MASK)
		return (false);

	/*
	 * The length of the option fields, expressed in 4-byte multiples, not
	 * including the 8-byte fixed tunnel header.
	 */
	optlen = ntohs(gnvh->geneve_optlen) * 4;
	error = geneve_next_option(gnvso, gnvh, &m);
	if (error != 0)
		return (false);

	vni = ntohl(gnvh->geneve_vni) >> GENEVE_HDR_VNI_SHIFT;
	sc = geneve_socket_lookup_softc(gnvso, vni);
	if (sc == NULL)
		return (false);

	if ((sc->gnv_flags & GENEVE_FLAG_RUNNING) == 0)
		goto out;

	proto = ntohs(gnvh->geneve_proto);
	m_adj(m, offset + sizeof(struct genevehdr) + optlen);

	/* if next protocol is ethernet, check its ethertype and learn it */
	if (proto == GENEVE_PROTO_ETHER) {
		offset = ETHER_HDR_LEN;
		error = geneve_input_ether(sc, &m, srcsa, &info);
		if (error != 0)
			goto out;
	} else {
		info.ethertype = proto;
		af = geneve_map_etype_to_af(info.ethertype);
		offset = 0;
	}

	error = geneve_input_inherit(sc, &m, offset, &info);
	if (error != 0)
		goto out;

	ifp = sc->gnv_ifp;
	if (ifp == m->m_pkthdr.rcvif)
		/* XXX Does not catch more complex loops. */
		goto out;

	m_clrprotoflags(m);
	m->m_pkthdr.rcvif = ifp;
	M_SETFIB(m, ifp->if_fib);
	geneve_input_csum(m, ifp, sc->gnv_stats.rxcsum);
	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
	if_inc_counter(ifp, IFCOUNTER_IBYTES, plen);
	if (sc->gnv_mc_ifp != NULL)
		if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);

	MPASS(m != NULL);

	if (proto == GENEVE_PROTO_ETHER)
		(*ifp->if_input)(ifp, m);
	else {
		BPF_MTAP2(ifp, &af, sizeof(af), m);
		netisr_dispatch_src(info.isr, (uintptr_t)xgnvso, m);
	}

	m = NULL;
out:
	geneve_release(sc);
	if (m != NULL) {
		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
		m_freem(m);
	}

	return (true);
}

static int
geneve_input_ether(struct geneve_softc *sc, struct mbuf **m0,
    const struct sockaddr *sa, struct geneve_pkt_info *info)
{
	struct mbuf *m;
	struct ether_header *eh;

	m = *m0;

	if (sc->gnv_proto != GENEVE_PROTO_ETHER)
		return (EPROTOTYPE);

	if (m->m_pkthdr.len < ETHER_HDR_LEN)
		return (EINVAL);

	if (m->m_len < ETHER_HDR_LEN &&
	    (m = m_pullup(m, ETHER_HDR_LEN)) == NULL) {
		*m0 = NULL;
		return (ENOBUFS);
	}

	eh = mtod(m, struct ether_header *);
	info->ethertype = ntohs(eh->ether_type);
	if (sc->gnv_flags & GENEVE_FLAG_LEARN)
		geneve_ftable_learn(sc, sa, eh->ether_shost);

	*m0 = m;
	return (0);
}

static int
geneve_input_inherit(struct geneve_softc *sc, struct mbuf **m0,
    int offset, struct geneve_pkt_info *info)
{
	struct mbuf *m;
	struct ip *iphdr;
	struct ip6_hdr *ip6hdr;
	uint8_t itos;

	m = *m0;

	switch (info->ethertype) {
	case ETHERTYPE_IP:
		offset += sizeof(struct ip);
		if (m->m_pkthdr.len < offset)
			return (EINVAL);

		if (m->m_len < offset &&
		    (m = m_pullup(m, offset)) == NULL) {
			*m0 = NULL;
			return (ENOBUFS);
		}
		iphdr = mtodo(m, offset - sizeof(struct ip));

		if (ip_ecn_egress(ECN_COMPLETE, &info->ecn, &iphdr->ip_tos) == 0) {
			*m0 = NULL;
			return (ENOBUFS);
		}

		if ((sc->gnv_flags & GENEVE_FLAG_TTL_INHERIT) != 0 && info->ttl > 0)
			iphdr->ip_ttl = info->ttl;

		info->isr = NETISR_IP;
		break;

	case ETHERTYPE_IPV6:
		offset += sizeof(struct ip6_hdr);
		if (m->m_pkthdr.len < offset)
			return (EINVAL);

		if (m->m_len < offset &&
		    (m = m_pullup(m, offset)) == NULL) {
			*m0 = NULL;
			return (ENOBUFS);
		}
		ip6hdr = mtodo(m, offset - sizeof(struct ip6_hdr));

		itos = (ntohl(ip6hdr->ip6_flow) >> IPV6_FLOWLABEL_LEN) & 0xff;
		if (ip_ecn_egress(ECN_COMPLETE, &info->ecn, &itos) == 0) {
			*m0 = NULL;
			return (ENOBUFS);
		}
		ip6hdr->ip6_flow |= htonl((uint32_t)itos << IPV6_FLOWLABEL_LEN);

		if ((sc->gnv_flags & GENEVE_FLAG_TTL_INHERIT) && (info->ttl > 0))
			ip6hdr->ip6_hlim = info->ttl;

		info->isr = NETISR_IPV6;
		break;

	case ETHERTYPE_ARP:
		if (sc->gnv_proto == GENEVE_PROTO_INHERIT)
			return (EINVAL);

		offset += sizeof(struct arphdr);
		if (m->m_pkthdr.len < offset)
			return (EINVAL);

		if (m->m_len < offset &&
		    (m = m_pullup(m, offset)) == NULL) {
			*m0 = NULL;
			return (ENOBUFS);
		}
		info->isr = NETISR_ARP;
		break;

	default:
		if_inc_counter(sc->gnv_ifp, IFCOUNTER_NOPROTO, 1);
		return (EINVAL);
	}

	*m0 = m;
	return (0);
}

static void
geneve_stats_alloc(struct geneve_softc *sc)
{
	struct geneve_statistics *stats = &sc->gnv_stats;

	stats->txcsum = counter_u64_alloc(M_WAITOK);
	stats->tso = counter_u64_alloc(M_WAITOK);
	stats->rxcsum = counter_u64_alloc(M_WAITOK);
}

static void
geneve_stats_free(struct geneve_softc *sc)
{
	struct geneve_statistics *stats = &sc->gnv_stats;

	counter_u64_free(stats->txcsum);
	counter_u64_free(stats->tso);
	counter_u64_free(stats->rxcsum);
}

static void
geneve_set_default_config(struct geneve_softc *sc)
{

	sc->gnv_flags |= GENEVE_FLAG_LEARN;

	sc->gnv_vni = GENEVE_VNI_MAX;
	sc->gnv_ttl = V_ip_defttl;

	sc->gnv_src_addr.sin.sin_port = htons(GENEVE_UDPPORT);
	sc->gnv_dst_addr.sin.sin_port = htons(GENEVE_UDPPORT);

	/*
	 * RFC 8926 Section 3.3, the entire 16-bit range MAY
	 * be used to maximize entropy.
	 */
	sc->gnv_min_port = V_ipport_firstauto;
	sc->gnv_max_port = V_ipport_lastauto;

	sc->gnv_proto = GENEVE_PROTO_ETHER;

	sc->gnv_ftable_max = GENEVE_FTABLE_MAX;
	sc->gnv_ftable_timeout = GENEVE_FTABLE_TIMEOUT;
}

static int
geneve_set_reqcap(struct geneve_softc *sc, struct ifnet *ifp, int reqcap,
    int reqcap2)
{
	int mask = reqcap ^ ifp->if_capenable;

	/* Disable TSO if tx checksums are disabled. */
	if (mask & IFCAP_TXCSUM && !(reqcap & IFCAP_TXCSUM) &&
	    reqcap & IFCAP_TSO4) {
		reqcap &= ~IFCAP_TSO4;
		if_printf(ifp, "tso4 disabled due to -txcsum.\n");
	}
	if (mask & IFCAP_TXCSUM_IPV6 && !(reqcap & IFCAP_TXCSUM_IPV6) &&
	    reqcap & IFCAP_TSO6) {
		reqcap &= ~IFCAP_TSO6;
		if_printf(ifp, "tso6 disabled due to -txcsum6.\n");
	}

	/* Do not enable TSO if tx checksums are disabled. */
	if (mask & IFCAP_TSO4 && reqcap & IFCAP_TSO4 &&
	    !(reqcap & IFCAP_TXCSUM)) {
		if_printf(ifp, "enable txcsum first.\n");
		return (EAGAIN);
	}
	if (mask & IFCAP_TSO6 && reqcap & IFCAP_TSO6 &&
	    !(reqcap & IFCAP_TXCSUM_IPV6)) {
		if_printf(ifp, "enable txcsum6 first.\n");
		return (EAGAIN);
	}

	sc->gnv_reqcap = reqcap;
	sc->gnv_reqcap2 = reqcap2;
	return (0);
}

/*
 * A GENEVE interface inherits the capabilities of the genevedev or the interface
 * hosting the genevelocal address.
 */
static void
geneve_set_hwcaps(struct geneve_softc *sc)
{
	struct epoch_tracker et;
	struct ifnet *p, *ifp;
	struct ifaddr *ifa;
	u_long hwa;
	int cap, ena;
	bool rel;

	/* reset caps */
	ifp = sc->gnv_ifp;
	ifp->if_capabilities &= GENEVE_BASIC_IFCAPS;
	ifp->if_capenable &= GENEVE_BASIC_IFCAPS;
	ifp->if_hwassist = 0;

	NET_EPOCH_ENTER(et);
	CURVNET_SET(ifp->if_vnet);

	p = NULL;
	rel = false;
	if (sc->gnv_mc_ifname[0] != '\0') {
		rel = true;
		p = ifunit_ref(sc->gnv_mc_ifname);
	} else if (geneve_sockaddr_in_any(&sc->gnv_src_addr) == 0) {
		if (sc->gnv_src_addr.sa.sa_family == AF_INET) {
			struct sockaddr_in in4 = sc->gnv_src_addr.sin;

			in4.sin_port = 0;
			ifa = ifa_ifwithaddr((struct sockaddr *)&in4);
			if (ifa != NULL)
				p = ifa->ifa_ifp;
		} else if (sc->gnv_src_addr.sa.sa_family == AF_INET6) {
			struct sockaddr_in6 in6 = sc->gnv_src_addr.sin6;

			in6.sin6_port = 0;
			ifa = ifa_ifwithaddr((struct sockaddr *)&in6);
			if (ifa != NULL)
				p = ifa->ifa_ifp;
		}
	}
	if (p == NULL) {
		CURVNET_RESTORE();
		NET_EPOCH_EXIT(et);
		return;
	}

	cap = ena = hwa = 0;

	/* checksum offload */
	if ((p->if_capabilities2 & IFCAP2_BIT(IFCAP2_GENEVE_HWCSUM)) != 0)
		cap |= p->if_capabilities & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
	if ((p->if_capenable2 & IFCAP2_BIT(IFCAP2_GENEVE_HWCSUM)) != 0) {
		ena |= sc->gnv_reqcap & p->if_capenable & (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6);
		if (ena & IFCAP_TXCSUM) {
			if (p->if_hwassist & CSUM_INNER_IP)
				hwa |= CSUM_IP;
			if (p->if_hwassist & CSUM_INNER_IP_UDP)
				hwa |= CSUM_IP_UDP;
			if (p->if_hwassist & CSUM_INNER_IP_TCP)
				hwa |= CSUM_IP_TCP;
		}
		if (ena & IFCAP_TXCSUM_IPV6) {
			if (p->if_hwassist & CSUM_INNER_IP6_UDP)
				hwa |= CSUM_IP6_UDP;
			if (p->if_hwassist & CSUM_INNER_IP6_TCP)
				hwa |= CSUM_IP6_TCP;
		}
	}

	/* hardware TSO */
	if ((p->if_capabilities2 & IFCAP2_BIT(IFCAP2_GENEVE_HWTSO)) != 0) {
		cap |= p->if_capabilities & IFCAP_TSO;
		if (p->if_hw_tsomax > IP_MAXPACKET - ifp->if_hdrlen)
			ifp->if_hw_tsomax = IP_MAXPACKET - ifp->if_hdrlen;
		else
			ifp->if_hw_tsomax = p->if_hw_tsomax;
		ifp->if_hw_tsomaxsegcount = p->if_hw_tsomaxsegcount - 1;
		ifp->if_hw_tsomaxsegsize = p->if_hw_tsomaxsegsize;
	}
	if ((p->if_capenable2 & IFCAP2_BIT(IFCAP2_GENEVE_HWTSO)) != 0) {
		ena |= sc->gnv_reqcap & p->if_capenable & IFCAP_TSO;
		if (ena & IFCAP_TSO) {
			if (p->if_hwassist & CSUM_INNER_IP_TSO)
				hwa |= CSUM_IP_TSO;
			if (p->if_hwassist & CSUM_INNER_IP6_TSO)
				hwa |= CSUM_IP6_TSO;
		}
	}

	ifp->if_capabilities |= cap;
	ifp->if_capenable |= ena;
	ifp->if_hwassist |= hwa;
	if (rel)
		if_rele(p);

	CURVNET_RESTORE();
	NET_EPOCH_EXIT(et);
}

static int
geneve_clone_create_nl(struct if_clone *ifc, char *name, size_t len,
    struct ifc_data_nl *ifd)
{
	struct nl_parsed_link *lattrs = ifd->lattrs;
	struct nl_pstate *npt = ifd->npt;
	struct nl_parsed_geneve attrs = {};
	int error;

	if ((lattrs->ifla_idata == NULL) ||
	    (!nl_has_attr(ifd->bm, IFLA_LINKINFO))) {
		nlmsg_report_err_msg(npt, "geneve protocol is required");
		return (ENOTSUP);
	}

	error = nl_parse_nested(lattrs->ifla_idata, &geneve_create_parser, npt, &attrs);
	if (error != 0)
		return (error);
	if (geneve_check_proto(attrs.ifla_proto)) {
		nlmsg_report_err_msg(npt, "Unsupported ethertype: 0x%04X", attrs.ifla_proto);
		return (ENOTSUP);
	}

	struct geneve_params gnvp = { .ifla_proto = attrs.ifla_proto };
	struct ifc_data ifd_new = {
		.flags = IFC_F_SYSSPACE,
		.unit = ifd->unit,
		.params = &gnvp
	};

	return (geneve_clone_create(ifc, name, len, &ifd_new, &ifd->ifp));
}

static int
geneve_clone_modify_nl(struct ifnet *ifp, struct ifc_data_nl *ifd)
{
	struct geneve_softc *sc = ifp->if_softc;
	struct nl_parsed_link *lattrs = ifd->lattrs;
	struct nl_pstate *npt = ifd->npt;
	struct nl_parsed_geneve params;
	struct nlattr *attrs = lattrs->ifla_idata;
	struct nlattr_bmask bm;
	int error = 0;

	if ((attrs == NULL) ||
	    (nl_has_attr(ifd->bm, IFLA_LINKINFO) == 0)) {
		error = nl_modify_ifp_generic(ifp, lattrs, ifd->bm, npt);
		return (error);
	}

	error = priv_check(curthread, PRIV_NET_GENEVE);
	if (error)
		return (error);

	/* make sure ignored attributes by nl_parse will not cause panics */
	memset(&params, 0, sizeof(params));

	nl_get_attrs_bmask_raw(NLA_DATA(attrs), NLA_DATA_LEN(attrs), &bm);
	error = nl_parse_nested(attrs, &geneve_modify_parser, npt, &params);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_ID))
		error = geneve_set_vni_nl(sc, npt, params.ifla_vni);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_LOCAL))
		error = geneve_set_local_addr_nl(sc, npt, params.ifla_local);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_REMOTE))
		error = geneve_set_remote_addr_nl(sc, npt, params.ifla_remote);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_LOCAL_PORT))
		error = geneve_set_local_port_nl(sc, npt, params.ifla_local_port);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_PORT))
		error = geneve_set_remote_port_nl(sc, npt, params.ifla_remote_port);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_PORT_RANGE))
		error = geneve_set_port_range_nl(sc, npt, params.ifla_port_range);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_DF))
		error = geneve_set_df_nl(sc, npt, params.ifla_df);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_TTL))
		error = geneve_set_ttl_nl(sc, npt, params.ifla_ttl);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_TTL_INHERIT))
		error = geneve_set_ttl_inherit_nl(sc, npt, params.ifla_ttl_inherit);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_DSCP_INHERIT))
		error = geneve_set_dscp_inherit_nl(sc, npt, params.ifla_dscp_inherit);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_COLLECT_METADATA))
		error = geneve_set_collect_metadata_nl(sc, npt, params.ifla_external);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_FTABLE_LEARN))
		error = geneve_set_learn_nl(sc, npt, params.ifla_ftable_learn);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_FTABLE_FLUSH))
		error = geneve_flush_ftable_nl(sc, npt, params.ifla_ftable_flush);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_FTABLE_MAX))
		error = geneve_set_ftable_max_nl(sc, npt, params.ifla_ftable_max);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_FTABLE_TIMEOUT))
		error = geneve_set_ftable_timeout_nl(sc, npt, params.ifla_ftable_timeout);

	if (error == 0 && nl_has_attr(&bm, IFLA_GENEVE_MC_IFNAME))
		error = geneve_set_mc_if_nl(sc, npt, params.ifla_mc_ifname);

	if (error == 0)
		error = nl_modify_ifp_generic(ifp, lattrs, ifd->bm, npt);

	return (error);
}

static void
geneve_clone_dump_nl(struct ifnet *ifp, struct nl_writer *nw)
{
	struct geneve_softc *sc;
	struct rm_priotracker tracker;
	int off, off2;

	nlattr_add_u32(nw, IFLA_LINK, ifp->if_index);
	nlattr_add_string(nw, IFLA_IFNAME, ifp->if_xname);

	off = nlattr_add_nested(nw, IFLA_LINKINFO);
	if (off == 0)
		return;

	nlattr_add_string(nw, IFLA_INFO_KIND, "geneve");
	off2 = nlattr_add_nested(nw, IFLA_INFO_DATA);
	if (off2 == 0) {
		nlattr_set_len(nw, off);
		return;
	}

	sc = ifp->if_softc;
	GENEVE_RLOCK(sc, &tracker);

	nlattr_add_u32(nw, IFLA_GENEVE_ID, sc->gnv_vni);
	nlattr_add_u16(nw, IFLA_GENEVE_PROTOCOL, sc->gnv_proto);
	geneve_get_local_addr_nl(sc, nw);
	geneve_get_remote_addr_nl(sc, nw);
	nlattr_add_u16(nw, IFLA_GENEVE_LOCAL_PORT, geneve_get_local_port(sc));
	nlattr_add_u16(nw, IFLA_GENEVE_PORT, geneve_get_remote_port(sc));

	const struct ifla_geneve_port_range port_range = {
		.low = sc->gnv_min_port,
		.high = sc->gnv_max_port
	};
	nlattr_add(nw, IFLA_GENEVE_PORT_RANGE, sizeof(port_range), &port_range);

	nlattr_add_u8(nw, IFLA_GENEVE_DF, (uint8_t)sc->gnv_df);
	nlattr_add_u8(nw, IFLA_GENEVE_TTL, sc->gnv_ttl);
	nlattr_add_bool(nw, IFLA_GENEVE_TTL_INHERIT,
	    sc->gnv_flags & GENEVE_FLAG_TTL_INHERIT);
	nlattr_add_bool(nw, IFLA_GENEVE_DSCP_INHERIT,
	    sc->gnv_flags & GENEVE_FLAG_DSCP_INHERIT);
	nlattr_add_bool(nw, IFLA_GENEVE_COLLECT_METADATA,
	    sc->gnv_flags & GENEVE_FLAG_COLLECT_METADATA);

	nlattr_add_bool(nw, IFLA_GENEVE_FTABLE_LEARN,
	    sc->gnv_flags & GENEVE_FLAG_LEARN);
	nlattr_add_u32(nw, IFLA_GENEVE_FTABLE_MAX, sc->gnv_ftable_max);
	nlattr_add_u32(nw, IFLA_GENEVE_FTABLE_TIMEOUT, sc->gnv_ftable_timeout);
	nlattr_add_u32(nw, IFLA_GENEVE_FTABLE_COUNT, sc->gnv_ftable_cnt);
	nlattr_add_u32(nw, IFLA_GENEVE_FTABLE_NOSPACE_CNT, sc->gnv_stats.ftable_nospace);
	nlattr_add_u32(nw, IFLA_GENEVE_FTABLE_LOCK_UP_FAIL_CNT,
	    sc->gnv_stats.ftable_lock_upgrade_failed);

	nlattr_add_string(nw, IFLA_GENEVE_MC_IFNAME, sc->gnv_mc_ifname);
	nlattr_add_u32(nw, IFLA_GENEVE_MC_IFINDEX, sc->gnv_mc_ifindex);

	nlattr_add_u64(nw, IFLA_GENEVE_TXCSUM_CNT,
	    counter_u64_fetch(sc->gnv_stats.txcsum));
	nlattr_add_u64(nw, IFLA_GENEVE_TSO_CNT,
	    counter_u64_fetch(sc->gnv_stats.tso));
	nlattr_add_u64(nw, IFLA_GENEVE_RXCSUM_CNT,
	    counter_u64_fetch(sc->gnv_stats.rxcsum));

	nlattr_set_len(nw, off2);
	nlattr_set_len(nw, off);

	GENEVE_RUNLOCK(sc, &tracker);
}

static int
geneve_clone_create(struct if_clone *ifc, char *name, size_t len,
    struct ifc_data *ifd, struct ifnet **ifpp)
{
	struct geneve_softc *sc;
	struct geneve_params gnvp;
	struct ifnet *ifp;
	int error;

	sc = malloc(sizeof(struct geneve_softc), M_GENEVE, M_WAITOK | M_ZERO);
	sc->gnv_fibnum = curthread->td_proc->p_fibnum;
	geneve_set_default_config(sc);

	if (ifd != NULL) {
		error = ifc_copyin(ifd, &gnvp, sizeof(gnvp));
		if (error != 0 ||
		    (error = geneve_check_proto(gnvp.ifla_proto)) != 0) {
			free(sc, M_GENEVE);
			return (error);
		}

		sc->gnv_proto = gnvp.ifla_proto;
	}

	if (sc->gnv_proto == GENEVE_PROTO_ETHER) {
		ifp = if_alloc(IFT_ETHER);
		ifp->if_flags |= IFF_SIMPLEX | IFF_BROADCAST;
		geneve_ftable_init(sc);
		callout_init_rw(&sc->gnv_callout, &sc->gnv_lock, 0);
	} else if (sc->gnv_proto == GENEVE_PROTO_INHERIT) {
		ifp = if_alloc(IFT_TUNNEL);
		ifp->if_flags |= IFF_NOARP;
	} else {
		free(sc, M_GENEVE);
		return (EINVAL);
	}

	geneve_stats_alloc(sc);
	sc->gnv_ifp = ifp;
	rm_init(&sc->gnv_lock, "geneverm");
	sc->gnv_port_hash_key = arc4random();

	ifp->if_softc = sc;
	if_initname(ifp, geneve_name, ifd->unit);
	ifp->if_flags |= IFF_MULTICAST;
	ifp->if_init = geneve_init;
	ifp->if_ioctl = geneve_ioctl;
	ifp->if_transmit = geneve_transmit;
	ifp->if_qflush = geneve_qflush;
	ifp->if_capabilities = GENEVE_BASIC_IFCAPS;
	ifp->if_capenable = GENEVE_BASIC_IFCAPS;
	sc->gnv_reqcap = -1;
	geneve_set_hwcaps(sc);

	if (sc->gnv_proto == GENEVE_PROTO_ETHER) {
		ifmedia_init(&sc->gnv_media, 0, geneve_media_change, geneve_media_status);
		ifmedia_add(&sc->gnv_media, IFM_ETHER | IFM_AUTO, 0, NULL);
		ifmedia_set(&sc->gnv_media, IFM_ETHER | IFM_AUTO);

		ether_gen_addr(ifp, &sc->gnv_hwaddr);
		ether_ifattach(ifp, sc->gnv_hwaddr.octet);

		ifp->if_baudrate = 0;
	} else {
		ifp->if_output = geneve_output;

		if_attach(ifp);
		bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
	}

	GENEVE_WLOCK(sc);
	geneve_setup_interface_hdrlen(sc);
	GENEVE_WUNLOCK(sc);
	*ifpp = ifp;

	return (0);
}

static int
geneve_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
{
	struct geneve_softc *sc;

	sc = if_getsoftc(ifp);
	geneve_teardown(sc);

	if (sc->gnv_proto == GENEVE_PROTO_ETHER) {
		geneve_ftable_flush(sc, 1);

		ether_ifdetach(ifp);
		if_free(ifp);
		ifmedia_removeall(&sc->gnv_media);

		geneve_ftable_fini(sc);
	} else {
		bpfdetach(ifp);
		if_detach(ifp);
		if_free(ifp);
	}

	rm_destroy(&sc->gnv_lock);
	geneve_stats_free(sc);
	free(sc, M_GENEVE);

	return (0);
}

/* BMV: Taken from if_bridge. */
static uint32_t
geneve_mac_hash(struct geneve_softc *sc, const uint8_t *addr)
{
	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->gnv_ftable_hash_key;

	b += addr[5] << 8;
	b += addr[4];
	a += addr[3] << 24;
	a += addr[2] << 16;
	a += addr[1] << 8;
	a += addr[0];

/*
 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
 */
#define	mix(a, b, c)							\
do {									\
	a -= b; a -= c; a ^= (c >> 13);					\
	b -= c; b -= a; b ^= (a << 8);					\
	c -= a; c -= b; c ^= (b >> 13);					\
	a -= b; a -= c; a ^= (c >> 12);					\
	b -= c; b -= a; b ^= (a << 16);					\
	c -= a; c -= b; c ^= (b >> 5);					\
	a -= b; a -= c; a ^= (c >> 3);					\
	b -= c; b -= a; b ^= (a << 10);					\
	c -= a; c -= b; c ^= (b >> 15);					\
} while (0)

	mix(a, b, c);

#undef mix

	return (c);
}

static int
geneve_media_change(struct ifnet *ifp)
{

	/* Ignore. */
	return (0);
}

static void
geneve_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
{

	ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
	ifmr->ifm_active = IFM_ETHER | IFM_FDX;
}

static int
geneve_sockaddr_cmp(const union sockaddr_union *unsa,
    const struct sockaddr *sa)
{

	return (memcmp(&unsa->sa, sa, unsa->sa.sa_len));
}

static void
geneve_sockaddr_copy(union sockaddr_union *dst,
    const struct sockaddr *sa)
{

	MPASS(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
	memset(dst, 0, sizeof(*dst));

	if (sa->sa_family == AF_INET) {
		dst->sin = *SATOCONSTSIN(sa);
		dst->sin.sin_len = sizeof(struct sockaddr_in);
	} else if (sa->sa_family == AF_INET6) {
		dst->sin6 = *SATOCONSTSIN6(sa);
		dst->sin6.sin6_len = sizeof(struct sockaddr_in6);
	}
}

static int
geneve_sockaddr_in_equal(const union sockaddr_union *unsa,
    const struct sockaddr *sa)
{
	int equal;

	if (sa->sa_family == AF_INET) {
		const struct in_addr *in4 = &SATOCONSTSIN(sa)->sin_addr;
		equal = in4->s_addr == unsa->sin.sin_addr.s_addr;
	} else if (sa->sa_family == AF_INET6) {
		const struct in6_addr *in6 = &SATOCONSTSIN6(sa)->sin6_addr;
		equal = IN6_ARE_ADDR_EQUAL(in6, &unsa->sin6.sin6_addr);
	} else
		equal = 0;

	return (equal);
}

static void
geneve_sockaddr_in_copy(union sockaddr_union *dst,
    const struct sockaddr *sa)
{

	MPASS(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);

	if (sa->sa_family == AF_INET) {
		const struct in_addr *in4 = &SATOCONSTSIN(sa)->sin_addr;
		dst->sin.sin_family = AF_INET;
		dst->sin.sin_len = sizeof(struct sockaddr_in);
		dst->sin.sin_addr = *in4;
	} else if (sa->sa_family == AF_INET6) {
		const struct in6_addr *in6 = &SATOCONSTSIN6(sa)->sin6_addr;
		dst->sin6.sin6_family = AF_INET6;
		dst->sin6.sin6_len = sizeof(struct sockaddr_in6);
		dst->sin6.sin6_addr = *in6;
	}
}

static int
geneve_sockaddr_supported(const union sockaddr_union *gnvaddr, int unspec)
{
	const struct sockaddr *sa;
	int supported;

	sa = &gnvaddr->sa;
	supported = 0;

	if (sa->sa_family == AF_UNSPEC && unspec != 0) {
		supported = 1;
	} else if (sa->sa_family == AF_INET) {
		supported = 1;
	} else if (sa->sa_family == AF_INET6) {
		supported = 1;
	}

	return (supported);
}

static int
geneve_sockaddr_in_any(const union sockaddr_union *gnvaddr)
{
	const struct sockaddr *sa;
	int any;

	sa = &gnvaddr->sa;

	if (sa->sa_family == AF_INET) {
		const struct in_addr *in4 = &SATOCONSTSIN(sa)->sin_addr;
		any = in4->s_addr == INADDR_ANY;
	} else if (sa->sa_family == AF_INET6) {
		const struct in6_addr *in6 = &SATOCONSTSIN6(sa)->sin6_addr;
		any = IN6_IS_ADDR_UNSPECIFIED(in6);
	} else
		any = -1;

	return (any);
}

static int
geneve_can_change_config(struct geneve_softc *sc)
{

	GENEVE_LOCK_ASSERT(sc);

	if (sc->gnv_flags & GENEVE_FLAG_RUNNING)
		return (0);
	if (sc->gnv_flags & (GENEVE_FLAG_INIT | GENEVE_FLAG_TEARDOWN))
		return (0);
	if (sc->gnv_flags & GENEVE_FLAG_COLLECT_METADATA)
		return (0);

	return (1);
}

static int
geneve_check_proto(uint16_t proto)
{
	int error;

	switch (proto) {
	case GENEVE_PROTO_ETHER:
	case GENEVE_PROTO_INHERIT:
		error = 0;
		break;

	default:
		error = EAFNOSUPPORT;
		break;
	}

	return (error);
}

static int
geneve_check_multicast_addr(const union sockaddr_union *sa)
{
	int mc;

	if (sa->sa.sa_family == AF_INET) {
		const struct in_addr *in4 = &SATOCONSTSIN(sa)->sin_addr;
		mc = IN_MULTICAST(ntohl(in4->s_addr));
	} else if (sa->sa.sa_family == AF_INET6) {
		const struct in6_addr *in6 = &SATOCONSTSIN6(sa)->sin6_addr;
		mc = IN6_IS_ADDR_MULTICAST(in6);
	} else
		mc = EINVAL;

	return (mc);
}

static int
geneve_check_sockaddr(const union sockaddr_union *sa, const int len)
{
	int error;

	error = 0;
	switch (sa->sa.sa_family) {
	case AF_INET:
	case AF_INET6:
		if (len < sizeof(struct sockaddr))
			error = EINVAL;
		break;

	default:
		error = EAFNOSUPPORT;
	}

	return (error);
}

static int
geneve_prison_remove(void *obj, void *data __unused)
{
#ifdef VIMAGE
	struct prison *pr;

	pr = obj;
	if (prison_owns_vnet(pr)) {
		CURVNET_SET(pr->pr_vnet);
		if (V_geneve_cloner != NULL) {
			ifc_detach_cloner(V_geneve_cloner);
			V_geneve_cloner = NULL;
		}
		CURVNET_RESTORE();
	}
#endif
	return (0);
}

static void
vnet_geneve_load(void)
{
	struct if_clone_addreq_v2 req = {
		.version = 2,
		.flags = IFC_F_AUTOUNIT,
		.match_f = NULL,
		.create_f = geneve_clone_create,
		.destroy_f = geneve_clone_destroy,
		.create_nl_f = geneve_clone_create_nl,
		.modify_nl_f = geneve_clone_modify_nl,
		.dump_nl_f = geneve_clone_dump_nl,
	};
	V_geneve_cloner = ifc_attach_cloner(geneve_name, (struct if_clone_addreq *)&req);
}
VNET_SYSINIT(vnet_geneve_load, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, vnet_geneve_load, NULL);

static void
vnet_geneve_unload(void)
{

	if (V_geneve_cloner != NULL)
		ifc_detach_cloner(V_geneve_cloner);
}
VNET_SYSUNINIT(vnet_geneve_unload, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, vnet_geneve_unload, NULL);

static void
geneve_module_init(void)
{
	mtx_init(&geneve_list_mtx, "geneve list", NULL, MTX_DEF);
	osd_method_t methods[PR_MAXMETHOD] = {
		[PR_METHOD_REMOVE] = geneve_prison_remove,
	};

	geneve_osd_jail_slot = osd_jail_register(NULL, methods);
	NL_VERIFY_PARSERS(all_parsers);
}

static void
geneve_module_deinit(void)
{
	struct if_clone *clone;
	VNET_ITERATOR_DECL(vnet_iter);

	VNET_LIST_RLOCK();
	VNET_FOREACH(vnet_iter) {
		clone = VNET_VNET(vnet_iter, geneve_cloner);
		if (clone != NULL) {
			ifc_detach_cloner(clone);
			VNET_VNET(vnet_iter, geneve_cloner) = NULL;
		}
	}
	VNET_LIST_RUNLOCK();
	NET_EPOCH_WAIT();
	MPASS(LIST_EMPTY(&geneve_socket_list));
	mtx_destroy(&geneve_list_mtx);
	if (geneve_osd_jail_slot != 0)
		osd_jail_deregister(geneve_osd_jail_slot);
}

static int
geneve_modevent(module_t mod, int type, void *unused)
{
	int error;

	error = 0;

	switch (type) {
	case MOD_LOAD:
		geneve_module_init();
		break;

	case MOD_UNLOAD:
		geneve_module_deinit();
		break;

	default:
		error = ENOTSUP;
		break;
	}

	return (error);
}

static moduledata_t geneve_mod = {
	"if_geneve",
	geneve_modevent,
	0
};

DECLARE_MODULE(if_geneve, geneve_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
MODULE_VERSION(if_geneve, 1);