|
root / base / usr / src / uts / common / netsmb
netsmb Plain Text 2163 lines 66.4 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
/*
 * Copyright (c) 2000, 2001 Boris Popov
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by Boris Popov.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * 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.
 *
 * $FreeBSD: src/sys/sys/mchain.h,v 1.1 2001/02/24 15:44:30 bp Exp $
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
 */

#ifndef _MCHAIN_H_
#define	_MCHAIN_H_

#include <sys/types.h>
#include <sys/isa_defs.h>
#include <sys/byteorder.h>

#ifdef _LITTLE_ENDIAN

/* little-endian values on little-endian */
#define	htoles(x)	((uint16_t)(x))
#define	letohs(x)	((uint16_t)(x))
#define	htolel(x)	((uint32_t)(x))
#define	letohl(x)	((uint32_t)(x))
#define	htoleq(x)	((uint64_t)(x))
#define	letohq(x)	((uint64_t)(x))

/*
 * big-endian values on little-endian (swap)
 *
 * Use the BSWAP macros because they're fastest, and they're
 * available in all environments where we use this header.
 */
#define	htobes(x)	BSWAP_16(x)
#define	betohs(x)	BSWAP_16(x)
#define	htobel(x)	BSWAP_32(x)
#define	betohl(x)	BSWAP_32(x)
#define	htobeq(x)	BSWAP_64(x)
#define	betohq(x)	BSWAP_64(x)

#else	/* (BYTE_ORDER == LITTLE_ENDIAN) */

/* little-endian values on big-endian (swap) */
#define	letohs(x)	BSWAP_16(x)
#define	htoles(x)	BSWAP_16(x)
#define	letohl(x)	BSWAP_32(x)
#define	htolel(x)	BSWAP_32(x)
#define	letohq(x)	BSWAP_64(x)
#define	htoleq(x)	BSWAP_64(x)

/* big-endian values on big-endian */
#define	htobes(x)	((uint16_t)(x))
#define	betohs(x)	((uint16_t)(x))
#define	htobel(x)	((uint32_t)(x))
#define	betohl(x)	((uint32_t)(x))
#define	htobeq(x)	((uint64_t)(x))
#define	betohq(x)	((uint64_t)(x))
#endif	/* (BYTE_ORDER == LITTLE_ENDIAN) */


/*
 * Additions for Solaris to replace things that came from
 * <sys/mbuf.h> in the Darwin code.  These are mostly just
 * wrappers for streams functions.  See: subr_mchain.c
 */

#if defined(_KERNEL) || defined(_FAKE_KERNEL)

/*
 * BSD-style mbuf "shim" for kernel code.  Note, this
 * does NOT implement BSD mbufs in the kernel.  Rather,
 * macros and wrapper functions are used so that code
 * fomerly using mbuf_t now use STREAMS mblk_t instead.
 */

#include <sys/stream.h> /* mblk_t */
#include <sys/strsun.h> /* MBLKL */
typedef mblk_t mbuf_t;

/* BEGIN CSTYLED */
/*
 * BSD-style mbufs, vs SysV-style mblks:
 * One big difference: the mbuf payload is:
 *   m_data ... (m_data + m_len)
 * In Unix STREAMS, the mblk payload is:
 *   b_rptr ... b_wptr
 *
 * Here are some handy conversion notes:
 *
 * struct mbuf                     struct mblk
 *   m->m_next                       m->b_cont
 *   m->m_nextpkt                    m->b_next
 *   m->m_data                       m->b_rptr
 *   m->m_len                        MBLKL(m)
 *   m->m_dat[]                      m->b_datap->db_base
 *   &m->m_dat[MLEN]                 m->b_datap->db_lim
 *   M_TRAILINGSPACE(m)              MBLKTAIL(m)
 *   m_freem(m)                      freemsg(m)
 *
 * Note that mbufs chains also have a special "packet" header,
 * which has the length of the whole message.  In STREAMS one
 * typically just calls msgdsize(m) to get that.
 */
/* END CSTYLED */

#define	mtod(m, t) ((t)((m)->b_rptr))

/* length arg for m_copym to "copy all" */
#define	M_COPYALL		-1

mblk_t *m_copym(mblk_t *, int, int, int);
mblk_t *m_pullup(mblk_t *, int);
mblk_t *m_split(mblk_t *, int, int);
void m_cat(mblk_t *, mblk_t *);
#define	m_freem(x)	freemsg(x)
mblk_t *m_getblk(int, int);
int  m_fixhdr(mblk_t *m);

#else	/* _KERNEL */

/*
 * BSD-style mbuf work-alike, for user-level.
 * See libsmbfs mbuf.c
 */
typedef struct mbuf {
	int		m_len;
	int		m_maxlen;
	char		*m_data;
	struct mbuf	*m_next;
} mbuf_t;

#define	mtod(m, t)	((t)(m)->m_data)

int m_get(int, mbuf_t **);
void m_freem(mbuf_t *);

#endif	/* _KERNEL */

/*
 * BSD-style mbchain/mdchain work-alike
 */

/*
 * Type of copy for mb_{put|get}_mem()
 */
#define	MB_MSYSTEM	0		/* use bcopy() */
#define	MB_MUSER	1		/* use copyin()/copyout() */
#define	MB_MINLINE	2		/* use an inline copy loop */
#define	MB_MZERO	3		/* bzero(), mb_put_mem only */
#define	MB_MCUSTOM	4		/* use an user defined function */

#if defined(_KERNEL) || defined(_FAKE_KERNEL)

struct mbchain {
	mblk_t *mb_top;
	mblk_t *mb_cur;
	uint_t mb_count;
};
typedef struct mbchain mbchain_t;

struct mdchain {
	mblk_t *md_top;		/* head of mblk chain */
	mblk_t *md_cur;		/* current mblk */
	uchar_t *md_pos;	/* position in md_cur */
	/* NB: md_pos is same type as mblk_t b_rptr, b_wptr members. */
};
typedef struct mdchain mdchain_t;

mblk_t *mb_detach(mbchain_t *mbp);
int  mb_fixhdr(mbchain_t *mbp);
int  mb_put_uio(mbchain_t *mbp, uio_t *uiop, size_t size);

void md_append_record(mdchain_t *mdp, mblk_t *top);
void md_next_record(mdchain_t *mdp);
int  md_get_uio(mdchain_t *mdp, uio_t *uiop, size_t size);

#else	/* _KERNEL */

/*
 * user-level code uses the same struct for both (MB, MD)
 */
typedef struct mbdata {
	mbuf_t	*mb_top;	/* head of mbuf chain */
	mbuf_t	*mb_cur;	/* current mbuf */
	char	*mb_pos;	/* position in mb_cur (get) */
	/* NB: mb_pos is same type as mbuf_t m_data member. */
	int	mb_count;	/* bytes marshalled (put) */
} mbdata_t;
typedef struct mbdata mbchain_t;
typedef struct mbdata mdchain_t;

#endif	/* _KERNEL */

int  mb_init(mbchain_t *);
void mb_initm(mbchain_t *, mbuf_t *);
void mb_done(mbchain_t *);
void *mb_reserve(mbchain_t *, int size);

int  mb_put_align8(mbchain_t *mbp);
int  mb_put_padbyte(mbchain_t *mbp);
int  mb_put_uint8(mbchain_t *, uint8_t);
int  mb_put_uint16be(mbchain_t *, uint16_t);
int  mb_put_uint16le(mbchain_t *, uint16_t);
int  mb_put_uint32be(mbchain_t *, uint32_t);
int  mb_put_uint32le(mbchain_t *, uint32_t);
int  mb_put_uint64be(mbchain_t *, uint64_t);
int  mb_put_uint64le(mbchain_t *, uint64_t);
int  mb_put_mem(mbchain_t *, const void *, int, int);
int  mb_put_mbuf(mbchain_t *, mbuf_t *);
int  mb_put_mbchain(mbchain_t *, mbchain_t *);

int  md_init(mdchain_t *mdp);
void md_initm(mdchain_t *mbp, mbuf_t *m);
void md_done(mdchain_t *mdp);

int  md_get_uint8(mdchain_t *, uint8_t *);
int  md_get_uint16be(mdchain_t *, uint16_t *);
int  md_get_uint16le(mdchain_t *, uint16_t *);
int  md_get_uint32be(mdchain_t *, uint32_t *);
int  md_get_uint32le(mdchain_t *, uint32_t *);
int  md_get_uint64be(mdchain_t *, uint64_t *);
int  md_get_uint64le(mdchain_t *, uint64_t *);
int  md_get_mem(mdchain_t *, void *, int, int);
int  md_get_mbuf(mdchain_t *, int, mbuf_t **);
int  md_seek(mdchain_t *, uint32_t);
uint32_t md_tell(mdchain_t *);

#endif	/* !_MCHAIN_H_ */
/*
 * Copyright (c) 2000-2001 Boris Popov
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by Boris Popov.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * 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.
 *
 * $Id: netbios.h,v 1.5 2004/03/19 01:49:45 lindak Exp $
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef _NETSMB_NETBIOS_H_
#define	_NETSMB_NETBIOS_H_

#ifndef _NETINET_IN_H_
#include <netinet/in.h>
#endif

/*
 * This is a fake address family number, used to
 * recognize our fake sockaddr_nb objects.
 * This is never handed to bind or connect.
 */
#ifndef AF_NETBIOS
#define	AF_NETBIOS (AF_MAX+2)
#endif

#define	PF_NETBIOS	AF_NETBIOS

/*
 * NetBIOS port numbers by the names used in the Darwin code.
 * XXX: Change the code to use IPPORT_xxx from in.h directly.
 * XXX: Add IPPORT_SMB_OVER_TCP or some such (port 445)
 */
#define	NBNS_UDP_PORT		IPPORT_NETBIOS_NS	/* 137 */
#define	SMB_TCP_PORT		IPPORT_NETBIOS_SSN	/* 139 */

#define	NBPROTO_TCPSSN	1		/* NETBIOS session over TCP */

#define	NB_NAMELEN	16
#define	NB_ENCNAMELEN	NB_NAMELEN * 2
#define	NB_MAXLABLEN	63

#define	NB_MAXPKTLEN	0x1FFFF

#define	NB_MINSALEN	(sizeof (struct sockaddr_nb))

/*
 * name types
 */
#define	NBT_WKSTA	0x00
#define	NBT_CLIENT	0x03
#define	NBT_RASSRVR	0x06
#define	NBT_DMB		0x1B
#define	NBT_IP		0x1C
#define	NBT_MB		0x1D
#define	NBT_BS		0x1E
#define	NBT_NETDDE	0x1F
#define	NBT_SERVER	0x20
#define	NBT_RASCLNT	0x21
#define	NBT_NMAGENT	0xBE
#define	NBT_NMUTIL	0xBF

/*
 * Session packet types
 */
#define	NB_SSN_MESSAGE		0x0
#define	NB_SSN_REQUEST		0x81
#define	NB_SSN_POSRESP		0x82
#define	NB_SSN_NEGRESP		0x83
#define	NB_SSN_RTGRESP		0x84
#define	NB_SSN_KEEPALIVE	0x85

/*
 * resolver: Opcodes
 */
