|
root / base / usr / src / lib / libipmp
libipmp Plain Text 2629 lines 67.7 KB
   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
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Copyright 2021 Tintri by DDN, Inc.  All rights reserved.
#

include $(SRC)/lib/Makefile.lib

HDRS =		ipmp.h ipmp_admin.h ipmp_mpathd.h ipmp_query.h ipmp_query_impl.h
HDRDIR =	common
# Hammerhead: amd64-only
SUBDIRS = $(MACH64)

POFILE =	libipmp.po
MSGFILES =	common/ipmp.c
XGETFLAGS =	-a

TYPECHECK_LIB =	libipmp.so.1
TYPELIST = \
	ipmp_addrinfo_t \
	ipmp_addrlist_t \
	ipmp_groupinfo_xfer_t \
	ipmp_grouplist_t \
	ipmp_ifinfo_xfer_t \
	ipmp_iflist_t \
	ipmp_infotype_t \
	ipmp_targinfo_xfer_t \
	mi_query_t \
	mi_result_t

all :		TARGET = all
clean :	TARGET = clean
clobber :	TARGET = clobber
install	:	TARGET = install

.KEEP_STATE:

all clean clobber install: $(SUBDIRS)

install_h:	$(ROOTHDRS)

check:		$(CHECKHDRS) $(TYPECHECK)

$(POFILE):	$(MSGFILES)
		$(BUILDPO.msgfiles)

_msg:		$(MSGDOMAINPOFILE)

$(SUBDIRS): FRC
	@cd $@; pwd; $(MAKE) $(TARGET)

FRC:

include $(SRC)/Makefile.msg.targ
include $(SRC)/lib/Makefile.targ
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
# Copyright 2019 Joyent, Inc.
#

LIBRARY =	libipmp.a
VERS =		.1
OBJECTS =	ipmp_admin.o ipmp_query.o ipmp_mpathd.o ipmp.o

include ../../Makefile.lib
include ../../Makefile.rootfs

LIBS =		$(DYNLIB)
LDLIBS +=	-linetutil -lsocket -lc

SRCDIR =	../common

CFLAGS +=	$(CCVERBOSE)
CPPFLAGS +=	-D_REENTRANT -I$(SRCDIR)

# ipmp_snap_take() generates false double free and dereferencing freed memory
# errors
pics/ipmp_query.o : SMOFF = check_free

.KEEP_STATE:

all: $(LIBS)


include ../../Makefile.targ
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2021, Tintri by DDN. All rights reserved.
#

include ../Makefile.com
include ../../Makefile.lib.64

install: all $(ROOTLIBS64) $(ROOTLINKS64) $(ROOTCOMPATLINKS64)
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * IPMP general interfaces (PSARC/2002/615).
 */

#include <assert.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include "ipmp_impl.h"

/*
 * Allocate a handle and store it in `*handlep' upon success.  Returns an IPMP
 * error code.
 */
int
ipmp_open(ipmp_handle_t *handlep)
{
	ipmp_state_t	*statep;

	statep = malloc(sizeof (ipmp_state_t));
	if (statep == NULL)
		return (IPMP_ENOMEM);

	statep->st_fd = -1;
	statep->st_snap = NULL;
	statep->st_magic = IPMP_MAGIC;

	*handlep = statep;
	return (IPMP_SUCCESS);
}

/*
 * Destroy the IPMP handle named by `handle'.
 */
void
ipmp_close(ipmp_handle_t handle)
{
	ipmp_state_t	*statep = handle;

	/*
	 * If this assertion triggers, someone called ipmp_close() twice in
	 * a row or stomped on us.
	 */
	assert(statep->st_magic == IPMP_MAGIC);

	/*
	 * If this assertion triggers, something's gone wrong internally.
	 */
	assert(statep->st_fd == -1);

	if (statep->st_snap != NULL)
		ipmp_snap_free(statep->st_snap);

	statep->st_magic = 0;
	free(statep);
}

/*
 * Error messages; must be in the same order as the codes in <ipmp.h>
 */
static char *errmsgs[IPMP_NERR] = {
	"operation succeeded", 			/*  0 IPMP_SUCCESS	*/
	"operation failed",			/*  1 IPMP_FAILURE	*/
	"minimum failover redundancy not met",	/*  2 IPMP_EMINRED	*/
	"failback disabled",			/*  3 IPMP_EFBDISABLED  */
	"unknown IPMP data address", 		/*  4 IPMP_EUNKADDR	*/
	"invalid argument",			/*  5 IPMP_EINVAL	*/
	"out of memory",			/*  6 IPMP_ENOMEM	*/
	"cannot contact in.mpathd",		/*  7 IPMP_ENOMPATHD	*/
	"unknown IPMP group", 			/*  8 IPMP_EUNKGROUP	*/
	"interface is not using IPMP",		/*  9 IPMP_EUNKIF	*/
	"unable to communicate with in.mpathd",	/* 10 IPMP_EPROTO	*/
	"interface has duplicate hardware address"
						/* 11 IPMP_EHWADDRDUP	*/
};

/*
 * Return a string describing the IPMP error code named by `error'.
 */