#define	NBNS_OPCODE_QUERY	0x00
#define	NBNS_OPCODE_REGISTER	0x05
#define	NBNS_OPCODE_RELEASE	0x06
#define	NBNS_OPCODE_WACK	0x07
#define	NBNS_OPCODE_REFRESH	0x08
#define	NBNS_OPCODE_RESPONSE	0x10	/* or'ed with other opcodes */

/*
 * resolver: NM_FLAGS
 */
#define	NBNS_NMFLAG_BCAST	0x01
#define	NBNS_NMFLAG_RA		0x08	/* recursion available */
#define	NBNS_NMFLAG_RD		0x10	/* recursion desired */
#define	NBNS_NMFLAG_TC		0x20	/* truncation occured */
#define	NBNS_NMFLAG_AA		0x40	/* authoritative answer */

/*
 * resolver: Question types
 */
#define	NBNS_QUESTION_TYPE_NB		0x0020
#define	NBNS_QUESTION_TYPE_NBSTAT	0x0021

/*
 * resolver: Question class
 */
#define	NBNS_QUESTION_CLASS_IN	0x0001

/*
 * resolver: Limits
 */
#define	NBNS_MAXREDIRECTS	3	/* max number of accepted redirects */
#define	NBDG_MAXSIZE		576	/* maximum nbns datagram size */

/*
 * NETBIOS addressing
 */

struct nb_name {
	uint_t		nn_type;
	char		nn_name[NB_NAMELEN];
	char		*nn_scope;
};
typedef struct nb_name nb_name_t;

/*
 * Our private NetBIOS socket address format.
 * Note that it's LARGER than sockaddr.
 *
 * XXX: Also note that the library code is sloppy about
 * casting this to sockaddr_in so let's keep snb_ipaddr
 * at the same offset, at least until that's fixed.
 */
struct sockaddr_nb {
	sa_family_t	snb_family;	/* address family */
	uint16_t    snb_flags;	/* NBNS_GROUPFLG, etc. */
	uint32_t	snb_ipaddr; /* always IPv4 */
	char		snb_name[NB_NAMELEN]; /* NOT encoded */
};
typedef struct sockaddr_nb sockaddr_nb_t;

#endif /* !_NETSMB_NETBIOS_H_ */
/*
 * Copyright (c) 2000-2001 Boris Popov
 * All rights reserved.
 *
 * Now many of these defines are from samba4 code, by Andrew Tridgell.
 * (Permission given to Conrad Minshall at CIFS plugfest Aug 13 2003.)
 * (Note the main decision was whether to use defines found in MS includes
 * and web pages, versus Samba, and the deciding factor is which developers
 * are more likely to be looking at this code base.)
 *
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by Boris Popov.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * 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.
 *
 * $Id: smb.h,v 1.36.90.1 2005/05/27 02:35:29 lindak Exp $
 */

/*
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
 */

#ifndef _NETSMB_SMB_H_
#define	_NETSMB_SMB_H_

/*
 * Common definitions and structures for SMB/CIFS protocol
 * This file should be purely SMB protocol definition stuff.
 * (Please don't make it a catch-all:)
 */

#include <smb/doserror.h>
#include <smb/lmerr.h>
#include <smb/nterror.h>
#include <smb/ntstatus.h>

/*
 * SMB dialects that we have to deal with.
 */
enum smb_dialects {
	SMB_DIALECT_NONE,
	SMB_DIALECT_CORE,		/* PC NETWORK PROGRAM 1.0, PCLAN1.0 */
	SMB_DIALECT_COREPLUS,		/* MICROSOFT NETWORKS 1.03 */
	SMB_DIALECT_LANMAN1_0,		/* MICROSOFT NETWORKS 3.0, LANMAN1.0 */
	SMB_DIALECT_LANMAN2_0,		/* LM1.2X002, DOS LM1.2X002, Samba */
	SMB_DIALECT_LANMAN2_1,		/* DOS LANMAN2.1, LANMAN2.1 */
	SMB_DIALECT_NTLM0_12,		/* NT LM 0.12, etc. */
	SMB_DIALECT_SMB2_FF		/* SMB1 negotiate to SMB2 */
};

/*
 * Formats of data/string buffers
 */
#define	SMB_DT_DATA		1
#define	SMB_DT_DIALECT		2
#define	SMB_DT_PATHNAME		3
#define	SMB_DT_ASCII		4
#define	SMB_DT_VARIABLE		5

/*
 * SMB header
 */

#define	SMB_SIGNATURE		"\xFFSMB"
#define	SMB_SIGLEN		4
#define	SMB_HDRCMD(p)		(*((uchar_t *)(p) + SMB_SIGLEN))
#define	SMB_HDRMID(p)		(*(ushort_t *)((uchar_t *)(p) + 30))
#define	SMB_HDR_OFF_MID		30
#define	SMB_HDRLEN		32

#define	SMB_HDR_V1	0xFF
#define	SMB_HDR_V2	0xFE
#define	SMB_HDR_V3E	0xFD	/* SMB3 encrypted */

/*
 * bits in the smb_flags field
 */
#define	SMB_FLAGS_SUPPORT_LOCKREAD	0x01
#define	SMB_FLAGS_CLIENT_BUF_AVAIL	0x02
#define	SMB_FLAGS_CASELESS		0x08
#define	SMB_FLAGS_CANONICAL_PATHNAMES	0x10
#define	SMB_FLAGS_REQUEST_OPLOCK	0x20
#define	SMB_FLAGS_REQUEST_BATCH_OPLOCK	0x40
#define	SMB_FLAGS_SERVER_RESP		0x80

/*
 * bits in the smb_flags2 field
 */
#define	SMB_FLAGS2_KNOWS_LONG_NAMES	0x0001
#define	SMB_FLAGS2_KNOWS_EAS		0x0002	/* client know about EAs */
#define	SMB_FLAGS2_SECURITY_SIGNATURE	0x0004	/* check SMB integrity */
#define	SMB_FLAGS2_IS_LONG_NAME		0x0040	/* any path name is long name */
#define	SMB_FLAGS2_EXT_SEC		0x0800	/* client aware of Extended */
						/* Security negotiation */
#define	SMB_FLAGS2_DFS			0x1000	/* resolve paths in DFS */
#define	SMB_FLAGS2_PAGING_IO		0x2000	/* for exec */
#define	SMB_FLAGS2_ERR_STATUS		0x4000	/* 1 - status.status */
#define	SMB_FLAGS2_UNICODE		0x8000	/* use Unicode for strings */

#define	SMB_UID_UNKNOWN		0xffff
#define	SMB_TID_UNKNOWN		0xffff
#define	SMB_FID_UNUSED		0xffff

/*
 * Security mode bits
 */
#define	SMB_SM_USER		0x01	/* server in the user security mode */
#define	SMB_SM_ENCRYPT		0x02	/* use challenge/responce */
#define	SMB_SM_SIGS		0x04
#define	SMB_SM_SIGS_REQUIRE	0x08

/*
 * Action bits in session setup reply
 */
#define	SMB_ACT_GUEST		0x01

/*
 * NTLM capabilities
 */
#define	SMB_CAP_RAW_MODE		0x0001
#define	SMB_CAP_MPX_MODE		0x0002
#define	SMB_CAP_UNICODE			0x0004
#define	SMB_CAP_LARGE_FILES		0x0008	/* 64 bit offsets supported */
#define	SMB_CAP_NT_SMBS			0x0010
#define	SMB_CAP_RPC_REMOTE_APIS		0x0020
#define	SMB_CAP_STATUS32		0x0040
#define	SMB_CAP_LEVEL_II_OPLOCKS	0x0080
#define	SMB_CAP_LOCK_AND_READ		0x0100
#define	SMB_CAP_NT_FIND			0x0200
#define	SMB_CAP_DFS			0x1000
#define	SMB_CAP_INFOLEVEL_PASSTHRU	0x2000
#define	SMB_CAP_LARGE_READX		0x4000
#define	SMB_CAP_LARGE_WRITEX		0x8000
#define	SMB_CAP_UNIX			0x00800000
#define	SMB_CAP_BULK_TRANSFER		0x20000000
#define	SMB_CAP_COMPRESSED_DATA		0x40000000
#define	SMB_CAP_EXT_SECURITY		0x80000000

/* SMB_COM_TREE_CONNECT_ANDX  flags. See [MS-SMB] for a complete description. */
#define	TREE_CONNECT_ANDX_DISCONNECT_TID		0x0001
#define	TREE_CONNECT_ANDX_EXTENDED_SIGNATURES	0x0004
#define	TREE_CONNECT_ANDX_EXTENDED_RESPONSE		0x0008

/*
 * SMB_COM_TREE_CONNECT_ANDX  optional support flags. See [MS-SMB] for a
 * complete description.
 */
#define	SMB_SUPPORT_SEARCH_BITS		0x0001	/* supports SearchAttributes */
#define	SMB_SHARE_IS_IN_DFS		0x0002	/* share is managed by DFS */
#define	SMB_CSC_MASK			0x000C	/* Offline-caching bits. */
#define	SMB_UNIQUE_FILE_NAME		0x0010	/* Long file names only */
#define	SMB_EXTENDED_SIGNATURES		0x0020	/* Signing key protection. */
/* See [MS-SMB] for a complete description of SMB_CSC_MASK bits. */
#define	SMB_CSC_CACHE_MANUAL_REINT	0x0000
#define	SMB_CSC_CACHE_AUTO_REINT	0x0004
#define	SMB_CSC_CACHE_VDO		0x0008

/*
 * File attributes
 */
#define	SMB_FA_RDONLY		0x01
#define	SMB_FA_HIDDEN		0x02
#define	SMB_FA_SYSTEM		0x04
#define	SMB_FA_VOLUME		0x08
#define	SMB_FA_DIR		0x10
#define	SMB_FA_ARCHIVE		0x20

/*
 * Extended file attributes
 */
#define	SMB_EFA_RDONLY			0x00000001
#define	SMB_EFA_HIDDEN			0x00000002
#define	SMB_EFA_SYSTEM			0x00000004
#define	SMB_EFA_VOLUME			0x00000008
#define	SMB_EFA_DIRECTORY		0x00000010
#define	SMB_EFA_ARCHIVE			0x00000020
#define	SMB_EFA_DEVICE			0x00000040
#define	SMB_EFA_NORMAL			0x00000080
#define	SMB_EFA_TEMPORARY		0x00000100
#define	SMB_EFA_SPARSE			0x00000200
#define	SMB_EFA_REPARSE_POINT		0x00000400
#define	SMB_EFA_COMPRESSED		0x00000800
#define	SMB_EFA_OFFLINE			0x00001000
#define	SMB_EFA_NONINDEXED		0x00002000
#define	SMB_EFA_ENCRYPTED		0x00004000
#define	SMB_EFA_POSIX_SEMANTICS		0x01000000
#define	SMB_EFA_BACKUP_SEMANTICS	0x02000000
#define	SMB_EFA_DELETE_ON_CLOSE		0x04000000
#define	SMB_EFA_SEQUENTIAL_SCAN		0x08000000
#define	SMB_EFA_RANDOM_ACCESS		0x10000000
#define	SMB_EFA_NO_BUFFERING		0x20000000
#define	SMB_EFA_WRITE_THROUGH		0x80000000

/*
 * Access Mode Encoding
 */
#define	SMB_AM_OPENREAD		0x0000
#define	SMB_AM_OPENWRITE	0x0001
#define	SMB_AM_OPENRW		0x0002
#define	SMB_AM_OPENEXEC		0x0003
#define	SMB_AM_OPENMODE		0x0003	/* mask for access mode bits */
#define	SMB_SM_COMPAT		0x0000
#define	SMB_SM_EXCLUSIVE	0x0010
#define	SMB_SM_DENYWRITE	0x0020
#define	SMB_SM_DENYREADEXEC	0x0030
#define	SMB_SM_DENYNONE		0x0040

/* NT_CREATE_ANDX flags */
#define	NTCREATEX_FLAGS_REQUEST_OPLOCK		0x02
#define	NTCREATEX_FLAGS_REQUEST_BATCH_OPLOCK	0x04
#define	NTCREATEX_FLAGS_OPEN_DIRECTORY		0x08
#define	NTCREATEX_FLAGS_EXTENDED		0x10

/* NT_CREATE_ANDX share_access (share mode) */
#define	NTCREATEX_SHARE_ACCESS_NONE		0
#define	NTCREATEX_SHARE_ACCESS_READ		1
#define	NTCREATEX_SHARE_ACCESS_WRITE		2
#define	NTCREATEX_SHARE_ACCESS_DELETE		4
#define	NTCREATEX_SHARE_ACCESS_ALL		7

/* NT_CREATE_ANDX open_disposition */
#define	NTCREATEX_DISP_SUPERSEDE	0 /* if file exists supersede it */
#define	NTCREATEX_DISP_OPEN		1 /* exists ? open it : fail */
#define	NTCREATEX_DISP_CREATE		2 /* exists ? fail : create it */
#define	NTCREATEX_DISP_OPEN_IF		3 /* exists ? open it : create it */
#define	NTCREATEX_DISP_OVERWRITE	4 /* exists ? overwrite : fail */
#define	NTCREATEX_DISP_OVERWRITE_IF	5 /* exists ? overwrite : create */

/* NT_CREATE_ANDX create_options */
#define	NTCREATEX_OPTIONS_DIRECTORY		0x0001
#define	NTCREATEX_OPTIONS_WRITE_THROUGH		0x0002
#define	NTCREATEX_OPTIONS_SEQUENTIAL_ONLY	0x0004
#define	NTCREATEX_OPTIONS_SYNC_ALERT		0x0010
#define	NTCREATEX_OPTIONS_ASYNC_ALERT		0x0020
#define	NTCREATEX_OPTIONS_NON_DIRECTORY_FILE	0x0040
#define	NTCREATEX_OPTIONS_NO_EA_KNOWLEDGE	0x0200
#define	NTCREATEX_OPTIONS_EIGHT_DOT_THREE_ONLY	0x0400
#define	NTCREATEX_OPTIONS_RANDOM_ACCESS		0x0800
#define	NTCREATEX_OPTIONS_DELETE_ON_CLOSE	0x1000
#define	NTCREATEX_OPTIONS_OPEN_BY_FILE_ID	0x2000

/* NT_CREATE_ANDX "impersonation" */
#define	NTCREATEX_IMPERSONATION_ANONYMOUS		0
#define	NTCREATEX_IMPERSONATION_IDENTIFICATION		1
#define	NTCREATEX_IMPERSONATION_IMPERSONATION		2
#define	NTCREATEX_IMPERSONATION_DELEGATION		3

/* NT_CREATE_ANDX security flags */
#define	NTCREATEX_SECURITY_DYNAMIC	1
#define	NTCREATEX_SECURITY_ALL		2

/* NT_CREATE_ANDX create_action in reply */
#define	NTCREATEX_ACTION_EXISTED	1
#define	NTCREATEX_ACTION_CREATED	2
#define	NTCREATEX_ACTION_TRUNCATED	3

/* SMB_TRANS2_FIND_FIRST2/SMB_TRANS2_FIND_NEXT2 flags */
#define	FIND2_CLOSE_AFTER_REQUEST	0x0001
#define	FIND2_CLOSE_ON_EOS		0x0002
#define	FIND2_RETURN_RESUME_KEYS	0x0004
#define	FIND2_CONTINUE_SEARCH		0x0008
#define	FIND2_BACKUP_INTENT		0x0010

/*
 * SMB commands
 */
#define	SMB_COM_CREATE_DIRECTORY	0x00
#define	SMB_COM_DELETE_DIRECTORY	0x01
#define	SMB_COM_OPEN			0x02
#define	SMB_COM_CREATE			0x03
#define	SMB_COM_CLOSE			0x04
#define	SMB_COM_FLUSH			0x05
#define	SMB_COM_DELETE			0x06
#define	SMB_COM_RENAME			0x07
#define	SMB_COM_QUERY_INFORMATION	0x08
#define	SMB_COM_SET_INFORMATION		0x09
#define	SMB_COM_READ			0x0A
#define	SMB_COM_WRITE			0x0B
#define	SMB_COM_LOCK_BYTE_RANGE		0x0C
#define	SMB_COM_UNLOCK_BYTE_RANGE	0x0D
#define	SMB_COM_CREATE_TEMPORARY	0x0E
#define	SMB_COM_CREATE_NEW		0x0F
#define	SMB_COM_CHECK_DIRECTORY		0x10
#define	SMB_COM_PROCESS_EXIT		0x11
#define	SMB_COM_SEEK			0x12
#define	SMB_COM_LOCK_AND_READ		0x13
#define	SMB_COM_WRITE_AND_UNLOCK	0x14
#define	SMB_COM_READ_RAW		0x1A
#define	SMB_COM_READ_MPX		0x1B
#define	SMB_COM_READ_MPX_SECONDARY	0x1C
#define	SMB_COM_WRITE_RAW		0x1D
#define	SMB_COM_WRITE_MPX		0x1E
#define	SMB_COM_WRITE_COMPLETE		0x20
#define	SMB_COM_SET_INFORMATION2	0x22
#define	SMB_COM_QUERY_INFORMATION2	0x23
#define	SMB_COM_LOCKING_ANDX		0x24
#define	SMB_COM_TRANSACTION		0x25
#define	SMB_COM_TRANSACTION_SECONDARY	0x26
#define	SMB_COM_IOCTL			0x27
#define	SMB_COM_IOCTL_SECONDARY		0x28
#define	SMB_COM_COPY			0x29
#define	SMB_COM_MOVE			0x2A
#define	SMB_COM_ECHO			0x2B
#define	SMB_COM_WRITE_AND_CLOSE		0x2C
#define	SMB_COM_OPEN_ANDX		0x2D
#define	SMB_COM_READ_ANDX		0x2E
#define	SMB_COM_WRITE_ANDX		0x2F
#define	SMB_COM_CLOSE_AND_TREE_DISC	0x31
#define	SMB_COM_TRANSACTION2		0x32
#define	SMB_COM_TRANSACTION2_SECONDARY	0x33
#define	SMB_COM_FIND_CLOSE2		0x34
#define	SMB_COM_FIND_NOTIFY_CLOSE	0x35
#define	SMB_COM_TREE_CONNECT		0x70
#define	SMB_COM_TREE_DISCONNECT		0x71
#define	SMB_COM_NEGOTIATE		0x72
#define	SMB_COM_SESSION_SETUP_ANDX	0x73
#define	SMB_COM_LOGOFF_ANDX		0x74
#define	SMB_COM_TREE_CONNECT_ANDX	0x75
#define	SMB_COM_QUERY_INFORMATION_DISK	0x80
#define	SMB_COM_SEARCH			0x81
#define	SMB_COM_FIND			0x82
#define	SMB_COM_FIND_UNIQUE		0x83
#define	SMB_COM_NT_TRANSACT		0xA0
#define	SMB_COM_NT_TRANSACT_SECONDARY	0xA1
#define	SMB_COM_NT_CREATE_ANDX		0xA2
#define	SMB_COM_NT_CANCEL		0xA4
#define	SMB_COM_OPEN_PRINT_FILE		0xC0
#define	SMB_COM_WRITE_PRINT_FILE	0xC1
#define	SMB_COM_CLOSE_PRINT_FILE	0xC2
#define	SMB_COM_GET_PRINT_QUEUE		0xC3
#define	SMB_COM_READ_BULK		0xD8
#define	SMB_COM_WRITE_BULK		0xD9
#define	SMB_COM_WRITE_BULK_DATA		0xDA

/*
 * SMB_COM_TRANSACTION2 subcommands
 */
#define	SMB_TRANS2_OPEN2			0x00
#define	SMB_TRANS2_FIND_FIRST2			0x01
#define	SMB_TRANS2_FIND_NEXT2			0x02
#define	SMB_TRANS2_QUERY_FS_INFORMATION		0x03
#define	SMB_TRANS2_SETFSINFO			0x04
#define	SMB_TRANS2_QUERY_PATH_INFORMATION	0x05
#define	SMB_TRANS2_SET_PATH_INFORMATION		0x06
#define	SMB_TRANS2_QUERY_FILE_INFORMATION	0x07
#define	SMB_TRANS2_SET_FILE_INFORMATION		0x08
#define	SMB_TRANS2_FSCTL			0x09
#define	SMB_TRANS2_IOCTL2			0x0A
#define	SMB_TRANS2_FIND_NOTIFY_FIRST		0x0B
#define	SMB_TRANS2_FIND_NOTIFY_NEXT		0x0C
#define	SMB_TRANS2_CREATE_DIRECTORY		0x0D
#define	SMB_TRANS2_SESSION_SETUP		0x0E
#define	SMB_TRANS2_GET_DFS_REFERRAL		0x10
#define	SMB_TRANS2_REPORT_DFS_INCONSISTENCY	0x11

/*
 * SMB_COM_NT_TRANSACT subcommands
 */
#define	NT_TRANSACT_CREATE		0x01
#define	NT_TRANSACT_IOCTL		0x02
#define	NT_TRANSACT_SET_SECURITY_DESC	0x03
#define	NT_TRANSACT_NOTIFY_CHANGE	0x04
#define	NT_TRANSACT_RENAME		0x05
#define	NT_TRANSACT_QUERY_SECURITY_DESC	0x06
#define	NT_TRANSACT_GET_USER_QUOTA	0x07
#define	NT_TRANSACT_SET_USER_QUOTA	0x08

/*
 * SMB_TRANS2_QUERY_FS_INFORMATION levels
 */
#define	SMB_QFS_ALLOCATION			1
#define	SMB_QFS_VOLUME				2
#define	SMB_QFS_LABEL_INFO			0x101
#define	SMB_QFS_VOLUME_INFO			0x102
#define	SMB_QFS_SIZE_INFO			0x103
#define	SMB_QFS_DEVICE_INFO			0x104
#define	SMB_QFS_ATTRIBUTE_INFO			0x105
#define	SMB_QFS_UNIX_INFO			0x200
#define	SMB_QFS_POSIX_WHOAMI			0x202
#define	SMB_QFS_MAC_FS_INFO			0x301
#define	SMB_QFS_VOLUME_INFORMATION		1001
#define	SMB_QFS_SIZE_INFORMATION		1003
#define	SMB_QFS_DEVICE_INFORMATION		1004
#define	SMB_QFS_ATTRIBUTE_INFORMATION		1005
#define	SMB_QFS_QUOTA_INFORMATION		1006
#define	SMB_QFS_FULL_SIZE_INFORMATION		1007
#define	SMB_QFS_OBJECTID_INFORMATION		1008