const char *
ipmp_errmsg(int error)
{
	if (error >= IPMP_NERR || error < 0)
		return (dgettext(TEXT_DOMAIN, "<unknown error>"));

	if (error == IPMP_FAILURE)
		return (strerror(errno));

	return (dgettext(TEXT_DOMAIN, errmsgs[error]));
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef _IPMP_H
#define	_IPMP_H

/*
 * General IPMP-related definitions and functions.
 *
 * These interfaces may only be used within ON or after signing a contract
 * with ON.  For documentation, refer to PSARC/2002/615.
 */

#ifdef __cplusplus
extern "C" {
#endif

#include <sys/sysevent/ipmp.h>

/*
 * IPMP library error codes.
 */
enum {
	IPMP_SUCCESS,		/* operation succeeded */
	IPMP_FAILURE,		/* operation failed (check errno) */
	IPMP_EMINRED,		/* minimum failover redundancy not met */
	IPMP_EFBDISABLED,	/* failback disabled */
	IPMP_EUNKADDR,		/* unknown IPMP data address */
	IPMP_EINVAL,		/* invalid argument */
	IPMP_ENOMEM,		/* out of memory */
	IPMP_ENOMPATHD,		/* cannot contact in.mpathd */
	IPMP_EUNKGROUP,		/* unknown IPMP group */
	IPMP_EUNKIF,		/* interface is not using IPMP */
	IPMP_EPROTO,		/* unable to communicate with in.mpathd */
	IPMP_EHWADDRDUP,	/* interface has duplicate hardware address */
	IPMP_NERR		/* number of error codes */
};

typedef struct ipmp_state *ipmp_handle_t;

extern int ipmp_open(ipmp_handle_t *);
extern void ipmp_close(ipmp_handle_t);
extern const char *ipmp_errmsg(int);

#ifdef __cplusplus
}
#endif

#endif /* _IPMP_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * IPMP administrative interfaces (see PSARC/2007/272).
 */

#include <assert.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>

#include "ipmp_impl.h"
#include "ipmp_mpathd.h"
#include "ipmp_admin.h"

static int
ipmp_command(ipmp_handle_t handle, const void *req, uint_t reqsize)
{
	ipmp_state_t	*statep = (ipmp_state_t *)handle;
	mi_result_t	result;
	struct timeval	end;
	int		save_errno;
	int		retval;

	if (gettimeofday(&end, NULL) == -1)
		return (IPMP_FAILURE);
	end.tv_sec += IPMP_REQTIMEOUT;

	assert(statep->st_fd == -1);
	retval = ipmp_connect(&statep->st_fd);
	if (retval != IPMP_SUCCESS)
		return (retval);

	retval = ipmp_write(statep->st_fd, req, reqsize);
	if (retval != IPMP_SUCCESS)
		goto out;

	retval = ipmp_read(statep->st_fd, &result, sizeof (result), &end);
	if (retval != IPMP_SUCCESS)
		goto out;

	errno = result.me_sys_error;
	retval = result.me_mpathd_error;
out:
	save_errno = errno;
	(void) close(statep->st_fd);
	statep->st_fd = -1;
	errno = save_errno;
	return (retval);
}

int
ipmp_offline(ipmp_handle_t handle, const char *ifname, uint_t minred)
{
	mi_offline_t mio;

	mio.mio_command = MI_OFFLINE;
	mio.mio_min_redundancy = minred;
	(void) strlcpy(mio.mio_ifname, ifname, LIFNAMSIZ);
	return (ipmp_command(handle, &mio, sizeof (mio)));
}

int
ipmp_undo_offline(ipmp_handle_t handle, const char *ifname)
{
	mi_undo_offline_t miu;

	miu.miu_command = MI_UNDO_OFFLINE;
	(void) strlcpy(miu.miu_ifname, ifname, LIFNAMSIZ);
	return (ipmp_command(handle, &miu, sizeof (miu)));
}

int
ipmp_ping_daemon(ipmp_handle_t handle)
{
	mi_ping_t mip;

	mip.mip_command = MI_PING;
	return (ipmp_command(handle, &mip, sizeof (mip)));
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef _IPMP_ADMIN_H
#define	_IPMP_ADMIN_H

#include <ipmp.h>
#include <sys/types.h>

/*
 * IPMP administrative interfaces.
 *
 * These interfaces may only be used within ON or after signing a contract
 * with ON.  For documentation, refer to PSARC/2007/272.
 */

#ifdef __cplusplus
extern "C" {
#endif

extern int ipmp_offline(ipmp_handle_t, const char *, uint_t);
extern int ipmp_undo_offline(ipmp_handle_t, const char *);
extern int ipmp_ping_daemon(ipmp_handle_t);

#ifdef __cplusplus
}
#endif

#endif /* _IPMP_ADMIN_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef _IPMP_IMPL_H
#define	_IPMP_IMPL_H

#include <ipmp.h>
#include <ipmp_query_impl.h>

/*
 * Implementation-private definitions for the IPMP library.
 */

#ifdef __cplusplus
extern "C" {
#endif

/*
 * Private IPMP state structure.
 */
typedef struct ipmp_state {
	uint32_t	st_magic;	/* magic tag */
	int		st_fd; 		/* socket to in.mpathd */
	ipmp_snap_t	*st_snap;	/* current snapshot, if any */
} ipmp_state_t;

#define	IPMP_MAGIC	0x49504D50	/* "IPMP" */

#ifdef __cplusplus
}
#endif

#endif /* _IPMP_IMPL_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2021 Tintri by DDN, Inc. All rights reserved.
 */

/*
 * Low-level interfaces for communicating with in.mpathd(8).
 *
 * These routines are not intended for use outside of libipmp.
 */

#include <alloca.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <sys/isa_defs.h>

#include "ipmp.h"
#include "ipmp_mpathd.h"

/*
 * Connect to the multipathing daemon.  Returns an IPMP error code; upon
 * success, `fdp' points to the newly opened socket.
 */
int
ipmp_connect(int *fdp)
{
	int	fd;
	int	error;
	int	on = 1;
	int	flags;
	struct sockaddr_in sin;

	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd == -1)
		return (IPMP_FAILURE);

	/*
	 * If we have sufficient privilege, enable TCP_ANONPRIVBIND so the
	 * kernel will choose a privileged source port (since in.mpathd only
	 * accepts requests on loopback, this is sufficient for security).
	 * If not, drive on since MI_QUERY and MI_PING commands are allowed
	 * from non-privileged ports.
	 */
	(void) setsockopt(fd, IPPROTO_TCP, TCP_ANONPRIVBIND, &on, sizeof (on));

	/*
	 * Bind to a port chosen by the kernel.
	 */
	(void) memset(&sin, 0, sizeof (struct sockaddr_in));
	sin.sin_port = htons(0);
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = htonl(INADDR_ANY);

	if (bind(fd, (struct sockaddr *)&sin, sizeof (sin)) == -1)
		goto fail;

	/*
	 * Attempt to connect to in.mpathd.
	 */
	sin.sin_port = htons(MPATHD_PORT);
	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

	if (connect(fd, (struct sockaddr *)&sin, sizeof (sin)) == -1) {
		if (errno == ECONNREFUSED) {
			(void) close(fd);
			return (IPMP_ENOMPATHD);
		}
		goto fail;
	}

	/*
	 * Kick the socket into nonblocking mode.
	 */
	flags = fcntl(fd, F_GETFL, 0);
	if (flags != -1)
		(void) fcntl(fd, F_SETFL, flags | O_NONBLOCK);

	*fdp = fd;
	return (IPMP_SUCCESS);
fail:
	error = errno;
	(void) close(fd);
	errno = error;
	return (IPMP_FAILURE);
}

/*
 * Read the TLV triplet from descriptor `fd' and store its type, length and
 * value in `*typep', `*lenp', and `*valuep' respectively, before the current
 * time becomes `endtp'.  The buffer pointed to by `*valuep' will be
 * dynamically allocated.  Returns an IPMP error code.
 */
int
ipmp_readtlv(int fd, ipmp_infotype_t *typep, size_t *lenp, void **valuep,
    const struct timeval *endtp)
{
	int	retval;
	void	*value;
	uint32_t tlen;

	retval = ipmp_read(fd, typep, sizeof (*typep), endtp);
	if (retval != IPMP_SUCCESS)
		return (retval);

	retval = ipmp_read(fd, &tlen, sizeof (tlen), endtp);
	if (retval != IPMP_SUCCESS)
		return (retval);

	*lenp = tlen;

	value = malloc(*lenp);
	if (value == NULL) {
		/*
		 * Even though we cannot allocate space for the value, we
		 * still slurp it off so the input stream doesn't get left
		 * in a weird place.
		 */
		value = alloca(*lenp);
		(void) ipmp_read(fd, value, *lenp, endtp);
		return (IPMP_ENOMEM);
	}

	retval = ipmp_read(fd, value, *lenp, endtp);
	if (retval != IPMP_SUCCESS) {
		free(value);
		return (retval);
	}

	*valuep = value;
	return (IPMP_SUCCESS);
}

/*
 * Write `buflen' bytes from `buffer' to open file `fd'.  Returns IPMP_SUCCESS
 * if all requested bytes were written, or an error code if not.
 */
int
ipmp_write(int fd, const void *buffer, size_t buflen)
{
	size_t		nwritten;
	ssize_t		nbytes;
	const char	*buf = buffer;

	for (nwritten = 0; nwritten < buflen; nwritten += nbytes) {
		nbytes = write(fd, &buf[nwritten], buflen - nwritten);
		if (nbytes == -1)
			return (IPMP_FAILURE);
		if (nbytes == 0) {
			errno = EIO;
			return (IPMP_FAILURE);
		}
	}

	assert(nwritten == buflen);
	return (IPMP_SUCCESS);
}

/*
 * Write the TLV triplet named by `type', `len' and `value' to file descriptor
 * `fd'.  Returns an IPMP error code.
 */
int
ipmp_writetlv(int fd, ipmp_infotype_t type, size_t len, void *value)
{
	int	retval;
	uint32_t tlen;

#if defined(_LP64)
	if (len > UINT32_MAX)
		return (IPMP_EPROTO);
#endif

	tlen = (uint32_t)len;

	retval = ipmp_write(fd, &type, sizeof (type));
	if (retval != IPMP_SUCCESS)
		return (retval);

	retval = ipmp_write(fd, &tlen, sizeof (uint32_t));
	if (retval != IPMP_SUCCESS)
		return (retval);

	return (ipmp_write(fd, value, tlen));
}

/*
 * Attempt to read `buflen' worth of bytes from `fd' into the buffer pointed
 * to by `buf' before the current time becomes `endtp'; a `endtp' of NULL
 * means forever.  Returns an IPMP error code.
 */
int
ipmp_read(int fd, void *buffer, size_t buflen, const struct timeval *endtp)
{
	int		retval;
	int		timeleft = -1;
	struct timeval	curtime;
	ssize_t		nbytes = 0;	/* total bytes processed */
	ssize_t		prbytes;	/* per-round bytes processed */
	struct pollfd	pfd;

	while (nbytes < buflen) {
		/*
		 * If a timeout was specified, then compute the amount of time
		 * left before timing out.
		 */
		if (endtp != NULL) {
			if (gettimeofday(&curtime, NULL) == -1)
				break;

			timeleft = (endtp->tv_sec - curtime.tv_sec) * MILLISEC;
			timeleft += (endtp->tv_usec - curtime.tv_usec) / 1000;

			/*
			 * If we should've already timed out, then just
			 * have poll() return immediately.
			 */
			if (timeleft < 0)
				timeleft = 0;
		}

		pfd.fd = fd;
		pfd.events = POLLIN;

		/*
		 * Wait for data to come in or for the timeout to fire.
		 */
		retval = poll(&pfd, 1, timeleft);
		if (retval <= 0) {
			if (retval == 0)
				errno = ETIME;
			break;
		}

		/*
		 * Descriptor is ready; have at it.
		 */
		prbytes = read(fd, (caddr_t)buffer + nbytes, buflen - nbytes);
		if (prbytes <= 0) {
			if (prbytes == -1 && errno == EINTR)
				continue;
			break;
		}
		nbytes += prbytes;
	}

	return (nbytes == buflen ? IPMP_SUCCESS : IPMP_FAILURE);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2021 Tintri by DDN, Inc. All rights reserved.
 */

#ifndef	_IPMP_MPATHD_H
#define	_IPMP_MPATHD_H

/*
 * Definitions for the messaging protocol between in.mpathd and libipmp.
 * This interface is project-private to the IPMP subsystem.
 */

#include <sys/types.h>
#include <sys/socket.h>		/* needed for <net/if.h> */
#include <net/if.h>		/* needed for LIFNAMSIZ */

#ifdef	__cplusplus
extern "C" {
#endif

#define	MPATHD_PORT	5999
#define	MPATHD_PATH	"/lib/inet/in.mpathd"

/*
 * Supported commands.
 */
enum {
	MI_PING		= 0,	/* ping in.mpathd */
	MI_OFFLINE	= 1,	/* offline the interface */
	MI_UNDO_OFFLINE	= 2,	/* undo the offline */
	MI_QUERY	= 3,	/* query ipmp-related information */
	MI_NCMD			/* total number of commands */
};

/*
 * Types of information which can be requested and received (except for
 * IPMP_IFLIST and IPMP_ADDRLIST, which can only be received).
 */
typedef enum {
	IPMP_GROUPLIST	= 1,
	IPMP_GROUPINFO	= 2,
	IPMP_GROUPCNT	= 3,
	IPMP_IFINFO	= 4,
	IPMP_IFLIST	= 5,
	IPMP_IFCNT	= 6,
	IPMP_ADDRLIST	= 7,
	IPMP_ADDRINFO	= 8,
	IPMP_ADDRCNT	= 9,
	IPMP_SNAP	= 10
} ipmp_infotype_t;

/*
 * Daemon ping request.
 */
typedef struct mi_ping {
	uint32_t	mip_command;
} mi_ping_t;

/*
 * Interface offline request; `mio_ifname' is the interface to offline;
 * `mio_min_redundancy' is the minimum amount of usable interfaces after
 * offline that must exist for the operation to succeed.
 */
typedef struct mi_offline {
	uint32_t	mio_command;
	char		mio_ifname[LIFNAMSIZ];
	uint32_t	mio_min_redundancy;
} mi_offline_t;

/*
 * Interface undo-offline request; `miu_uname' is the interface to
 * undo-offline.
 */
typedef struct mi_undo_offline {
	uint32_t	miu_command;
	char		miu_ifname[LIFNAMSIZ];
} mi_undo_offline_t;

/*
 * Retrieve IPMP-related information: `miq_inforeq' is the type of information
 * being request (see above for the list of types).  If the request type is
 * IPMP_GROUPINFO, then `miq_grname' indicates the group.  If the request type
 * is IPMP_IFINFO, then `miq_ifname' indicates the interface.  If the request
 * type is IPMP_ADDRINFO then `miq_grname' indicates the group and `miq_addr'
 * indicates the address.
 */
typedef struct mi_query {
	uint32_t	miq_command;
	ipmp_infotype_t	miq_inforeq;
	union {
		char	miqu_ifname[LIFNAMSIZ];
		char	miqu_grname[LIFGRNAMSIZ];
	} miq_infodata;
	struct sockaddr_storage	miq_addr;
} mi_query_t;
#define	miq_ifname	miq_infodata.miqu_ifname
#define	miq_grname	miq_infodata.miqu_grname

/*
 * Union of all commands. Can be used to estimate the maximum buffer size
 * requirement for receiving any command.
 */
union mi_commands {
	uint32_t		mi_command;
	mi_ping_t		mi_pcmd;
	mi_offline_t		mi_ocmd;
	mi_undo_offline_t	mi_ucmd;
	mi_query_t		mi_qcmd;
};

/*
 * Result structure returned by in.mpathd.
 */
typedef struct mi_result {
	uint32_t me_sys_error;			/* System error (errno.h) */
	uint32_t me_mpathd_error;		/* Mpathd error */
} mi_result_t;

#define	IPMP_REQTIMEOUT	5			/* seconds */

extern int ipmp_connect(int *);
extern int ipmp_read(int, void *, size_t, const struct timeval *);
extern int ipmp_write(int, const void *, size_t);
extern int ipmp_writetlv(int, ipmp_infotype_t, size_t, void *);
extern int ipmp_readtlv(int, ipmp_infotype_t *, size_t *, void **,
    const struct timeval *);

#ifdef	__cplusplus
}
#endif

#endif	/* _IPMP_MPATHD_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2021 Tintri by DDN, Inc. All rights reserved.
 */

/*
 * IPMP query interfaces (see PSARC/2002/615 and PSARC/2007/272).
 */

#include <assert.h>
#include <errno.h>
#include <libinetutil.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

#include "ipmp_impl.h"
#include "ipmp_mpathd.h"
#include "ipmp_query_impl.h"

static ipmp_ifinfo_t	*ipmp_ifinfo_clone(ipmp_ifinfo_t *);
static ipmp_addrinfo_t	*ipmp_addrinfo_clone(ipmp_addrinfo_t *);
static ipmp_addrlist_t	*ipmp_addrlist_clone(ipmp_addrlist_t *);
static ipmp_grouplist_t	*ipmp_grouplist_clone(ipmp_grouplist_t *);
static ipmp_groupinfo_t	*ipmp_groupinfo_clone(ipmp_groupinfo_t *);
static ipmp_iflist_t	*ipmp_iflist_create(uint_t, char (*)[LIFNAMSIZ]);
static void		ipmp_freeiflist(ipmp_iflist_t *);
static ipmp_addrlist_t *ipmp_addrlist_create(uint_t, struct sockaddr_storage *);
static void		ipmp_freeaddrlist(ipmp_addrlist_t *);
static ipmp_groupinfo_t *ipmp_snap_getgroupinfo(ipmp_snap_t *, const char *);
static ipmp_ifinfo_t	*ipmp_snap_getifinfo(ipmp_snap_t *, const char *);
static ipmp_addrinfo_t  *ipmp_snap_getaddrinfo(ipmp_snap_t *, const char *,
			    struct sockaddr_storage *);
static int		ipmp_snap_take(ipmp_state_t *, ipmp_snap_t **);
static boolean_t	ipmp_checktlv(ipmp_infotype_t, size_t, void *);
static int		ipmp_querydone(ipmp_state_t *, int);

/*
 * Using `statep', send a query request for `type' to in.mpathd, and if
 * necessary wait until at least `endtp' for a response.  Returns an IPMP
 * error code.  If successful, the caller may then read additional query
 * information through ipmp_readinfo(), and must eventually call
 * ipmp_querydone() to complete the query operation.  Only one query may be
 * outstanding on a given `statep' at a time.
 */
static int
ipmp_sendquery(ipmp_state_t *statep, ipmp_infotype_t type, const char *name,
    const void *addr, struct timeval *endtp)
{
	mi_query_t	query;
	mi_result_t	result;
	int		retval;

	query.miq_command = MI_QUERY;
	query.miq_inforeq = type;

	switch (type) {
	case IPMP_ADDRINFO:
		(void) strlcpy(query.miq_grname, name, LIFGRNAMSIZ);
		query.miq_addr = *(struct sockaddr_storage *)addr;
		break;

	case IPMP_GROUPINFO:
		(void) strlcpy(query.miq_grname, name, LIFGRNAMSIZ);
		break;

	case IPMP_IFINFO:
		(void) strlcpy(query.miq_ifname, name, LIFNAMSIZ);
		break;

	case IPMP_GROUPLIST:
	case IPMP_SNAP:
		break;

	default:
		assert(0);
	}

	if (gettimeofday(endtp, NULL) == -1)
		return (IPMP_FAILURE);

	endtp->tv_sec += IPMP_REQTIMEOUT;

	assert(statep->st_fd == -1);
	retval = ipmp_connect(&statep->st_fd);
	if (retval != IPMP_SUCCESS)
		return (retval);

	retval = ipmp_write(statep->st_fd, &query, sizeof (query));
	if (retval != IPMP_SUCCESS)
		return (ipmp_querydone(statep, retval));

	retval = ipmp_read(statep->st_fd, &result, sizeof (result), endtp);
	if (retval != IPMP_SUCCESS)
		return (ipmp_querydone(statep, retval));

	if (result.me_mpathd_error != IPMP_SUCCESS)
		return (ipmp_querydone(statep, result.me_mpathd_error));

	return (IPMP_SUCCESS);
}

/*
 * Using `statep', read a query response of type `infotype' into a dynamically
 * allocated buffer pointed to by `*infop', before the current time becomes
 * `endtp'.  Returns an IPMP error code.
 */
static int
ipmp_readinfo(ipmp_state_t *statep, ipmp_infotype_t infotype, void **infop,
    const struct timeval *endtp)
{
	int		retval;
	size_t		len;
	ipmp_infotype_t	type;

	retval = ipmp_readtlv(statep->st_fd, &type, &len, infop, endtp);
	if (retval != IPMP_SUCCESS)
		return (retval);

	if (type != infotype || !ipmp_checktlv(type, len, *infop)) {
		free(*infop);
		return (IPMP_EPROTO);
	}

	return (IPMP_SUCCESS);
}

/*
 * Using `statep', read in the remaining IPMP group information TLVs from
 * in.mpathd into `grinfop' before the current time becomes `endtp'.  Returns
 * an IPMP error code.  On failure, `grinfop' will have its original contents.
 */
static int
ipmp_readgroupinfo_lists(ipmp_state_t *statep, ipmp_groupinfo_t *grinfop,
    const struct timeval *endtp)
{
	int retval;
	ipmp_iflist_t *iflistp;
	ipmp_addrlist_t *adlistp;

	retval = ipmp_readinfo(statep, IPMP_IFLIST, (void **)&iflistp, endtp);
	if (retval != IPMP_SUCCESS)
		return (retval);

	retval = ipmp_readinfo(statep, IPMP_ADDRLIST, (void **)&adlistp, endtp);
	if (retval != IPMP_SUCCESS) {
		ipmp_freeiflist(iflistp);
		return (retval);
	}

	grinfop->gr_iflistp = iflistp;
	grinfop->gr_adlistp = adlistp;
	return (IPMP_SUCCESS);
}

/*
 * Using `statep', read in the remaining IPMP interface information TLVs from
 * in.mpathd into `ifinfop' before the current time becomes `endtp'.  Returns
 * an IPMP error code.  On failure, `ifinfop' will have its original contents.
 */
static int
ipmp_readifinfo_lists(ipmp_state_t *statep, ipmp_ifinfo_t *ifinfop,
    const struct timeval *endtp)
{
	int retval;
	ipmp_addrlist_t *tlist4p, *tlist6p;

	retval = ipmp_readinfo(statep, IPMP_ADDRLIST, (void **)&tlist4p, endtp);
	if (retval != IPMP_SUCCESS)
		return (retval);

	retval = ipmp_readinfo(statep, IPMP_ADDRLIST, (void **)&tlist6p, endtp);
	if (retval != IPMP_SUCCESS) {
		ipmp_freeaddrlist(tlist4p);
		return (retval);
	}

	ifinfop->if_targinfo4.it_targlistp = tlist4p;
	ifinfop->if_targinfo6.it_targlistp = tlist6p;
	return (IPMP_SUCCESS);
}

/*
 * Complete the query operation started in ipmp_sendquery().  The interface is
 * designed to be easy to use in the `return' statement of a function, and
 * thus returns the passed in `retval' and preserves `errno'.
 */
static int
ipmp_querydone(ipmp_state_t *statep, int retval)
{
	int error = errno;

	(void) close(statep->st_fd);
	statep->st_fd = -1;
	errno = error;
	return (retval);
}

/*
 * Using `handle', get the group list and store the results in a dynamically
 * allocated buffer pointed to by `*grlistpp'.  Returns an IPMP error code.
 */
int
ipmp_getgrouplist(ipmp_handle_t handle, ipmp_grouplist_t **grlistpp)
{
	ipmp_state_t	*statep = handle;
	struct timeval	end;
	int		retval;

	if (statep->st_snap != NULL) {
		*grlistpp = ipmp_grouplist_clone(statep->st_snap->sn_grlistp);
		return (*grlistpp != NULL ? IPMP_SUCCESS : IPMP_ENOMEM);
	}

	retval = ipmp_sendquery(statep, IPMP_GROUPLIST, NULL, NULL, &end);
	if (retval != IPMP_SUCCESS)
		return (retval);

	retval = ipmp_readinfo(statep, IPMP_GROUPLIST, (void **)grlistpp, &end);
	return (ipmp_querydone(statep, retval));
}

/*
 * Free the group list pointed to by `grlistp'.
 */
void
ipmp_freegrouplist(ipmp_grouplist_t *grlistp)
{
	free(grlistp);
}

/*
 * Convert a ipmp_groupinfo_xfer_t used for communication with in.mpathd
 * into a newly allocated ipmp_groupinfo_t. Free the ipmp_groupinfo_xfer_t,
 * regardless of failure.
 */
static ipmp_groupinfo_t *
ipmp_convertgroupinfo(ipmp_groupinfo_xfer_t *grxferp)
{
	ipmp_groupinfo_t *grinfop;

	grinfop = calloc(1, sizeof (ipmp_groupinfo_t));
	if (grinfop != NULL) {
		memcpy(grinfop->gr_name, grxferp->grx_name,
		    sizeof (grinfop->gr_name));
		grinfop->gr_sig = grxferp->grx_sig;
		grinfop->gr_state = grxferp->grx_state;
		memcpy(grinfop->gr_ifname, grxferp->grx_ifname,
		    sizeof (grinfop->gr_ifname));
		memcpy(grinfop->gr_m4ifname, grxferp->grx_m4ifname,
		    sizeof (grinfop->gr_m4ifname));
		memcpy(grinfop->gr_m6ifname, grxferp->grx_m6ifname,
		    sizeof (grinfop->gr_m6ifname));
		memcpy(grinfop->gr_bcifname, grxferp->grx_bcifname,
		    sizeof (grinfop->gr_bcifname));
		grinfop->gr_fdt = grxferp->grx_fdt;
	}

	free(grxferp);

	return (grinfop);
}
/*
 * Using `handle', get the group information associated with group `name' and
 * store the results in a dynamically allocated buffer pointed to by
 * `*grinfopp'.  Returns an IPMP error code.
 */
int
ipmp_getgroupinfo(ipmp_handle_t handle, const char *name,
    ipmp_groupinfo_t **grinfopp)
{
	ipmp_state_t	*statep = handle;
	int		retval;
	struct timeval	end;
	ipmp_groupinfo_t *grinfop;
	ipmp_groupinfo_xfer_t *grxferp;

	if (statep->st_snap != NULL) {
		grinfop = ipmp_snap_getgroupinfo(statep->st_snap, name);
		if (grinfop == NULL)
			return (IPMP_EUNKGROUP);

		*grinfopp = ipmp_groupinfo_clone(grinfop);
		return (*grinfopp != NULL ? IPMP_SUCCESS : IPMP_ENOMEM);
	}

	retval = ipmp_sendquery(statep, IPMP_GROUPINFO, name, NULL, &end);
	if (retval != IPMP_SUCCESS)
		return (retval);

	retval = ipmp_readinfo(statep, IPMP_GROUPINFO, (void **)&grxferp, &end);
	if (retval != IPMP_SUCCESS)
		return (ipmp_querydone(statep, retval));

	*grinfopp = ipmp_convertgroupinfo(grxferp);
	if (*grinfopp == NULL)
		return (ipmp_querydone(statep, IPMP_ENOMEM));

	retval = ipmp_readgroupinfo_lists(statep, *grinfopp, &end);
	if (retval != IPMP_SUCCESS)
		free(*grinfopp);

	return (ipmp_querydone(statep, retval));
}

/*
 * Free the group information pointed to by `grinfop'.
 */
void
ipmp_freegroupinfo(ipmp_groupinfo_t *grinfop)
{
	ipmp_freeaddrlist(grinfop->gr_adlistp);
	ipmp_freeiflist(grinfop->gr_iflistp);
	free(grinfop);
}

/*
 * Convert a ipmp_ifinfo_xfer_t used for communication with in.mpathd
 * into a newly allocated ipmp_ifinfo_t. Free the ipmp_ifinfo_xfer_t,
 * regardless of failure.
 */
static ipmp_ifinfo_t *
ipmp_convertifinfo(ipmp_ifinfo_xfer_t *ifxferp)
{
	ipmp_ifinfo_t *ifinfop;

	ifinfop = calloc(1, sizeof (ipmp_ifinfo_t));
	if (ifinfop != NULL) {
		memcpy(ifinfop->if_name, ifxferp->ifx_name,
		    sizeof (ifinfop->if_name));
		memcpy(ifinfop->if_group, ifxferp->ifx_group,
		    sizeof (ifinfop->if_group));
		ifinfop->if_state = ifxferp->ifx_state;
		ifinfop->if_type = ifxferp->ifx_type;
		ifinfop->if_linkstate = ifxferp->ifx_linkstate;
		ifinfop->if_probestate = ifxferp->ifx_probestate;
		ifinfop->if_flags = ifxferp->ifx_flags;
		memcpy(ifinfop->if_targinfo4.it_name,
		    ifxferp->ifx_targinfo4.itx_name,
		    sizeof (ifinfop->if_targinfo4.it_name));
		ifinfop->if_targinfo4.it_testaddr =
		    ifxferp->ifx_targinfo4.itx_testaddr;
		ifinfop->if_targinfo4.it_targmode =
		    ifxferp->ifx_targinfo4.itx_targmode;
		memcpy(ifinfop->if_targinfo6.it_name,
		    ifxferp->ifx_targinfo6.itx_name,
		    sizeof (ifinfop->if_targinfo6.it_name));
		ifinfop->if_targinfo6.it_testaddr =
		    ifxferp->ifx_targinfo6.itx_testaddr;
		ifinfop->if_targinfo6.it_targmode =
		    ifxferp->ifx_targinfo6.itx_targmode;
	}

	free(ifxferp);

	return (ifinfop);
}

/*
 * Using `handle', get the interface information associated with interface
 * `name' and store the results in a dynamically allocated buffer pointed to
 * by `*ifinfopp'.  Returns an IPMP error code.
 */
int
ipmp_getifinfo(ipmp_handle_t handle, const char *name, ipmp_ifinfo_t **ifinfopp)
{
	ipmp_state_t	*statep = handle;
	ipmp_ifinfo_t	*ifinfop;
	ipmp_ifinfo_xfer_t *ifxferp;
	int		retval;
	struct timeval	end;

	if (statep->st_snap != NULL) {
		ifinfop = ipmp_snap_getifinfo(statep->st_snap, name);
		if (ifinfop == NULL)
			return (IPMP_EUNKIF);

		*ifinfopp = ipmp_ifinfo_clone(ifinfop);
		return (*ifinfopp != NULL ? IPMP_SUCCESS : IPMP_ENOMEM);
	}

	retval = ipmp_sendquery(statep, IPMP_IFINFO, name, NULL, &end);
	if (retval != IPMP_SUCCESS)
		return (retval);

	retval = ipmp_readinfo(statep, IPMP_IFINFO, (void **)&ifxferp, &end);
	if (retval != IPMP_SUCCESS)
		return (ipmp_querydone(statep, retval));

	*ifinfopp = ipmp_convertifinfo(ifxferp);
	if (*ifinfopp == NULL)
		return (ipmp_querydone(statep, IPMP_ENOMEM));

	retval = ipmp_readifinfo_lists(statep, *ifinfopp, &end);
	if (retval != IPMP_SUCCESS)
		free(*ifinfopp);

	return (ipmp_querydone(statep, retval));
}

/*
 * Free the interface information pointed to by `ifinfop'.
 */
void
ipmp_freeifinfo(ipmp_ifinfo_t *ifinfop)
{
	ipmp_freeaddrlist(ifinfop->if_targinfo4.it_targlistp);
	ipmp_freeaddrlist(ifinfop->if_targinfo6.it_targlistp);
	free(ifinfop);
}

/*
 * Using `handle', get the address information associated with address `addrp'
 * on group `grname' and store the results in a dynamically allocated buffer
 * pointed to by `*adinfopp'.  Returns an IPMP error code.
 */
int
ipmp_getaddrinfo(ipmp_handle_t handle, const char *grname,
    struct sockaddr_storage *addrp, ipmp_addrinfo_t **adinfopp)
{
	ipmp_state_t	*statep = handle;
	ipmp_addrinfo_t	*adinfop;
	int		retval;
	struct timeval	end;

	if (statep->st_snap != NULL) {
		adinfop = ipmp_snap_getaddrinfo(statep->st_snap, grname, addrp);
		if (adinfop == NULL)
			return (IPMP_EUNKADDR);

		*adinfopp = ipmp_addrinfo_clone(adinfop);
		return (*adinfopp != NULL ? IPMP_SUCCESS : IPMP_ENOMEM);
	}

	retval = ipmp_sendquery(statep, IPMP_ADDRINFO, grname, addrp, &end);
	if (retval != IPMP_SUCCESS)
		return (retval);

	retval = ipmp_readinfo(statep, IPMP_ADDRINFO, (void **)adinfopp, &end);
	return (ipmp_querydone(statep, retval));
}

/*
 * Free the address information pointed to by `adinfop'.
 */
void
ipmp_freeaddrinfo(ipmp_addrinfo_t *adinfop)
{
	free(adinfop);
}

/*
 * Check if `buf' has a NUL byte in its first `bufsize' bytes.
 */
static boolean_t
hasnulbyte(const char *buf, size_t bufsize)
{
	while (bufsize-- > 0) {
		if (buf[bufsize] == '\0')
			return (B_TRUE);
	}
	return (B_FALSE);
}

/*
 * Check that the TLV triplet named by `type', `len' and `value' is correctly
 * formed.
 */
static boolean_t
ipmp_checktlv(ipmp_infotype_t type, size_t len, void *value)
{
	ipmp_iflist_t		*iflistp;
	ipmp_ifinfo_xfer_t	*ifxferp;
	ipmp_grouplist_t	*grlistp;
	ipmp_groupinfo_xfer_t	*grxferp;
	ipmp_addrlist_t		*adlistp;
	unsigned int		i;

	switch (type) {
	case IPMP_ADDRINFO:
		if (len != sizeof (ipmp_addrinfo_t))
			return (B_FALSE);
		break;

	case IPMP_ADDRLIST:
		adlistp = (ipmp_addrlist_t *)value;
		if (len < IPMP_ADDRLIST_SIZE(0) ||
		    len < IPMP_ADDRLIST_SIZE(adlistp->al_naddr))
			return (B_FALSE);
		break;

	case IPMP_IFLIST:
		iflistp = (ipmp_iflist_t *)value;
		if (len < IPMP_IFLIST_SIZE(0) ||
		    len < IPMP_IFLIST_SIZE(iflistp->il_nif))
			return (B_FALSE);

		for (i = 0; i < iflistp->il_nif; i++)
			if (!hasnulbyte(iflistp->il_ifs[i], LIFNAMSIZ))
				return (B_FALSE);
		break;

	case IPMP_IFINFO:
		ifxferp = (ipmp_ifinfo_xfer_t *)value;
		if (len != sizeof (ipmp_ifinfo_xfer_t))
			return (B_FALSE);

		if (!hasnulbyte(ifxferp->ifx_name, LIFNAMSIZ) ||
		    !hasnulbyte(ifxferp->ifx_group, LIFGRNAMSIZ))
			return (B_FALSE);
		break;

	case IPMP_GROUPLIST:
		grlistp = (ipmp_grouplist_t *)value;
		if (len < IPMP_GROUPLIST_SIZE(0) ||
		    len < IPMP_GROUPLIST_SIZE(grlistp->gl_ngroup))
			return (B_FALSE);

		for (i = 0; i < grlistp->gl_ngroup; i++)
			if (!hasnulbyte(grlistp->gl_groups[i], LIFGRNAMSIZ))
				return (B_FALSE);
		break;

	case IPMP_GROUPINFO:
		grxferp = (ipmp_groupinfo_xfer_t *)value;
		if (len != sizeof (ipmp_groupinfo_xfer_t))
			return (B_FALSE);

		if (!hasnulbyte(grxferp->grx_name, LIFGRNAMSIZ))
			return (B_FALSE);
		break;

	case IPMP_ADDRCNT:
	case IPMP_GROUPCNT:
	case IPMP_IFCNT:
		if (len != sizeof (uint32_t))
			return (B_FALSE);
		break;

	default:
		return (B_FALSE);
	}

	return (B_TRUE);
}

/*
 * Create a group list; arguments match ipmp_grouplist_t fields.  Returns a
 * pointer to the new group list on success, or NULL on failure.
 */
ipmp_grouplist_t *
ipmp_grouplist_create(uint64_t sig, unsigned int ngroup,
    char (*groups)[LIFGRNAMSIZ])
{
	unsigned int i;
	ipmp_grouplist_t *grlistp;

	grlistp = malloc(IPMP_GROUPLIST_SIZE(ngroup));
	if (grlistp == NULL)
		return (NULL);

	grlistp->gl_sig = sig;
	grlistp->gl_ngroup = ngroup;
	for (i = 0; i < ngroup; i++)
		(void) strlcpy(grlistp->gl_groups[i], groups[i], LIFGRNAMSIZ);

	return (grlistp);
}

/*
 * Clone the group list named by `grlistp'.  Returns a pointer to the clone on
 * success, or NULL on failure.
 */
ipmp_grouplist_t *
ipmp_grouplist_clone(ipmp_grouplist_t *grlistp)
{
	return (ipmp_grouplist_create(grlistp->gl_sig, grlistp->gl_ngroup,
	    grlistp->gl_groups));
}

/*
 * Create target information; arguments match ipmp_targinfo_t fields.  Returns
 * a pointer to the new target info on success, or NULL on failure.
 */
ipmp_targinfo_t *
ipmp_targinfo_create(const char *name, struct sockaddr_storage *testaddrp,
    ipmp_if_targmode_t targmode, uint_t ntarg, struct sockaddr_storage *targs)
{
	ipmp_targinfo_t *targinfop;

	targinfop = malloc(sizeof (ipmp_targinfo_t));
	if (targinfop == NULL)
		return (NULL);

	targinfop->it_testaddr = *testaddrp;
	targinfop->it_targmode = targmode;
	targinfop->it_targlistp = ipmp_addrlist_create(ntarg, targs);
	if (targinfop->it_targlistp == NULL) {
		ipmp_freetarginfo(targinfop);
		return (NULL);
	}
	(void) strlcpy(targinfop->it_name, name, LIFNAMSIZ);

	return (targinfop);
}

/*
 * Free the target information pointed to by `targinfop'.
 */
void
ipmp_freetarginfo(ipmp_targinfo_t *targinfop)
{
	free(targinfop->it_targlistp);
	free(targinfop);
}

/*
 * Create an interface list; arguments match ipmp_iflist_t fields.  Returns a
 * pointer to the new interface list on success, or NULL on failure.
 */
static ipmp_iflist_t *
ipmp_iflist_create(uint_t nif, char (*ifs)[LIFNAMSIZ])
{
	unsigned int i;
	ipmp_iflist_t *iflistp;

	iflistp = malloc(IPMP_IFLIST_SIZE(nif));
	if (iflistp == NULL)
		return (NULL);

	iflistp->il_nif = nif;
	for (i = 0; i < nif; i++)
		(void) strlcpy(iflistp->il_ifs[i], ifs[i], LIFNAMSIZ);

	return (iflistp);
}

/*
 * Free the interface list pointed to by `iflistp'.
 */
static void
ipmp_freeiflist(ipmp_iflist_t *iflistp)
{
	free(iflistp);
}

/*
 * Create an interface; arguments match ipmp_ifinfo_t fields.  Returns a
 * pointer to the new interface on success, or NULL on failure.
 */
ipmp_ifinfo_t *
ipmp_ifinfo_create(const char *name, const char *group, ipmp_if_state_t state,
    ipmp_if_type_t type, ipmp_if_linkstate_t linkstate,
    ipmp_if_probestate_t probestate, ipmp_if_flags_t flags,
    ipmp_targinfo_t *targinfo4p, ipmp_targinfo_t *targinfo6p)
{
	ipmp_ifinfo_t *ifinfop;

	ifinfop = malloc(sizeof (ipmp_ifinfo_t));
	if (ifinfop == NULL)
		return (NULL);

	(void) strlcpy(ifinfop->if_name, name, LIFNAMSIZ);
	(void) strlcpy(ifinfop->if_group, group, LIFGRNAMSIZ);

	ifinfop->if_state	= state;
	ifinfop->if_type	= type;
	ifinfop->if_linkstate	= linkstate;
	ifinfop->if_probestate	= probestate;
	ifinfop->if_flags	= flags;
	ifinfop->if_targinfo4	= *targinfo4p;
	ifinfop->if_targinfo6	= *targinfo6p;

	ifinfop->if_targinfo4.it_targlistp =
	    ipmp_addrlist_clone(targinfo4p->it_targlistp);
	ifinfop->if_targinfo6.it_targlistp =
	    ipmp_addrlist_clone(targinfo6p->it_targlistp);

	if (ifinfop->if_targinfo4.it_targlistp == NULL ||
	    ifinfop->if_targinfo6.it_targlistp == NULL) {
		ipmp_freeifinfo(ifinfop);
		return (NULL);
	}

	return (ifinfop);
}

/*
 * Clone the interface information named by `ifinfop'.  Returns a pointer to
 * the clone on success, or NULL on failure.
 */
ipmp_ifinfo_t *
ipmp_ifinfo_clone(ipmp_ifinfo_t *ifinfop)
{
	return (ipmp_ifinfo_create(ifinfop->if_name, ifinfop->if_group,
	    ifinfop->if_state, ifinfop->if_type, ifinfop->if_linkstate,
	    ifinfop->if_probestate, ifinfop->if_flags, &ifinfop->if_targinfo4,
	    &ifinfop->if_targinfo6));
}

/*
 * Create a group; arguments match ipmp_groupinfo_t fields.  Returns a pointer
 * to the new group on success, or NULL on failure.
 */
ipmp_groupinfo_t *
ipmp_groupinfo_create(const char *name, uint64_t sig, uint_t fdt,
    ipmp_group_state_t state, uint_t nif, char (*ifs)[LIFNAMSIZ],
    const char *grifname, const char *m4ifname, const char *m6ifname,
    const char *bcifname, uint_t naddr, struct sockaddr_storage *addrs)
{
	ipmp_groupinfo_t *grinfop;

	grinfop = malloc(sizeof (ipmp_groupinfo_t));
	if (grinfop == NULL)
		return (NULL);

	grinfop->gr_sig	= sig;
	grinfop->gr_fdt = fdt;
	grinfop->gr_state = state;
	grinfop->gr_iflistp = ipmp_iflist_create(nif, ifs);
	grinfop->gr_adlistp = ipmp_addrlist_create(naddr, addrs);
	if (grinfop->gr_iflistp == NULL || grinfop->gr_adlistp == NULL) {
		ipmp_freegroupinfo(grinfop);
		return (NULL);
	}
	(void) strlcpy(grinfop->gr_name, name, LIFGRNAMSIZ);
	(void) strlcpy(grinfop->gr_ifname, grifname, LIFNAMSIZ);
	(void) strlcpy(grinfop->gr_m4ifname, m4ifname, LIFNAMSIZ);
	(void) strlcpy(grinfop->gr_m6ifname, m6ifname, LIFNAMSIZ);
	(void) strlcpy(grinfop->gr_bcifname, bcifname, LIFNAMSIZ);

	return (grinfop);
}

/*
 * Clone the group information named by `grinfop'.  Returns a pointer to
 * the clone on success, or NULL on failure.
 */
ipmp_groupinfo_t *
ipmp_groupinfo_clone(ipmp_groupinfo_t *grinfop)
{
	ipmp_addrlist_t *adlistp = grinfop->gr_adlistp;

	return (ipmp_groupinfo_create(grinfop->gr_name, grinfop->gr_sig,
	    grinfop->gr_fdt, grinfop->gr_state, grinfop->gr_iflistp->il_nif,
	    grinfop->gr_iflistp->il_ifs, grinfop->gr_ifname,
	    grinfop->gr_m4ifname, grinfop->gr_m6ifname, grinfop->gr_bcifname,
	    adlistp->al_naddr, adlistp->al_addrs));
}

/*
 * Create an address list; arguments match ipmp_addrlist_t fields.  Returns
 * a pointer to the new address list on success, or NULL on failure.
 */
static ipmp_addrlist_t *
ipmp_addrlist_create(uint_t naddr, struct sockaddr_storage *addrs)
{
	unsigned int i;
	ipmp_addrlist_t *adlistp;

	adlistp = malloc(IPMP_ADDRLIST_SIZE(naddr));
	if (adlistp == NULL)
		return (NULL);

	adlistp->al_naddr = naddr;
	for (i = 0; i < naddr; i++)
		adlistp->al_addrs[i] = addrs[i];

	return (adlistp);
}

/*
 * Clone the address list named by `adlistp'.  Returns a pointer to the clone
 * on success, or NULL on failure.
 */
static ipmp_addrlist_t *
ipmp_addrlist_clone(ipmp_addrlist_t *adlistp)
{
	return (ipmp_addrlist_create(adlistp->al_naddr, adlistp->al_addrs));
}

/*
 * Free the address list pointed to by `adlistp'.
 */
static void
ipmp_freeaddrlist(ipmp_addrlist_t *adlistp)
{
	free(adlistp);
}

/*
 * Create an address; arguments match ipmp_addrinfo_t fields.  Returns a
 * pointer to the new address on success, or NULL on failure.
 */
ipmp_addrinfo_t *
ipmp_addrinfo_create(struct sockaddr_storage *addrp, ipmp_addr_state_t state,
    const char *group, const char *binding)
{
	ipmp_addrinfo_t *adinfop;

	adinfop = malloc(sizeof (ipmp_addrinfo_t));
	if (adinfop == NULL)
		return (NULL);

	adinfop->ad_addr = *addrp;
	adinfop->ad_state = state;
	(void) strlcpy(adinfop->ad_group, group, LIFGRNAMSIZ);
	(void) strlcpy(adinfop->ad_binding, binding, LIFNAMSIZ);

	return (adinfop);
}

/*
 * Clone the address information named by `adinfop'.  Returns a pointer to
 * the clone on success, or NULL on failure.
 */
ipmp_addrinfo_t *
ipmp_addrinfo_clone(ipmp_addrinfo_t *adinfop)
{
	return (ipmp_addrinfo_create(&adinfop->ad_addr, adinfop->ad_state,
	    adinfop->ad_group, adinfop->ad_binding));
}

/*
 * Set the query context associated with `handle' to `qcontext', which must be
 * either IPMP_QCONTEXT_LIVE or IPMP_QCONTEXT_SNAP.  Upon success, any
 * previous snapshot associated with `handle' is discarded.  Returns an IPMP
 * error code.
 */
int
ipmp_setqcontext(ipmp_handle_t handle, ipmp_qcontext_t qcontext)
{
	ipmp_state_t	*statep = handle;
	ipmp_snap_t	*snap;
	int		retval;

	switch (qcontext) {
	case IPMP_QCONTEXT_LIVE:
		snap = NULL;
		break;

	case IPMP_QCONTEXT_SNAP:
		retval = ipmp_snap_take(statep, &snap);
		if (retval != IPMP_SUCCESS)
			return (retval);
		break;

	default:
		return (IPMP_EINVAL);
	}

	if (statep->st_snap != NULL)
		ipmp_snap_free(statep->st_snap);
	statep->st_snap = snap;

	return (IPMP_SUCCESS);
}

/*
 * Create an empty snapshot.  Returns a pointer to the snapshot on success,
 * or NULL on failure.
 */
ipmp_snap_t *
ipmp_snap_create(void)
{
	ipmp_snap_t *snap;

	snap = malloc(sizeof (ipmp_snap_t));
	if (snap == NULL)
		return (NULL);

	snap->sn_grlistp = NULL;
	snap->sn_grinfolistp = NULL;
	snap->sn_ifinfolistp = NULL;
	snap->sn_adinfolistp = NULL;
	snap->sn_ngroup = 0;
	snap->sn_nif = 0;
	snap->sn_naddr = 0;

	return (snap);
}

/*
 * Free all of the resources associated with snapshot `snap'.
 */
void
ipmp_snap_free(ipmp_snap_t *snap)
{
	ipmp_ifinfolist_t	*iflp, *ifnext;
	ipmp_addrinfolist_t	*adlp, *adnext;
	ipmp_groupinfolist_t	*grlp, *grnext;

	ipmp_freegrouplist(snap->sn_grlistp);

	for (grlp = snap->sn_grinfolistp; grlp != NULL; grlp = grnext) {
		grnext = grlp->grl_next;
		ipmp_freegroupinfo(grlp->grl_grinfop);
		free(grlp);
	}

	for (iflp = snap->sn_ifinfolistp; iflp != NULL; iflp = ifnext) {
		ifnext = iflp->ifl_next;
		ipmp_freeifinfo(iflp->ifl_ifinfop);
		free(iflp);
	}

	for (adlp = snap->sn_adinfolistp; adlp != NULL; adlp = adnext) {
		adnext = adlp->adl_next;
		ipmp_freeaddrinfo(adlp->adl_adinfop);
		free(adlp);
	}

	free(snap);
}

/*
 * Add the group information in `grinfop' to the snapshot named by `snap'.
 * Returns an IPMP error code.
 */
int
ipmp_snap_addgroupinfo(ipmp_snap_t *snap, ipmp_groupinfo_t *grinfop)
{
	ipmp_groupinfolist_t *grlp;

	/*
	 * If the information for this group is already in the snapshot,
	 * in.mpathd is broken.
	 */
	if (ipmp_snap_getgroupinfo(snap, grinfop->gr_name) != NULL)
		return (IPMP_EPROTO);

	grlp = malloc(sizeof (ipmp_groupinfolist_t));
	if (grlp == NULL)
		return (IPMP_ENOMEM);

	grlp->grl_grinfop = grinfop;
	grlp->grl_next = snap->sn_grinfolistp;
	snap->sn_grinfolistp = grlp;
	snap->sn_ngroup++;

	return (IPMP_SUCCESS);
}

/*
 * Add the interface information in `ifinfop' to the snapshot named by `snap'.
 * Returns an IPMP error code.
 */
int
ipmp_snap_addifinfo(ipmp_snap_t *snap, ipmp_ifinfo_t *ifinfop)
{
	ipmp_ifinfolist_t *iflp;

	/*
	 * If the information for this interface is already in the snapshot,
	 * in.mpathd is broken.
	 */
	if (ipmp_snap_getifinfo(snap, ifinfop->if_name) != NULL)
		return (IPMP_EPROTO);

	iflp = malloc(sizeof (ipmp_ifinfolist_t));
	if (iflp == NULL)
		return (IPMP_ENOMEM);

	iflp->ifl_ifinfop = ifinfop;
	iflp->ifl_next = snap->sn_ifinfolistp;
	snap->sn_ifinfolistp = iflp;
	snap->sn_nif++;

	return (IPMP_SUCCESS);
}

/*
 * Add the address information in `adinfop' to the snapshot named by `snap'.
 * Returns an IPMP error code.
 */
int
ipmp_snap_addaddrinfo(ipmp_snap_t *snap, ipmp_addrinfo_t *adinfop)
{
	ipmp_addrinfolist_t *adlp;

	/*
	 * Any duplicate addresses should've already been weeded by in.mpathd.
	 */
	if (ipmp_snap_getaddrinfo(snap, adinfop->ad_group,
	    &adinfop->ad_addr) != NULL)
		return (IPMP_EPROTO);

	adlp = malloc(sizeof (ipmp_addrinfolist_t));
	if (adlp == NULL)
		return (IPMP_ENOMEM);

	adlp->adl_adinfop = adinfop;
	adlp->adl_next = snap->sn_adinfolistp;
	snap->sn_adinfolistp = adlp;
	snap->sn_naddr++;

	return (IPMP_SUCCESS);
}

/*
 * Retrieve the information for the group `name' in snapshot `snap'.
 * Returns a pointer to the group information on success, or NULL on failure.
 */
static ipmp_groupinfo_t *
ipmp_snap_getgroupinfo(ipmp_snap_t *snap, const char *name)
{
	ipmp_groupinfolist_t *grlp;

	for (grlp = snap->sn_grinfolistp; grlp != NULL; grlp = grlp->grl_next) {
		if (strcmp(grlp->grl_grinfop->gr_name, name) == 0)
			break;
	}

	return (grlp != NULL ? grlp->grl_grinfop : NULL);
}

/*
 * Retrieve the information for the interface `name' in snapshot `snap'.
 * Returns a pointer to the interface information on success, or NULL on
 * failure.
 */
static ipmp_ifinfo_t *
ipmp_snap_getifinfo(ipmp_snap_t *snap, const char *name)
{
	ipmp_ifinfolist_t *iflp;

	for (iflp = snap->sn_ifinfolistp; iflp != NULL; iflp = iflp->ifl_next) {
		if (strcmp(iflp->ifl_ifinfop->if_name, name) == 0)
			break;
	}

	return (iflp != NULL ? iflp->ifl_ifinfop : NULL);
}

/*
 * Retrieve the information for the address `addrp' on group `grname' in
 * snapshot `snap'.  Returns a pointer to the address information on success,
 * or NULL on failure.
 */
static ipmp_addrinfo_t *
ipmp_snap_getaddrinfo(ipmp_snap_t *snap, const char *grname,
    struct sockaddr_storage *addrp)
{
	ipmp_addrinfolist_t *adlp;

	for (adlp = snap->sn_adinfolistp; adlp != NULL; adlp = adlp->adl_next) {
		if (strcmp(grname, adlp->adl_adinfop->ad_group) == 0 &&
		    sockaddrcmp(addrp, &adlp->adl_adinfop->ad_addr))
			break;
	}

	return (adlp != NULL ? adlp->adl_adinfop : NULL);
}

/*
 * Using `statep', take a snapshot of the IPMP subsystem and if successful
 * return it in a dynamically allocated snapshot pointed to by `*snapp'.
 * Returns an IPMP error code.
 */
static int
ipmp_snap_take(ipmp_state_t *statep, ipmp_snap_t **snapp)
{
	uint64_t	naddr, ngroup, nif;
	ipmp_snap_t	*snap;
	ipmp_infotype_t	type;
	int		retval;
	size_t		len;
	void		*infop;
	struct timeval	end;

	naddr = ngroup = nif = UINT64_MAX;

	snap = ipmp_snap_create();
	if (snap == NULL)
		return (IPMP_ENOMEM);

	retval = ipmp_sendquery(statep, IPMP_SNAP, NULL, NULL, &end);
	if (retval != IPMP_SUCCESS) {
		ipmp_snap_free(snap);
		return (retval);
	}

	/*
	 * Build up our snapshot.  We know there will always be at least four
	 * TLVs for IPMP_GROUPLIST, IPMP_IFCNT, IPMP_GROUPCNT, and IPMP_ADDRCNT.
	 * If we receive anything illogical (e.g., more than the expected number
	 * of interfaces), then bail out.  However, to a large extent we have to
	 * trust the information sent by in.mpathd.
	 */
	do {
		infop = NULL;
		retval = ipmp_readtlv(statep->st_fd, &type, &len, &infop, &end);
		if (retval != IPMP_SUCCESS)
			goto fail;

		if (!ipmp_checktlv(type, len, infop)) {
			retval = IPMP_EPROTO;
			goto fail;
		}

		switch (type) {
		case IPMP_GROUPLIST:
			if (snap->sn_grlistp != NULL) {
				retval = IPMP_EPROTO;
				break;
			}
			snap->sn_grlistp = infop;
			break;

		case IPMP_IFINFO:
			if (snap->sn_nif == nif) {
				retval = IPMP_EPROTO;
				break;
			}

			infop = ipmp_convertifinfo(infop);
			if (infop == NULL) {
				retval = IPMP_ENOMEM;
				break;
			}

			/*
			 * Read in V4 and V6 targlist TLVs that follow.
			 */
			retval = ipmp_readifinfo_lists(statep, infop, &end);
			if (retval != IPMP_SUCCESS)
				break;

			retval = ipmp_snap_addifinfo(snap, infop);
			if (retval != IPMP_SUCCESS) {
				ipmp_freeifinfo(infop);
				infop = NULL;
			}
			break;

		case IPMP_ADDRINFO:
			if (snap->sn_naddr == naddr) {
				retval = IPMP_EPROTO;
				break;
			}

			retval = ipmp_snap_addaddrinfo(snap, infop);
			/*
			 * NOTE: since we didn't call ipmp_read*info_lists(),
			 * no need to use ipmp_freeaddrinfo() on failure.
			 */
			break;

		case IPMP_GROUPINFO:
			if (snap->sn_ngroup == ngroup) {
				retval = IPMP_EPROTO;
				break;
			}

			infop = ipmp_convertgroupinfo(infop);
			if (infop == NULL) {
				retval = IPMP_ENOMEM;
				break;
			}

			/*
			 * Read in IPMP groupinfo list TLVs that follow.
			 */
			retval = ipmp_readgroupinfo_lists(statep, infop, &end);
			if (retval != IPMP_SUCCESS)
				break;

			retval = ipmp_snap_addgroupinfo(snap, infop);
			if (retval != IPMP_SUCCESS) {
				ipmp_freegroupinfo(infop);
				infop = NULL;
			}
			break;

		case IPMP_ADDRCNT:
			if (naddr != UINT64_MAX) {
				retval = IPMP_EPROTO;
				break;
			}

			naddr = *(uint32_t *)infop;
			break;

		case IPMP_GROUPCNT:
			if (ngroup != UINT64_MAX) {
				retval = IPMP_EPROTO;
				break;
			}

			ngroup = *(uint32_t *)infop;
			break;

		case IPMP_IFCNT:
			if (nif != UINT64_MAX) {
				retval = IPMP_EPROTO;
				break;
			}

			nif = *(uint32_t *)infop;
			break;

		default:
			retval = IPMP_EPROTO;
			break;
		}
fail:
		if (retval != IPMP_SUCCESS) {
			free(infop);
			ipmp_snap_free(snap);
			return (ipmp_querydone(statep, retval));
		}
	} while (snap->sn_grlistp == NULL || snap->sn_nif < nif ||
	    snap->sn_ngroup < ngroup || snap->sn_naddr < naddr);

	*snapp = snap;
	return (ipmp_querydone(statep, IPMP_SUCCESS));
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2021 Tintri by DDN, Inc. All rights reserved.
 */

#ifndef _IPMP_QUERY_H
#define	_IPMP_QUERY_H

#include <sys/types.h>
#include <sys/socket.h>			/* needed by <net/if.h> */
#include <net/if.h>			/* for LIF*NAMSIZ */
#include <ipmp.h>

/*
 * IPMP query interfaces.
 *
 * These interfaces may only be used within ON or after signing a contract
 * with ON.  For documentation, refer to PSARC/2002/615 and PSARC/2007/272.
 */

#ifdef __cplusplus
extern "C" {
#endif

/*
 * Assorted enumerations used in the data types described below.
 */
typedef enum ipmp_if_probestate {
	IPMP_PROBE_OK,		/* probes detect no problems */
	IPMP_PROBE_FAILED,	/* probes detect failure */
	IPMP_PROBE_UNKNOWN,	/* probe detection unavailable */
	IPMP_PROBE_DISABLED	/* probe detection disabled */
} ipmp_if_probestate_t;

typedef enum ipmp_if_linkstate {
	IPMP_LINK_UP,		/* link detects up */
	IPMP_LINK_DOWN,		/* link detects down */
	IPMP_LINK_UNKNOWN	/* link detection unavailable */
} ipmp_if_linkstate_t;

typedef enum ipmp_if_flags {
	IPMP_IFFLAG_INACTIVE	= 0x1,
	IPMP_IFFLAG_HWADDRDUP	= 0x2,
	IPMP_IFFLAG_ACTIVE	= 0x4,
	IPMP_IFFLAG_DOWN	= 0x8
} ipmp_if_flags_t;

typedef enum ipmp_addr_state {
	IPMP_ADDR_UP,		/* address is up */
	IPMP_ADDR_DOWN		/* address is down */
} ipmp_addr_state_t;

typedef enum ipmp_if_targmode {
	IPMP_TARG_DISABLED,	/* use of targets is disabled */
	IPMP_TARG_ROUTES,	/* route-learned targets */
	IPMP_TARG_MULTICAST	/* multicast-learned targets */
} ipmp_if_targmode_t;

#define	IPMP_LIST_SIZE(listtype, elsize, nel) \
	((sizeof (ipmp_ ## listtype ## _t) - (elsize)) + ((nel) * (elsize)))

/*
 * Data type describing a list of IPMP groups.
 */
typedef struct ipmp_grouplist {
	uint64_t	gl_sig;
	unsigned int	gl_ngroup;
	uint32_t	gl_pad;
	char		gl_groups[1][LIFGRNAMSIZ];
} ipmp_grouplist_t;

#define	IPMP_GROUPLIST_SIZE(ngr)	\
	IPMP_LIST_SIZE(grouplist, LIFGRNAMSIZ, ngr)

/*
 * Data type describing a list of interfaces.
 */
typedef struct ipmp_iflist {
	unsigned int	il_nif;
	char		il_ifs[1][LIFNAMSIZ];
} ipmp_iflist_t;

#define	IPMP_IFLIST_SIZE(nif)		\
	IPMP_LIST_SIZE(iflist, LIFNAMSIZ, nif)

/*
 * Data type describing a list of addresses.
 */
typedef struct ipmp_addrlist {
	unsigned int		al_naddr;
	uint32_t		al_pad;
	struct sockaddr_storage al_addrs[1];
} ipmp_addrlist_t;

#define	IPMP_ADDRLIST_SIZE(naddr)	\
	IPMP_LIST_SIZE(addrlist, sizeof (struct sockaddr_storage), naddr)

/*
 * Data type describing the state of an IPMP group, and a subset data type
 * used for communication between libipmp and in.mpathd.
 */
typedef struct ipmp_groupinfo {
	char			gr_name[LIFGRNAMSIZ];
	uint64_t		gr_sig;
	ipmp_group_state_t	gr_state;
	ipmp_iflist_t		*gr_iflistp;
	ipmp_addrlist_t		*gr_adlistp;
	char			gr_ifname[LIFNAMSIZ];
	char			gr_m4ifname[LIFNAMSIZ];
	char			gr_m6ifname[LIFNAMSIZ];
	char			gr_bcifname[LIFNAMSIZ];
	unsigned int		gr_fdt;
} ipmp_groupinfo_t;

typedef struct ipmp_groupinfo_xfer {
	char			grx_name[LIFGRNAMSIZ];
	uint64_t		grx_sig;
	ipmp_group_state_t	grx_state;
	char			grx_ifname[LIFNAMSIZ];
	char			grx_m4ifname[LIFNAMSIZ];
	char			grx_m6ifname[LIFNAMSIZ];
	char			grx_bcifname[LIFNAMSIZ];
	unsigned int		grx_fdt;
} ipmp_groupinfo_xfer_t;

/*
 * Data type describing IPMP target information for a particular interface,
 * and a subset data type used for communication between libipmp and in.mpathd.
 */
typedef struct ipmp_targinfo {
	char			it_name[LIFNAMSIZ];
	struct sockaddr_storage	it_testaddr;
	ipmp_if_targmode_t	it_targmode;
	ipmp_addrlist_t		*it_targlistp;
} ipmp_targinfo_t;

typedef struct ipmp_targinfo_xfer {
	char			itx_name[LIFNAMSIZ];
	struct sockaddr_storage	itx_testaddr;
	ipmp_if_targmode_t	itx_targmode;
	uint32_t		itx_pad;
} ipmp_targinfo_xfer_t;


/*
 * Data type describing the IPMP-related state of an interface, and a subset
 * data type used for communication between libipmp and in.mpathd.
 */
typedef struct ipmp_ifinfo {
	char			if_name[LIFNAMSIZ];
	char			if_group[LIFGRNAMSIZ];
	ipmp_if_state_t		if_state;
	ipmp_if_type_t		if_type;
	ipmp_if_linkstate_t	if_linkstate;
	ipmp_if_probestate_t	if_probestate;
	ipmp_if_flags_t		if_flags;
	ipmp_targinfo_t		if_targinfo4;
	ipmp_targinfo_t		if_targinfo6;
} ipmp_ifinfo_t;

typedef struct ipmp_ifinfo_xfer {
	char			ifx_name[LIFNAMSIZ];
	char			ifx_group[LIFGRNAMSIZ];
	ipmp_if_state_t		ifx_state;
	ipmp_if_type_t		ifx_type;
	ipmp_if_linkstate_t	ifx_linkstate;
	ipmp_if_probestate_t	ifx_probestate;
	ipmp_if_flags_t		ifx_flags;
	uint32_t		ifx_pad;
	ipmp_targinfo_xfer_t	ifx_targinfo4;
	ipmp_targinfo_xfer_t	ifx_targinfo6;
} ipmp_ifinfo_xfer_t;


/*
 * Data type describing an IPMP data address.
 */
typedef struct ipmp_addrinfo {
	struct sockaddr_storage	ad_addr;
	ipmp_addr_state_t	ad_state;
	char			ad_group[LIFGRNAMSIZ];
	char			ad_binding[LIFNAMSIZ];
	uint32_t		ad_pad;
} ipmp_addrinfo_t;

typedef enum {
	IPMP_QCONTEXT_LIVE,
	IPMP_QCONTEXT_SNAP
} ipmp_qcontext_t;

extern int  ipmp_setqcontext(ipmp_handle_t, ipmp_qcontext_t);
extern int  ipmp_getgrouplist(ipmp_handle_t, ipmp_grouplist_t **);
extern void ipmp_freegrouplist(ipmp_grouplist_t *);
extern int  ipmp_getgroupinfo(ipmp_handle_t, const char *, ipmp_groupinfo_t **);
extern void ipmp_freegroupinfo(ipmp_groupinfo_t *);
extern int  ipmp_getifinfo(ipmp_handle_t, const char *, ipmp_ifinfo_t **);
extern void ipmp_freeifinfo(ipmp_ifinfo_t *);
extern int  ipmp_getaddrinfo(ipmp_handle_t, const char *,
    struct sockaddr_storage *, ipmp_addrinfo_t **);
extern void ipmp_freeaddrinfo(ipmp_addrinfo_t *);

#ifdef __cplusplus
}
#endif

#endif /* _IPMP_QUERY_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2021 Tintri by DDN, Inc. All rights reserved.
 */

#ifndef _IPMP_QUERY_IMPL_H
#define	_IPMP_QUERY_IMPL_H

#include <ipmp_query.h>

/*
 * Private IPMP query interfaces and structures.
 *
 * These are *only* for use by in.mpathd and libipmp itself.
 */

#ifdef __cplusplus
extern "C" {
#endif

/*
 * List of ipmp_groupinfo_t structures.
 */
typedef struct ipmp_groupinfolist {
	struct ipmp_groupinfolist *grl_next;
	ipmp_groupinfo_t	*grl_grinfop;
} ipmp_groupinfolist_t;

/*
 * List of ipmp_ifinfo_t structures.
 */
typedef struct ipmp_ifinfolist {
	struct ipmp_ifinfolist	*ifl_next;
	ipmp_ifinfo_t		*ifl_ifinfop;
} ipmp_ifinfolist_t;

/*
 * List of ipmp_addrinfo_t structures.
 */
typedef struct ipmp_addrinfolist {
	struct ipmp_addrinfolist *adl_next;
	ipmp_addrinfo_t		*adl_adinfop;
} ipmp_addrinfolist_t;

/*
 * Snapshot of IPMP state.
 */
typedef struct ipmp_snap {
	ipmp_grouplist_t	*sn_grlistp;
	ipmp_groupinfolist_t	*sn_grinfolistp;
	ipmp_ifinfolist_t	*sn_ifinfolistp;
	ipmp_addrinfolist_t	*sn_adinfolistp;
	uint32_t		sn_ngroup;
	uint32_t		sn_nif;
	uint32_t		sn_naddr;
} ipmp_snap_t;

/*
 * Snapshot-related routines.
 */
extern ipmp_snap_t *ipmp_snap_create(void);
extern void ipmp_snap_free(ipmp_snap_t *);
extern int ipmp_snap_addifinfo(ipmp_snap_t *, ipmp_ifinfo_t *);
extern int ipmp_snap_addaddrinfo(ipmp_snap_t *, ipmp_addrinfo_t *);
extern int ipmp_snap_addgroupinfo(ipmp_snap_t *, ipmp_groupinfo_t *);

/*
 * IPMP structure creation/destruction routines.
 */
extern ipmp_ifinfo_t *ipmp_ifinfo_create(const char *, const char *,
    ipmp_if_state_t, ipmp_if_type_t, ipmp_if_linkstate_t, ipmp_if_probestate_t,
    ipmp_if_flags_t, ipmp_targinfo_t *, ipmp_targinfo_t *);
extern ipmp_groupinfo_t *ipmp_groupinfo_create(const char *, uint64_t, uint_t,
    ipmp_group_state_t, uint_t, char (*)[LIFNAMSIZ], const char *,
    const char *, const char *, const char *, uint_t,
    struct sockaddr_storage *);
extern ipmp_grouplist_t *ipmp_grouplist_create(uint64_t, unsigned int,
    char (*)[LIFGRNAMSIZ]);
extern ipmp_addrinfo_t *ipmp_addrinfo_create(struct sockaddr_storage *,
    ipmp_addr_state_t, const char *, const char *);
extern ipmp_targinfo_t *ipmp_targinfo_create(const char *,
    struct sockaddr_storage *, ipmp_if_targmode_t, uint_t,
    struct sockaddr_storage *);
extern void ipmp_freetarginfo(ipmp_targinfo_t *);


#ifdef __cplusplus
}
#endif

#endif /* _IPMP_QUERY_IMPL_H */
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
#

#
# MAPFILE HEADER START
#
# WARNING:  STOP NOW.  DO NOT MODIFY THIS FILE.
# Object versioning must comply with the rules detailed in
#
#	usr/src/lib/README.mapfiles
#
# You should not be making modifications here until you've read the most current
# copy of that file. If you need help, contact a gatekeeper for guidance.
#
# MAPFILE HEADER END
#

$mapfile_version 2

SYMBOL_VERSION SUNWprivate_1.1 {
    global:
	ipmp_addrinfo_create;
	ipmp_close;
	ipmp_errmsg;
	ipmp_freeaddrinfo;
	ipmp_freegroupinfo;
	ipmp_freegrouplist;
	ipmp_freeifinfo;
	ipmp_freetarginfo;
	ipmp_getaddrinfo;
	ipmp_getgroupinfo;
	ipmp_getgrouplist;
	ipmp_getifinfo;
	ipmp_groupinfo_create;
	ipmp_grouplist_create;
	ipmp_ifinfo_create;
	ipmp_offline;
	ipmp_open;
	ipmp_ping_daemon;
	ipmp_read;
	ipmp_setqcontext;
	ipmp_snap_addaddrinfo;
	ipmp_snap_addgroupinfo;
	ipmp_snap_addifinfo;
	ipmp_snap_create;
	ipmp_snap_free;
	ipmp_targinfo_create;
	ipmp_undo_offline;
	ipmp_write;
	ipmp_writetlv;
    local:
	*;
};