/*
 * NT Notify Change Compeletion Filter
 * NT Notify Actions
 * (We don't use these.)
 */

/*
 * SMB_QFS_ATTRIBUTE_INFO bits.
 * The following info found in msdn
 * (http://msdn.microsoft.com/library/default.asp?
 * url=/library/en-us/wmisdk/wmi/win32_cdromdrive.asp)
 * Naming is mostly as in samba, to help Those Who Google.
 */
#define	FILE_CASE_SENSITIVE_SEARCH	0x00000001
#define	FILE_CASE_PRESERVED_NAMES	0x00000002
#define	FILE_UNICODE_ON_DISK		0x00000004
#define	FILE_PERSISTENT_ACLS		0x00000008
#define	FILE_FILE_COMPRESSION		0x00000010
#define	FILE_VOLUME_QUOTAS		0x00000020
#define	FILE_SUPPORTS_SPARSE_FILES	0x00000040
#define	FILE_SUPPORTS_REPARSE_POINTS	0x00000080
#define	FILE_SUPPORTS_REMOTE_STORAGE	0x00000100
#define	FILE_SUPPORTS_LONG_NAMES	0x00004000
#define	FILE_VOLUME_IS_COMPRESSED	0x00008000
#define	FILE_SUPPORTS_OBJECT_IDS	0x00010000
#define	FILE_SUPPORTS_ENCRYPTION	0x00020000
#define	FILE_NAMED_STREAMS		0x00040000
#define	FILE_READ_ONLY_VOLUME		0x00080000

/*
 * SMB_TRANS2_QUERY_PATH levels
 */
#define	SMB_QFILEINFO_STANDARD			1
#define	SMB_QFILEINFO_EA_SIZE			2
#define	SMB_QFILEINFO_EAS_FROM_LIST		3
#define	SMB_QFILEINFO_ALL_EAS			4
#define	SMB_QFILEINFO_IS_NAME_VALID		6	/* QPATHINFO only? */
#define	SMB_QFILEINFO_BASIC_INFO		0x101
#define	SMB_QFILEINFO_STANDARD_INFO		0x102
#define	SMB_QFILEINFO_EA_INFO			0x103
#define	SMB_QFILEINFO_NAME_INFO			0x104
#define	SMB_QFILEINFO_ALLOCATION_INFO		0x105
#define	SMB_QFILEINFO_END_OF_FILE_INFO		0x106
#define	SMB_QFILEINFO_ALL_INFO			0x107
#define	SMB_QFILEINFO_ALT_NAME_INFO		0x108
#define	SMB_QFILEINFO_STREAM_INFO		0x109
#define	SMB_QFILEINFO_COMPRESSION_INFO		0x10b
#define	SMB_QFILEINFO_UNIX_BASIC		0x200
#define	SMB_QFILEINFO_UNIX_LINK			0x201
#define	SMB_QFILEINFO_POSIX_ACL			0x204
#define	SMB_QFILEINFO_UNIX_INFO2		0x20B
#define	SMB_QFILEINFO_MAC_DT_GET_APPL		0x306
#define	SMB_QFILEINFO_MAC_DT_GET_ICON		0x307
#define	SMB_QFILEINFO_MAC_DT_GET_ICON_INFO	0x308
#define	SMB_QFILEINFO_MAC_SPOTLIGHT		0x310
#define	SMB_QFILEINFO_BASIC_INFORMATION		1004
#define	SMB_QFILEINFO_STANDARD_INFORMATION	1005
#define	SMB_QFILEINFO_INTERNAL_INFORMATION	1006
#define	SMB_QFILEINFO_EA_INFORMATION		1007
#define	SMB_QFILEINFO_ACCESS_INFORMATION	1008
#define	SMB_QFILEINFO_NAME_INFORMATION		1009
#define	SMB_QFILEINFO_POSITION_INFORMATION	1014
#define	SMB_QFILEINFO_MODE_INFORMATION		1016
#define	SMB_QFILEINFO_ALIGNMENT_INFORMATION	1017
#define	SMB_QFILEINFO_ALL_INFORMATION		1018
#define	SMB_QFILEINFO_ALT_NAME_INFORMATION	1021
#define	SMB_QFILEINFO_STREAM_INFORMATION	1022
#define	SMB_QFILEINFO_COMPRESSION_INFORMATION	1028
#define	SMB_QFILEINFO_NETWORK_OPEN_INFORMATION	1034
#define	SMB_QFILEINFO_ATTRIBUTE_TAG_INFORMATION 1035

/*
 * SMB_TRANS2_FIND_FIRST2 information levels
 */
#define	SMB_FIND_STANDARD		1
#define	SMB_FIND_EA_SIZE		2
#define	SMB_FIND_EAS_FROM_LIST		3
#define	SMB_FIND_DIRECTORY_INFO		0x101
#define	SMB_FIND_FULL_DIRECTORY_INFO	0x102
#define	SMB_FIND_NAME_INFO		0x103
#define	SMB_FIND_BOTH_DIRECTORY_INFO	0x104
#define	SMB_FIND_UNIX_INFO		0x200
/* Transact 2 Find First levels */
#define	SMB_FIND_FILE_UNIX		0x202
#define	SMB_FIND_FILE_UNIX_INFO2	0x20B /* UNIX File Info2 */

/*
 * Selectors for NT_TRANSACT_QUERY_SECURITY_DESC and
 * NT_TRANSACT_SET_SECURITY_DESC.  Details found in the MSDN
 * library by searching on security_information.
 * Note the protected/unprotected bits did not exist in NT.
 */

#define	OWNER_SECURITY_INFORMATION		0x00000001
#define	GROUP_SECURITY_INFORMATION		0x00000002
#define	DACL_SECURITY_INFORMATION		0x00000004
#define	SACL_SECURITY_INFORMATION		0x00000008
#define	UNPROTECTED_SACL_SECURITY_INFORMATION	0x10000000
#define	UNPROTECTED_DACL_SECURITY_INFORMATION	0x20000000
#define	PROTECTED_SACL_SECURITY_INFORMATION	0x40000000
#define	PROTECTED_DACL_SECURITY_INFORMATION	0x80000000

/*
 * security descriptor header
 * it is followed by the optional SIDs and ACLs
 * note this is "raw", ie little-endian
 */
struct ntsecdesc {
	uint8_t		sd_revision;	/* 0x01 observed between W2K */
	uint8_t		sd_pad1;
	uint16_t	sd_flags;
	uint32_t	sd_owneroff;	/* offset to owner SID */
	uint32_t	sd_groupoff;	/* offset to group SID */
	uint32_t	sd_sacloff;	/* offset to system/audit ACL */
	uint32_t	sd_dacloff;	/* offset to discretionary ACL */
}; /* XXX: __attribute__((__packed__)); */
typedef struct ntsecdesc ntsecdesc_t;

#define	wset_sdrevision(s) ((s)->sd_revision = 0x01)
#define	sdflags(s) (letohs((s)->sd_flags))
#define	wset_sdflags(s, f) ((s)->sd_flags = letohs(f))
#define	sdowner(s) \
	((struct ntsid *)((s)->sd_owneroff ? \
	(char *)(s) + letohl((s)->sd_owneroff) : \
	NULL))
#define	wset_sdowneroff(s, o) ((s)->sd_owneroff = htolel(o))
#define	sdgroup(s) \
	((struct ntsid *)((s)->sd_groupoff ? \
	(char *)(s) + letohl((s)->sd_groupoff) : \
	NULL))
#define	wset_sdgroupoff(s, o) ((s)->sd_groupoff = htolel(o))
#define	sdsacl(s) \
	((struct ntacl *)((s)->sd_sacloff ? \
	(char *)(s) + letohl((s)->sd_sacloff) : \
	NULL))
#define	wset_sdsacloff(s, o) ((s)->sd_sacloff = htolel(o))
#define	sddacl(s) \
	((struct ntacl *)((s)->sd_dacloff ? \
	(char *)(s) + letohl((s)->sd_dacloff) : \
	NULL))
#define	wset_sddacloff(s, o) ((s)->sd_dacloff = htolel(o))

/*
 * sd_flags bits
 */
#define	SD_OWNER_DEFAULTED		0x0001
#define	SD_GROUP_DEFAULTED		0x0002
#define	SD_DACL_PRESENT			0x0004
#define	SD_DACL_DEFAULTED		0x0008
#define	SD_SACL_PRESENT			0x0010
#define	SD_SACL_DEFAULTED		0x0020
#define	SD_DACL_TRUSTED			0x0040
#define	SD_SERVER_SECURITY		0x0080
#define	SD_DACL_AUTO_INHERIT_REQ	0x0100
#define	SD_SACL_AUTO_INHERIT_REQ	0x0200
#define	SD_DACL_AUTO_INHERITED		0x0400
#define	SD_SACL_AUTO_INHERITED		0x0800
#define	SD_DACL_PROTECTED		0x1000
#define	SD_SACL_PROTECTED		0x2000
#define	SD_RM_CONTROL_VALID		0x4000
#define	SD_SELF_RELATIVE		0x8000

/*
 * access control list header
 * it is followed by the ACEs
 * note this is "raw", ie little-endian
 */
struct ntacl {
	uint8_t	acl_revision;	/* 0x02 observed with W2K */
	uint8_t	acl_pad1;
	uint16_t	acl_len; /* bytes; includes this header */
	uint16_t	acl_acecount;
	uint16_t	acl_pad2;
}; /* XXX: __attribute__((__packed__)); */
typedef struct ntacl ntacl_t;

#define	wset_aclrevision(a) ((a)->acl_revision = 0x02)
#define	acllen(a) (letohs((a)->acl_len))
#define	wset_acllen(a, l) ((a)->acl_len = htoles(l))
#define	aclacecount(a) (letohs((a)->acl_acecount))
#define	wset_aclacecount(a, c) ((a)->acl_acecount = htoles(c))
#define	aclace(a) ((struct ntace *)((char *)(a) + sizeof (struct ntacl)))

/*
 * access control entry header
 * it is followed by type-specific ace data,
 * which for the simple types is just a SID
 * note this is "raw", ie little-endian
 */
struct ntace {
	uint8_t	ace_type;
	uint8_t	ace_flags;
	uint16_t	ace_len; /* bytes; includes this header */
	uint32_t	ace_rights; /* generic, standard, specific, etc */
}; /* XXX: __attribute__((__packed__)); */

#define	acetype(a) ((a)->ace_type)
#define	wset_acetype(a, t) ((a)->ace_type = (t))
#define	aceflags(a) ((a)->ace_flags)
#define	wset_aceflags(a, f) ((a)->ace_flags = (f))
#define	acelen(a) (letohs((a)->ace_len))
#define	wset_acelen(a, l) ((a)->ace_len = htoles(l))
#define	acerights(a) (letohl((a)->ace_rights))
#define	wset_acerights(a, r) ((a)->ace_rights = htolel(r))
#define	aceace(a) ((struct ntace *)((char *)(a) + acelen(a)))
#define	acesid(a) ((struct ntsid *)((char *)(a) + sizeof (struct ntace)))

/*
 * ace_rights
 * (Samba bit names are used here, with permission, as the shorter Windows
 * names are more likely to cause namespace collisions)
 */
#define	SA_RIGHT_FILE_READ_DATA		0x00000001
#define	SA_RIGHT_FILE_WRITE_DATA	0x00000002
#define	SA_RIGHT_FILE_APPEND_DATA	0x00000004
#define	SA_RIGHT_FILE_READ_EA		0x00000008
#define	SA_RIGHT_FILE_WRITE_EA		0x00000010
#define	SA_RIGHT_FILE_EXECUTE		0x00000020
#define	SA_RIGHT_FILE_DELETE_CHILD	0x00000040
#define	SA_RIGHT_FILE_READ_ATTRIBUTES	0x00000080
#define	SA_RIGHT_FILE_WRITE_ATTRIBUTES	0x00000100
#define	SA_RIGHT_FILE_ALL_ACCESS	0x000001FF

#define	STD_RIGHT_DELETE_ACCESS		0x00010000
#define	STD_RIGHT_READ_CONTROL_ACCESS	0x00020000
#define	STD_RIGHT_WRITE_DAC_ACCESS	0x00040000
#define	STD_RIGHT_WRITE_OWNER_ACCESS	0x00080000
#define	STD_RIGHT_SYNCHRONIZE_ACCESS	0x00100000
#define	STD_RIGHT_ALL_ACCESS		0x001F0000

#define	SEC_RIGHT_SYSTEM_SECURITY	0x01000000
/*
 * Don't use MAXIMUM_ALLOWED as Samba (2.2.3 at least) will
 * return NT_STATUS_INVALID_LOCK_SEQUENCE
 */
#define	SEC_RIGHT_MAXIMUM_ALLOWED	0x02000000

#define	GENERIC_RIGHT_ALL_ACCESS	0x10000000
#define	GENERIC_RIGHT_EXECUTE_ACCESS	0x20000000
#define	GENERIC_RIGHT_WRITE_ACCESS	0x40000000
#define	GENERIC_RIGHT_READ_ACCESS	0x80000000

/*
 * these mappings are from Windows sample code but are likely incomplete
 *
 * GENERIC_RIGHT_READ_ACCESS :
 *	STD_RIGHT_SYNCHRONIZE_ACCESS |
 *	STD_RIGHT_READ_CONTROL_ACCESS |
 *	SA_RIGHT_FILE_READ_ATTRIBUTES |
 *	SA_RIGHT_FILE_READ_EA |
 *	SA_RIGHT_FILE_READ_DATA
 * GENERIC_RIGHT_WRITE_ACCESS :
 *	STD_RIGHT_SYNCHRONIZE_ACCESS |
 *	STD_RIGHT_READ_CONTROL_ACCESS |
 *	SA_RIGHT_FILE_WRITE_ATTRIBUTES |
 *	SA_RIGHT_FILE_WRITE_EA |
 *	SA_RIGHT_FILE_APPEND_DATA |
 *	SA_RIGHT_FILE_WRITE_DATA
 * GENERIC_RIGHT_EXECUTE_ACCESS :
 *	STD_RIGHT_SYNCHRONIZE_ACCESS |
 *	STD_RIGHT_READ_CONTROL_ACCESS |
 *	SA_RIGHT_FILE_READ_ATTRIBUTES |
 *	SA_RIGHT_FILE_EXECUTE
 * GENERIC_RIGHT_ALL_ACCESS :
 *	STD_RIGHT_SYNCHRONIZE_ACCESS |
 *	STD_RIGHT_WRITE_OWNER_ACCESS |
 *	STD_RIGHT_WRITE_DAC_ACCESS |
 *	STD_RIGHT_READ_CONTROL_ACCESS |
 *	STD_RIGHT_DELETE_ACCESS |
 *	SA_RIGHT_FILE_ALL_ACCESS
 */

/*
 * security identifier header
 * it is followed by sid_numauth sub-authorities,
 * which are 32 bits each.
 * note the subauths are little-endian on the wire, but
 * need to be big-endian for memberd/DS
 */
#define	SIDAUTHSIZE 6
struct ntsid {
	uint8_t	sid_revision;
	uint8_t	sid_subauthcount;
	uint8_t	sid_authority[SIDAUTHSIZE]; /* ie not little endian */
}; /* XXX: __attribute__((__packed__)); */
typedef struct ntsid ntsid_t;

#define	sidsubauthcount(s) (s->sid_subauthcount)
#define	sidlen(s) (sizeof (struct ntsid) + 4 * (s)->sid_subauthcount)
#define	MAXSIDLEN (sizeof (struct ntsid) + 4 * KAUTH_NTSID_MAX_AUTHORITIES)
#define	sidsub(s) ((uint32_t *)((char *)(s) + sizeof (struct ntsid)))

/*
 * MS' defined values for ace_type
 */
#define	ACCESS_ALLOWED_ACE_TYPE			0x0
#define	ACCESS_DENIED_ACE_TYPE			0x1
#define	SYSTEM_AUDIT_ACE_TYPE			0x2
#define	SYSTEM_ALARM_ACE_TYPE			0x3
#define	ACCESS_ALLOWED_COMPOUND_ACE_TYPE	0x4
#define	ACCESS_ALLOWED_OBJECT_ACE_TYPE		0x5
#define	ACCESS_DENIED_OBJECT_ACE_TYPE		0x6
#define	SYSTEM_AUDIT_OBJECT_ACE_TYPE		0x7
#define	SYSTEM_ALARM_OBJECT_ACE_TYPE		0x8
#define	ACCESS_ALLOWED_CALLBACK_ACE_TYPE	0x9
#define	ACCESS_DENIED_CALLBACK_ACE_TYPE		0xA
#define	ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE	0xB
#define	ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE	0xC
#define	SYSTEM_AUDIT_CALLBACK_ACE_TYPE		0xD
#define	SYSTEM_ALARM_CALLBACK_ACE_TYPE		0xE
#define	SYSTEM_AUDIT_CALLBACK_OBJECT_ACE_TYPE	0xF
#define	SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE	0x10

/*
 * MS' defined values for ace_flags
 */
#define	OBJECT_INHERIT_ACE_FLAG			0x01
#define	CONTAINER_INHERIT_ACE_FLAG		0x02
#define	NO_PROPAGATE_INHERIT_ACE_FLAG		0x04
#define	INHERIT_ONLY_ACE_FLAG			0x08
#define	INHERITED_ACE_FLAG			0x10
#define	UNDEF_ACE_FLAG				0x20 /* MS doesn't define it */
#define	VALID_INHERIT_ACE_FLAGS			0x1F
#define	SUCCESSFUL_ACCESS_ACE_FLAG		0x40
#define	FAILED_ACCESS_ACE_FLAG			0x80

/*
 * Set PATH/FILE information levels
 */
#define	SMB_SFILEINFO_STANDARD			1
#define	SMB_SFILEINFO_EA_SET			2
#define	SMB_SFILEINFO_BASIC_INFO		0x101
#define	SMB_SFILEINFO_DISPOSITION_INFO		0x102
#define	SMB_SFILEINFO_ALLOCATION_INFO		0x103
#define	SMB_SFILEINFO_END_OF_FILE_INFO		0x104
#define	SMB_SFILEINFO_UNIX_BASIC		0x200
#define	SMB_SFILEINFO_UNIX_LINK			0x201
#define	SMB_SFILEINFO_UNIX_HLINK		0x203
#define	SMB_SFILEINFO_POSIX_ACL			0x204
#define	SMB_SFILEINFO_POSIX_UNLINK		0x20A
#define	SMB_SFILEINFO_UNIX_INFO2		0x20B
#define	SMB_SFILEINFO_DIRECTORY_INFORMATION	1001
#define	SMB_SFILEINFO_FULL_DIRECTORY_INFORMATION	1002
#define	SMB_SFILEINFO_BOTH_DIRECTORY_INFORMATION	1003
#define	SMB_SFILEINFO_BASIC_INFORMATION		1004
#define	SMB_SFILEINFO_STANDARD_INFORMATION	1005
#define	SMB_SFILEINFO_INTERNAL_INFORMATION	1006
#define	SMB_SFILEINFO_EA_INFORMATION		1007
#define	SMB_SFILEINFO_ACCESS_INFORMATION	1008
#define	SMB_SFILEINFO_NAME_INFORMATION		1009
#define	SMB_SFILEINFO_RENAME_INFORMATION	1010
#define	SMB_SFILEINFO_LINK_INFORMATION		1011
#define	SMB_SFILEINFO_NAMES_INFORMATION		1012
#define	SMB_SFILEINFO_DISPOSITION_INFORMATION	1013
#define	SMB_SFILEINFO_POSITION_INFORMATION	1014
#define	SMB_SFILEINFO_1015			1015 /* ? */
#define	SMB_SFILEINFO_MODE_INFORMATION		1016
#define	SMB_SFILEINFO_ALIGNMENT_INFORMATION	1017
#define	SMB_SFILEINFO_ALL_INFORMATION		1018
#define	SMB_SFILEINFO_ALLOCATION_INFORMATION	1019
#define	SMB_SFILEINFO_END_OF_FILE_INFORMATION	1020
#define	SMB_SFILEINFO_ALT_NAME_INFORMATION	1021
#define	SMB_SFILEINFO_STREAM_INFORMATION	1022
#define	SMB_SFILEINFO_PIPE_INFORMATION		1023
#define	SMB_SFILEINFO_PIPE_LOCAL_INFORMATION	1024
#define	SMB_SFILEINFO_PIPE_REMOTE_INFORMATION	1025
#define	SMB_SFILEINFO_MAILSLOT_QUERY_INFORMATION	1026
#define	SMB_SFILEINFO_MAILSLOT_SET_INFORMATION		1027
#define	SMB_SFILEINFO_COMPRESSION_INFORMATION		1028
#define	SMB_SFILEINFO_OBJECT_ID_INFORMATION		1029
#define	SMB_SFILEINFO_COMPLETION_INFORMATION		1030
#define	SMB_SFILEINFO_MOVE_CLUSTER_INFORMATION		1031
#define	SMB_SFILEINFO_QUOTA_INFORMATION		1032
#define	SMB_SFILEINFO_REPARSE_POINT_INFORMATION	1033
#define	SMB_SFILEINFO_NETWORK_OPEN_INFORMATION	1034
#define	SMB_SFILEINFO_ATTRIBUTE_TAG_INFORMATION 1035
#define	SMB_SFILEINFO_TRACKING_INFORMATION	1036
#define	SMB_SFILEINFO_MAXIMUM_INFORMATION	1037

/*
 * LOCKING_ANDX LockType flags
 */
#define	SMB_LOCKING_ANDX_SHARED_LOCK	0x01
#define	SMB_LOCKING_ANDX_OPLOCK_RELEASE	0x02
#define	SMB_LOCKING_ANDX_CHANGE_LOCKTYPE 0x04
#define	SMB_LOCKING_ANDX_CANCEL_LOCK	0x08
#define	SMB_LOCKING_ANDX_LARGE_FILES	0x10


/*
 * size of the GUID returned in an extended security negotiate response
 */
#define	SMB_GUIDLEN	16

typedef uint16_t	smbfh;

/*
 * NTLMv2 blob header structure.
 */
struct ntlmv2_blobhdr {
	uint32_t	header;
	uint32_t	reserved;
	uint64_t	timestamp;
	uint64_t	client_nonce;
	uint32_t	unknown1;
};
typedef struct ntlmv2_blobhdr ntlmv2_blobhdr_t;

/*
 * NTLMv2 name header structure, for names in a blob.
 */
struct ntlmv2_namehdr {
	uint16_t	type;
	uint16_t	len;
};
typedef struct ntlmv2_namehdr ntlmv2_namehdr_t;

#define	NAMETYPE_EOL		0x0000	/* end of list of names */
#define	NAMETYPE_MACHINE_NB	0x0001	/* NetBIOS machine name */
#define	NAMETYPE_DOMAIN_NB	0x0002	/* NetBIOS domain name */
#define	NAMETYPE_MACHINE_DNS	0x0003	/* DNS machine name */
#define	NAMETYPE_DOMAIN_DNS	0x0004	/* DNS Active Directory domain name */

/*
 * Named pipe commands.
 */
#define	TRANS_CALL_NAMED_PIPE		0x54	/* open/write/read/close pipe */
#define	TRANS_WAIT_NAMED_PIPE		0x53	/* wait for pipe to be !busy */
#define	TRANS_PEEK_NAMED_PIPE		0x23	/* read but don't remove data */
#define	TRANS_Q_NAMED_PIPE_HAND_STATE	0x21	/* query pipe handle modes */
#define	TRANS_SET_NAMED_PIPE_HAND_STATE	0x01	/* set pipe handle modes */
#define	TRANS_Q_NAMED_PIPE_INFO		0x22	/* query pipe attributes */
#define	TRANS_TRANSACT_NAMED_PIPE	0x26	/* r/w operation on pipe */
#define	TRANS_READ_NAMED_PIPE		0x11	/* read pipe in "raw" mode */
						/* (non message mode) */
#define	TRANS_WRITE_NAMED_PIPE		0x31	/* write pipe "raw" mode */
						/* (non message mode) */

/*
 * Share types, visible via NetShareEnum
 */
#define	STYPE_DISKTREE			0x00000000
#define	STYPE_PRINTQ			0x00000001
#define	STYPE_DEVICE			0x00000002
#define	STYPE_IPC			0x00000003
#define	STYPE_UNKNOWN			0x00000004
#define	STYPE_MASK			0x0000000F
#define	STYPE_TEMPORARY			0x40000000
#define	STYPE_HIDDEN			0x80000000

/*
 * Characters that are not allowed in an SMB file name component.
 * From MSDN: Naming Files, Paths, ...
 *	< (less than)
 *	> (greater than)
 *	: (colon)
 *	" (double quote)
 *	/ (forward slash)
 *	\ (backslash)
 *	| (vertical bar or pipe)
 *	? (question mark)
 *	* (asterisk)
 */
#define	SMB_FILENAME_INVALID_CHARS	"<>:\"/\\|?*"

#endif /* _NETSMB_SMB_H_ */
/*
 * 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 2018 Nexenta Systems, Inc.  All rights reserved.
 * Copyright 2025 RackTop Systems, Inc.
 */

#ifndef _NETSMB_SMB2_H
#define	_NETSMB_SMB2_H

#ifdef __cplusplus
extern "C" {
#endif

#define	SMB2_PROTOCOL_ID	{ 0xFE, 'S', 'M', 'B' }
#define	SMB2_HDR_SIZE	64
#define	SMB2_HDRLEN	SMB2_HDR_SIZE

/*
 * SMB2 header command codes.
 * These are uint16_t on the wire.
 */
typedef enum {
	SMB2_NEGOTIATE = 0,
	SMB2_SESSION_SETUP,
	SMB2_LOGOFF,
	SMB2_TREE_CONNECT,
	SMB2_TREE_DISCONNECT,
	SMB2_CREATE,
	SMB2_CLOSE,
	SMB2_FLUSH,
	SMB2_READ,
	SMB2_WRITE,
	SMB2_LOCK,
	SMB2_IOCTL,
	SMB2_CANCEL,
	SMB2_ECHO,
	SMB2_QUERY_DIRECTORY,
	SMB2_CHANGE_NOTIFY,
	SMB2_QUERY_INFO,
	SMB2_SET_INFO,
	SMB2_OPLOCK_BREAK,
	/*
	 * The above (oplock break) is the last real SMB2 op-code.
	 * We use one more slot to represent invalid commands, and
	 * the final enum value is used for array sizes. Keep last!
	 */
	SMB2_INVALID_CMD,
	SMB2__NCMDS
} SMB2_cmd_code;

/*
 * SMB2 header flags.
 */

/*
 * SERVER_TO_REDIR
 * When set, indicates the message is a response rather than
 * a request. This MUST be set on responses sent from the
 * server to the client, and MUST NOT be set on requests
 * sent from the client to the server.
 */
#define	SMB2_FLAGS_SERVER_TO_REDIR	0x00000001

/*
 * ASYNC_COMMAND
 * When set, indicates that this is an ASYNC SMB2 header.
 * Always set for headers of the form described in this
 * section.
 */
#define	SMB2_FLAGS_ASYNC_COMMAND	0x00000002

/*
 * RELATED_OPERATIONS
 * When set in an SMB2 request, indicates that this request
 * is a related operation in a compounded request chain.
 * [MS-SMB2 sec. 3.2.4.1.4]
 *
 * When set in an SMB2 compound response, indicates that
 * the request corresponding to this response was part of a
 * related operation in a compounded request chain.
 * [MS-SMB2 sec. 3.3.5.2.7.2]
 */
#define	SMB2_FLAGS_RELATED_OPERATIONS	0x00000004

/*
 * SIGNED
 * When set, indicates that this packet has been signed.
 * [MS-SMB2 3.1.5.1]
 */
#define	SMB2_FLAGS_SIGNED	0x00000008

/*
 * [MS-SMB2] 3.2.5.3.1 The SessionKey MUST be set to the
 * first 16 bytes of the cryptographic key from GSSAPI.
 * (Padded with zeros if the GSSAPI key is shorter.)
 */
#define	SMB2_SESSION_KEY_LEN	16

/*
 * DFS_OPERATIONS
 * When set, indicates that this command is a Distributed
 * File System (DFS) operation.  [MS-SMB2 3.3.5.9]
 */
#define	SMB2_FLAGS_DFS_OPERATIONS	0x10000000

/*
 * REPLAY_OPERATION
 * This flag is only valid for the SMB 3.0 dialect. When set,
 * it indicates that this command is a replay operation.
 * The client MUST ignore this bit on receipt.
 */
#define	SMB2_FLAGS_REPLAY_OPERATION	0x20000000

/*
 * SMB2 Netgotiate [MS-SMB2 2.2.3]
 */

#define	SMB2_NEGOTIATE_SIGNING_ENABLED	0x01
#define	SMB2_NEGOTIATE_SIGNING_REQUIRED	0x02

#define	SMB2_CAP_DFS			0x00000001

/* Added with SMB2.1 */
#define	SMB2_CAP_DFS			0x00000001
#define	SMB2_CAP_LEASING		0x00000002
/*
 * LARGE_MTU:
 * When set, indicates that the client supports multi-credit operations.
 */
#define	SMB2_CAP_LARGE_MTU		0x00000004

/* Added with SMB3.0 */
#define	SMB2_CAP_MULTI_CHANNEL		0x00000008
#define	SMB2_CAP_PERSISTENT_HANDLES	0x00000010
#define	SMB2_CAP_DIRECTORY_LEASING	0x00000020
#define	SMB2_CAP_ENCRYPTION		0x00000040

/*
 * SMB3.1.1 Negotiate Context pre-authentication hash
 * there's only the one.
 */
#define	SMB3_HASH_SHA512	1

/*
 * SMB3.1.1 Negotiate Context encryption ciphers
 */
#define	SMB3_CIPHER_NONE	0
#define	SMB3_CIPHER_AES128_CCM	1
#define	SMB3_CIPHER_AES128_GCM	2
#define	SMB3_CIPHER_AES256_CCM	3
#define	SMB3_CIPHER_AES256_GCM	4

/*
 * SMB3.1.1 Negotiate Context signing algorithms
 */
#define	SMB3_SIGN_SHA256_HMAC	0	/* 2.x */
#define	SMB3_SIGN_AES128_CMAC	1	/* 3.0 */
#define	SMB3_SIGN_AES128_GMAC	2	/* 3.1.1 */


/*
 * SMB2 Session Setup [MS-SMB2 2.2.5]
 */

/* SMB2 session flags */
#define	SMB2_SESSION_FLAG_IS_GUEST	0x0001
#define	SMB2_SESSION_FLAG_IS_NULL	0x0002
#define	SMB2_SESSION_FLAG_ENCRYPT_DATA	0x0004

/*
 * SMB2 Tree connect, disconnect
 */

/* SMB2 sharetype flags */
#define	SMB2_SHARE_TYPE_DISK		0x1
#define	SMB2_SHARE_TYPE_PIPE		0x2
#define	SMB2_SHARE_TYPE_PRINT		0x3

/* SMB2 share flags */
#define	SMB2_SHAREFLAG_MANUAL_CACHING			0x00000000
#define	SMB2_SHAREFLAG_AUTO_CACHING			0x00000010
#define	SMB2_SHAREFLAG_VDO_CACHING			0x00000020
#define	SMB2_SHAREFLAG_NO_CACHING			0x00000030
#define	SMB2_SHAREFLAG_DFS				0x00000001
#define	SMB2_SHAREFLAG_DFS_ROOT				0x00000002
#define	SMB2_SHAREFLAG_RESTRICT_EXCLUSIVE_OPENS		0x00000100
#define	SMB2_SHAREFLAG_FORCE_SHARED_DELETE		0x00000200
#define	SMB2_SHAREFLAG_ALLOW_NAMESPACE_CACHING		0x00000400
#define	SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM	0x00000800
#define	SMB2_SHAREFLAG_FORCE_LEVELII_OPLOCK		0x00001000
/* SMB 3.0 */
#define	SMB2_SHAREFLAG_ENABLE_HASH_V1			0x00002000
#define	SMB2_SHAREFLAG_ENABLE_HASH_V2			0x00004000
#define	SMB2_SHAREFLAG_ENCRYPT_DATA			0x00008000

/* SMB2 share capabilities */
#define	SMB2_SHARE_CAP_DFS				0x00000008
/* SMB 3.0 */
#define	SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY		0x00000010
#define	SMB2_SHARE_CAP_SCALEOUT				0x00000020
#define	SMB2_SHARE_CAP_CLUSTER				0x00000040

/*
 * SMB2 Create (open)
 */

/* SMB2 requested oplock levels */
#define	SMB2_OPLOCK_LEVEL_NONE				0x00
#define	SMB2_OPLOCK_LEVEL_II				0x01
#define	SMB2_OPLOCK_LEVEL_EXCLUSIVE			0x08
#define	SMB2_OPLOCK_LEVEL_BATCH				0x09
#define	SMB2_OPLOCK_LEVEL_LEASE				0xFF

/* SMB2 impersonation levels */
#define	SMB2_IMPERSONATION_ANONYMOUS			0x00
#define	SMB2_IMPERSONATION_IDENTIFICATION		0x01
#define	SMB2_IMPERSONATION_IMPERSONATION		0x02
#define	SMB2_IMPERSONATION_DELEGATE			0x03

/*
 * Note: ShareAccess, CreateDispositon, CreateOptions,
 * all use the same definitions as SMB1 (from MS-FSA).
 * Ditto FileAccess flags (as with ACLs)
 */

/* SMB2 Create Context tags */

#define	SMB2_CREATE_EA_BUFFER			0x45787441 /* ("ExtA") */
/*
 * The data contains the extended attributes
 * that MUST be stored on the created file.
 * This value MUST NOT be set for named
 * pipes and print files.
 */

#define	SMB2_CREATE_SD_BUFFER			0x53656344 /* ("SecD") */
/*
 * The data contains a security descriptor that
 * MUST be stored on the created file.
 * This value MUST NOT be set for named
 * pipes and print files.
 */

#define	SMB2_CREATE_DURABLE_HANDLE_REQUEST	0x44486e51 /* ("DHnQ") */
/* The client is requesting the open to be durable */

#define	SMB2_CREATE_DURABLE_HANDLE_RECONNECT	0x44486e43 /* ("DHnC") */
/*
 * The client is requesting to reconnect to a
 * durable open after being disconnected
 */

#define	SMB2_CREATE_ALLOCATION_SIZE		0x416c5369 /* ("AISi") */
/*
 * The data contains the required allocation
 * size of the newly created file.
 */

#define	SMB2_CREATE_QUERY_MAXIMAL_ACCESS	0x4d784163 /* ("MxAc") */
/*
 * The client is requesting that the server
 * return maximal access information.
 */

#define	SMB2_CREATE_TIMEWARP_TOKEN		0x54577270 /* ("TWrp") */
/*
 * The client is requesting that the server
 * open an earlier version of the file identified
 * by the provided time stamp.
 */

#define	SMB2_CREATE_QUERY_ON_DISK_ID		0x51466964 /* ("QFid") */
/*
 * The client is requesting that the server return a 32-byte
 * opaque BLOB that uniquely identifies the file being opened
 * on disk. No data is passed to the server by the client.
 */

#define	SMB2_CREATE_REQUEST_LEASE		0x52714c73 /* ("RqLs") */
/*
 * The client is requesting that the server return a lease.
 * This value is only supported for the SMB 2.1 and 3.0 dialects.
 */

/* SMB2 create request lease */
#define	SMB2_LEASE_NONE				0x00
#define	SMB2_LEASE_READ_CACHING			0x01
#define	SMB2_LEASE_HANDLE_CACHING		0x02
#define	SMB2_LEASE_WRITE_CACHING		0x04

/* SMB2 lease break notification flags */
#define	SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED  0x01

/*
 * SMB2 Close
 */
#define	SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB	0x0001

/*
 * SMB2 Write
 */
#define	SMB2_WRITEFLAG_WRITE_THROUGH		0x00000001

/*
 * SMB2 Lock Request
 */

/* SMB2 lock flags */

/*
 * SMB2_LOCKFLAG_SHARED_LOCK
 * The range MUST be locked shared, allowing other opens
 * to read from or take a shared lock on the range. All opens
 * MUST NOT be allowed to write within the range. Other
 * locks can be requested and taken on this range.
 */
#define	SMB2_LOCKFLAG_SHARED_LOCK	0x00000001

/*
 * SMB2_LOCKFLAG_EXCLUSIVE_LOCK
 * The range MUST be locked exclusive, not allowing other
 * opens to read, write, or lock within the range.
 */
#define	SMB2_LOCKFLAG_EXCLUSIVE_LOCK	0x00000002

/*
 * SMB2_LOCKFLAG_UNLOCK
 * The range MUST be unlocked from a previous lock taken
 * on this range. The unlock range MUST be identical to the
 * lock range. Sub-ranges cannot be unlocked.
 */
#define	SMB2_LOCKFLAG_UNLOCK		0x00000004

/*
 * SMB2_LOCKFLAG_FAIL_IMMEDIATELY
 * The lock operation MUST fail immediately if it conflicts
 * with an existing lock, instead of waiting for the range to
 * become available.  This can be OR'ed with either of
 * shared_lock, exclusive_lock (nothing else).
 */
#define	SMB2_LOCKFLAG_FAIL_IMMEDIATELY	0x00000010

/*
 * SMB2 Ioctl Request
 */
#define	SMB2_IOCTL_IS_FSCTL		0x00000001


/*
 * SMB2 Query Directory
 */

/*
 * SMB2 query directory info levels
 * Same as SMB1 (see ntifs.h)
 */

/*
 * SMB2 Query Directory Flags
 * (our own names for these - spec. used poor names)
 */
#define	SMB2_QDIR_FLAG_RESTART		0x01 /* SMB2_RESTART_SCANS */
#define	SMB2_QDIR_FLAG_SINGLE		0x02 /* SMB2_RETURN_SINGLE_ENTRY */
#define	SMB2_QDIR_FLAG_INDEX		0x04 /* SMB2_INDEX_SPECIFIED */
#define	SMB2_QDIR_FLAG_REOPEN		0x10 /* SMB2_REOPEN */

/*
 * SMB2 Query Info Request
 */

/* info type */
#define	SMB2_0_INFO_FILE		0x01
/* The file information is requested. */
#define	SMB2_0_INFO_FILESYSTEM		0x02
/* The underlying object store information is requested. */
#define	SMB2_0_INFO_SECURITY		0x03
/* The security information is requested. */
#define	SMB2_0_INFO_QUOTA		0x04
/* The underlying object store quota information is requested. */

/*
 * MS-FSCC 2.5 FileSystem Information Classes.
 * Also see MSDN for ZwQueryVolumeInformationFile.
 */
typedef enum _FS_INFORMATION_CLASS
{
	FileFsVolumeInformation		= 1, /* Query */
	FileFsLabelInformation		= 2, /* Set */
	FileFsSizeInformation		= 3, /* Query */
	FileFsDeviceInformation		= 4, /* Query */
	FileFsAttributeInformation	= 5, /* Query */
	FileFsControlInformation	= 6, /* Query, Set */
	FileFsFullSizeInformation	= 7, /* Query */
	FileFsObjectIdInformation	= 8, /* Query, Set */
	FileFsDriverPathInformation	= 9 /* Query */
} FS_INFORMATION_CLASS;

/*
 * MS-FSCC 2.4 File Information Classes
 */
typedef enum _FILE_INFORMATION_CLASS
{
	FileDirectoryInformation	= 1,
	FileFullDirectoryInformation	= 2,
	FileBothDirectoryInformation	= 3,
	FileBasicInformation		= 4,
	FileStandardInformation		= 5,
	FileInternalInformation		= 6,
	FileEaInformation		= 7,
	FileAccessInformation		= 8,
	FileNameInformation		= 9,
	FileRenameInformation		= 10,
	FileLinkInformation		= 11,
	FileNamesInformation		= 12,
	FileDispositionInformation	= 13,
	FilePositionInformation		= 14,
	FileFullEaInformation		= 15,
	FileModeInformation		= 16,
	FileAlignmentInformation	= 17,
	FileAllInformation		= 18,
	FileAllocationInformation	= 19,
	FileEndOfFileInformation	= 20,
	FileAlternateNameInformation	= 21,
	FileStreamInformation		= 22,
	FilePipeInformation		= 23,
	FilePipeLocalInformation	= 24,
	FilePipeRemoteInformation	= 25,
	FileMailslotQueryInformation	= 26,
	FileMailslotSetInformation	= 27,
	FileCompressionInformation	= 28,
	FileObjectIdInformation		= 29,
	FileMoveClusterInformation	= 31,
	FileQuotaInformation		= 32,
	FileReparsePointInformation	= 33,
	FileNetworkOpenInformation	= 34,
	FileAttributeTagInformation	= 35,
	FileTrackingInformation		= 36,
	FileIdBothDirectoryInformation	= 37,
	FileIdFullDirectoryInformation	= 38,
	FileValidDataLengthInformation	= 39,
	FileShortNameInformation	= 40,
	FileSfioReserveInformation	= 44,
	FileSfioVolumeInformation	= 45,
	FileHardLinkInformation		= 46,
	FileNormalizedNameInformation	= 48,
	FileIdGlobalTxDirectoryInformation = 50,
	FileStandardLinkInformation	= 54
} FILE_INFORMATION_CLASS;

/*
 * SMB2 Change Nofity Request
 */
#define	SMB2_WATCH_TREE			0x00000001

/*
 * After here, added stuff from darwin
 */
#define	SMB2_TID_UNKNOWN	0
#define	SMB2_FID_UNUSED		0xffffffffffffffff

/* smb2_durable_handle flags */
typedef enum _SMB2_DURABLE_HANDLE_FLAGS
{
	SMB2_DURABLE_HANDLE_REQUEST = 0x0001,
	SMB2_DURABLE_HANDLE_RECONNECT = 0x0002,
	SMB2_DURABLE_HANDLE_GRANTED = 0x0004,
	SMB2_LEASE_GRANTED = 0x0008
} _SMB2_DURABLE_HANDLE_FLAGS;

struct smb2_durable_handle {
	uint64_t fid;	/* SMBFID to reconnect in durable handle reconnect */
	uint64_t flags;
	uint64_t lease_key_hi;	/* atomic increment number */
	uint64_t lease_key_low;	/* node hash value */
	uint32_t lease_state;
	uint32_t pad;
};

#ifdef __cplusplus
}
#endif

#endif /* _NETSMB_SMB2_H */
/*
 * Copyright (c) 2000-2001 Boris Popov
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by Boris Popov.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * 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.
 *
 * $Id: smb_dev.h,v 1.10.178.1 2005/05/27 02:35:29 lindak Exp $
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
 * Copyright 2021-2025 RackTop Systems, Inc.
 */

#ifndef _NETSMB_DEV_H_
#define	_NETSMB_DEV_H_

/*
 * This file defines an internal ABI for the "nsmb" driver,
 * particularly the various data structures passed to ioctl.
 * In order to avoid some messy 32-bit to 64-bit conversions
 * in the driver, we take pains to define all data structures
 * that pass across the user/kernel boundary in a way that
 * makes them invariant across 32-bit and 64-bit ABIs.
 * This invariance is checked during the driver build
 * using a mechanism similar to genassym.h builds.
 *
 * If you change any of the ioctl data structures in
 * this file, YOU MUST ALSO edit this file:
 *   uts/common/fs/smbclnt/netsmb/offsets.in
 * and then verify the invariance describe above.
 *
 * Also, remember to "bump" NSMB_VER below when
 * any part of this user/kernel I/F changes.
 */

#include <sys/types.h>
#include <sys/socket_impl.h>
#include <netinet/in.h>

#define	NSMB_NAME		"nsmb"

/*
 * Update NSMB_VER* if any of the ioctl codes and/or
 * associated structures change in ways that would
 * make them incompatible with an old driver.
 */
#define	NSMB_VERMAJ	2
#define	NSMB_VERMIN	0x100
#define	NSMB_VERSION	((NSMB_VERMAJ << 16) | NSMB_VERMIN)

/*
 * Some errno values we need to expose to the library.
 * NB: these are also defined in the library smbfs_api.h
 * to avoid exposing all of this stuff in that API.
 *
 * EBADRPC is used for message decoding errors.
 * EAUTH is used for CIFS authentication errors.
 */
#ifndef EBADRPC
#define	EBADRPC		113
#endif
#ifndef EAUTH
#define	EAUTH		114
#endif

/*
 * Upper/lower case options
 */
#define	SMB_CS_NONE	0x0000
#define	SMB_CS_UPPER	0x0001	/* convert passed string to upper case */
#define	SMB_CS_LOWER	0x0002	/* convert passed string to lower case */

/*
 * access mode stuff (see also smb_lib.h)
 */
#define	SMBM_ANY_OWNER		((uid_t)-1)
#define	SMBM_ANY_GROUP		((gid_t)-1)

/*
 * Option flags in smbioc_ossn.ioc_opt
 * and vcspec.optflags
 */
#define	SMBVOPT_CREATE		0x0001	/* create object if necessary */
#define	SMBVOPT_PRIVATE		0x0002	/* connection should be private */
#define	SMBVOPT_SINGLESHARE	0x0004	/* keep only one share at this VC */
#define	SMBVOPT_PERMANENT	0x0010	/* object will keep last reference */
#define	SMBVOPT_ANONYMOUS	0x0020	/* using a NULL session */

#define	SMBVOPT_SIGNING_ENABLED		0x10000	/* sign if server agrees */
#define	SMBVOPT_SIGNING_REQUIRED	0x20000 /* signing required */
#define	SMBVOPT_SIGNING_MASK		0x30000	/* all signing bits */

#define	SMB2_DIALECT_BASE	0x0200
#define	SMB2_DIALECT_0202	0x0202
#define	SMB2_DIALECT_02ff	0x02ff
#define	SMB2_DIALECT_0210	0x0210
#define	SMB2_DIALECT_0300	0x0300
#define	SMB2_DIALECT_0302	0x0302
#define	SMB2_DIALECT_0311	0x0311

/* Maximum supported dialect (for ssn_maxver) */
#define	SMB2_DIALECT_MAX	SMB2_DIALECT_0311

/*
 * Option flags in smbioc_oshare.ioc_opt
 * and sharespec.optflags
 */
#define	SMBSOPT_CREATE		SMBVOPT_CREATE
#define	SMBSOPT_PERMANENT	SMBVOPT_PERMANENT

/* All user and machine names. */
#define	SMBIOC_MAX_NAME		256

/*
 * Size of storage for p/w hashes.
 * Also for SMBIOC_GETSSNKEY.
 */
#define	SMBIOC_HASH_SZ	16

/*
 * network IO daemon states
 */
enum smbiod_state {
	SMBIOD_ST_UNINIT = 0,	/* uninitialized */
	SMBIOD_ST_RECONNECT,	/* a [re]connect attempt requested */
	SMBIOD_ST_RCFAILED,	/* a reconnect attempt has failed */
	SMBIOD_ST_CONNECTED,	/* Transport (TCP) connected */
	SMBIOD_ST_NEGOTIATED,	/* Negotiated SMB/SMB2+ */
	SMBIOD_ST_AUTHCONT,	/* Session setup continuing */
	SMBIOD_ST_AUTHFAIL,	/* Session setup failed */
	SMBIOD_ST_AUTHOK,	/* Session setup success */
	SMBIOD_ST_VCACTIVE,	/* iod_work running */
	SMBIOD_ST_IDLE,		/* no trees, will go DEAD */
	SMBIOD_ST_DEAD		/* connection gone, no IOD */
};


/*
 * We're now using structures that are invariant
 * across 32-bit vs 64-bit compilers for all
 * member sizes and offsets.  Scalar members
 * simply have to use fixed-size types.
 * Pointers are a little harder...
 * We use this union for all pointers that
 * must pass between user and kernel.
 */
typedef union lptr {
	uint64_t lp_ll;
#ifdef _LP64
	void	*lp_ptr;
#endif
#ifdef _ILP32
	void	*_lp_p2[2];
#ifdef _LITTLE_ENDIAN
#define	lp_ptr	_lp_p2[0]
#define	lp_pad	_lp_p2[1]
#else /* _ENDIAN */
#define	lp_pad	_lp_p2[0]
#define	lp_ptr	_lp_p2[1]
#endif /* _ENDIAN */
#endif /* _ILP32 */
} lptr_t;

/*
 * Handy union of sockaddr types we use.
 * Type discriminator is sa_family
 */
union smbioc_sockaddr {
	struct sockaddr sa;	/* generic */
	struct sockaddr_in sin;
	struct sockaddr_in6 sin6;
};
typedef union smbioc_sockaddr smbioc_sockaddr_t;

/*
 * This is what identifies a session.
 */
struct smbioc_ssn_ident {
	smbioc_sockaddr_t id_srvaddr;
	char		id_domain[SMBIOC_MAX_NAME];
	char		id_user[SMBIOC_MAX_NAME];
};
typedef struct smbioc_ssn_ident smbioc_ssn_ident_t;

/*
 * Flags for smbioc_ossn.ssn_opt
 */
#define	SMBLK_CREATE		SMBVOPT_CREATE

/*
 * Structure used with SMBIOC_SSN_FIND, _CREATE
 */
struct smbioc_ossn {
	uint32_t		ssn_owner;	/* Unix owner (UID) */
	uint32_t		ssn_vopt;	/* i.e. SMBVOPT_CREATE */
	uint16_t		ssn_minver;	/* Min SMB version. */
	uint16_t		ssn_maxver;	/* Max SMB version. */
	smbioc_ssn_ident_t	ssn_id;
	char			ssn_srvname[SMBIOC_MAX_NAME];
};
typedef struct smbioc_ossn smbioc_ossn_t;
/* Convenience names for members under ssn_id */
#define	ssn_srvaddr	ssn_id.id_srvaddr
#define	ssn_domain	ssn_id.id_domain
#define	ssn_user	ssn_id.id_user

/*
 * Structure used with SMBIOC_TREE_FIND, _CONNECT
 */
struct smbioc_oshare {
	uint32_t	sh_use;		/* requested */
	uint32_t	sh_type;	/* returned */
	char		sh_name[SMBIOC_MAX_NAME];
	char		sh_pass[SMBIOC_MAX_NAME];
};
typedef struct smbioc_oshare smbioc_oshare_t;

typedef struct smbioc_tcon {
	int32_t		tc_flags;
	int32_t		tc_opt;
	smbioc_oshare_t	tc_sh;
} smbioc_tcon_t;

/*
 * This is the operational state information passed
 * in and out of the driver for SMBIOC_SSN_WORK
 */
struct smbioc_ssn_work {
	uint32_t	wk_out_state;	/* out-only */
	uint32_t	wk_u_ssnkey_len; /* ssn key length */
	lptr_t		wk_u_ssnkey_buf; /* user-space ptr! */
	uint32_t	wk_u_auth_rlen;	/* recv auth tok len */
	uint32_t	wk_u_auth_wlen;	/* send auth tok len */
	lptr_t		wk_u_auth_rbuf;	/* recv auth tok buf */
	lptr_t		wk_u_auth_wbuf;	/* send auth tok buf */
	uint8_t		wk_cl_guid[16];	/* client GUID */
};
typedef struct smbioc_ssn_work smbioc_ssn_work_t;

/*
 * User-level SMB requests
 */

typedef struct smbioc_rw {
	uint32_t	ioc_cnt;
	uint32_t	ioc_flags;
	lloff_t	_ioc_offset;
	lptr_t	_ioc_base;
} smbioc_rw_t;
#define	ioc_offset	_ioc_offset._f
#define	ioc_base	_ioc_base.lp_ptr

/* Transact on named pipe (send/recv) */
typedef struct smbioc_xnp {
	uint32_t	ioc_tdlen;	/* transmit len */
	uint32_t	ioc_rdlen;	/* recv maxlen */
	uint32_t	ioc_more;	/* more data to read */
	uint32_t	ioc_pad1;
	lptr_t		_ioc_tdata;
	lptr_t		_ioc_rdata;
} smbioc_xnp_t;
#define	ioc_tdata	_ioc_tdata.lp_ptr
#define	ioc_rdata	_ioc_rdata.lp_ptr

typedef struct smbioc_ntcreate {
	uint32_t	ioc_req_acc;
	uint32_t	ioc_efattr;
	uint32_t	ioc_share_acc;
	uint32_t	ioc_open_disp;
	uint32_t	ioc_creat_opts;
	char		ioc_name[SMBIOC_MAX_NAME];
} smbioc_ntcreate_t;

typedef struct smbioc_printjob {
	uint16_t	ioc_setuplen;
	uint16_t	ioc_prmode;
	char		ioc_title[SMBIOC_MAX_NAME];
} smbioc_printjob_t;

/* Password Keychain (PK) support. */
typedef struct smbioc_pk {
	uid_t	pk_uid;				/* UID for PAM use */
	char pk_dom[SMBIOC_MAX_NAME];		/* CIFS domain name */
	char pk_usr[SMBIOC_MAX_NAME];		/* CIFS user name */
	uchar_t pk_lmhash[SMBIOC_HASH_SZ];	/* LanMan p/w hash */
	uchar_t pk_nthash[SMBIOC_HASH_SZ];	/* NTLM p/w hash */
} smbioc_pk_t;


/*
 * Device IOCTLs
 *
 * Define ioctl codes the way ZFS does.
 * The "base" value is arbitrary, and can
 * occupy the high word if we like, because
 * our driver does its own copyin/copyout.
 * Keep GETVERS first and use it to verify
 * driver compatibility with the library.
 */
#define	SMBIOC_BASE	((('n' << 8) | 's') << 8)
typedef enum nsmb_ioc {
	SMBIOC_GETVERS = SMBIOC_BASE,	/* keep first */
	SMBIOC_FLAGS2,		/* obsolete */
	SMBIOC_GETSSNKEY,	/* get SMB session key */
	SMBIOC_DUP_DEV,		/* duplicate dev handle */

	SMBIOC_READ,		/* read (pipe) */
	SMBIOC_WRITE,		/* write (pipe) */
	SMBIOC_XACTNP,		/* "transact" (pipe) */
	SMBIOC_NTCREATE,	/* open or create */
	SMBIOC_PRINTJOB,	/* open print job */
	SMBIOC_CLOSEFH,		/* from ntcreate or printjob */

	SMBIOC_SSN_CREATE,
	SMBIOC_SSN_FIND,
	SMBIOC_SSN_KILL,	/* force disconnect */
	SMBIOC_SSN_RELE,	/* drop our reference */

	SMBIOC_TREE_CONNECT,	/* create and connect */
	SMBIOC_TREE_FIND,
	SMBIOC_TREE_KILL,
	SMBIOC_TREE_RELE,

	SMBIOC_IOD_CONNECT,	/* Setup connection */
	SMBIOC_IOD_NEGOTIATE,	/* SMB/SMB2 negotiate */
	SMBIOC_IOD_SSNSETUP,	/* SMB/SMB2 session setup */
	SMBIOC_IOD_WORK,	/* work on session requests */
	SMBIOC_IOD_IDLE,	/* wait for requests on this session */
	SMBIOC_IOD_RCFAIL,	/* tell driver reconnect failed */

	/* Password Keychain (PK) support. */
	SMBIOC_PK_ADD,    /* Add/Modify a password entry */
	SMBIOC_PK_CHK,    /* Check for a password entry */
	SMBIOC_PK_DEL,    /* Delete specified password entry */
	SMBIOC_PK_DEL_OWNER,	/* all owned by the caller */
	SMBIOC_PK_DEL_EVERYONE	/* all owned by everyone */
} nsmb_ioc_t;

#endif /* _NETSMB_DEV_H_ */