|
root / base / usr / src / uts / common / fs / smbsrv / smb2_dispatch.c
smb2_dispatch.c C 1708 lines 46.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
/*
 * 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 2015-2021 Tintri by DDN, Inc. All rights reserved.
 * Copyright 2020-2024 RackTop Systems, Inc.
 */


#include <smbsrv/smb2_kproto.h>
#include <smbsrv/smb_kstat.h>
#include <smbsrv/smb2.h>

#define	SMB2_ASYNCID(sr) (sr->smb2_messageid ^ (1ULL << 62))

smb_sdrc_t smb2_invalid_cmd(smb_request_t *);
static void smb2_tq_work(void *);
static void smb2sr_run_postwork(smb_request_t *);
static int smb3_decrypt_msg(smb_request_t *);

static const smb_disp_entry_t
smb2_disp_table[SMB2__NCMDS] = {

	/* text-name, pre, func, post, cmd-code, dialect, flags */

	{  "smb2_negotiate", NULL,
	    smb2_negotiate, NULL, 0, 0,
	    SDDF_SUPPRESS_TID | SDDF_SUPPRESS_UID },

	{  "smb2_session_setup", NULL,
	    smb2_session_setup, NULL, 0, 0,
	    SDDF_SUPPRESS_TID | SDDF_SUPPRESS_UID },

	{  "smb2_logoff", NULL,
	    smb2_logoff, NULL, 0, 0,
	    SDDF_SUPPRESS_TID },

	{  "smb2_tree_connect", NULL,
	    smb2_tree_connect, NULL, 0, 0,
	    SDDF_SUPPRESS_TID },

	{  "smb2_tree_disconn", NULL,
	    smb2_tree_disconn, NULL, 0, 0 },

	{  "smb2_create", NULL,
	    smb2_create, NULL, 0, 0 },

	{  "smb2_close", NULL,
	    smb2_close, NULL, 0, 0 },

	{  "smb2_flush", NULL,
	    smb2_flush, NULL, 0, 0 },

	{  "smb2_read", NULL,
	    smb2_read, NULL, 0, 0 },

	{  "smb2_write", NULL,
	    smb2_write, NULL, 0, 0 },

	{  "smb2_lock", NULL,
	    smb2_lock, NULL, 0, 0 },

	{  "smb2_ioctl", NULL,
	    smb2_ioctl, NULL, 0, 0 },

	{  "smb2_cancel", NULL,
	    smb2_cancel, NULL, 0, 0,
	    SDDF_SUPPRESS_UID | SDDF_SUPPRESS_TID },

	{  "smb2_echo", NULL,
	    smb2_echo, NULL, 0, 0,
	    SDDF_SUPPRESS_UID | SDDF_SUPPRESS_TID },

	{  "smb2_query_dir", NULL,
	    smb2_query_dir, NULL, 0, 0 },

	{  "smb2_change_notify", NULL,
	    smb2_change_notify, NULL, 0, 0 },

	{  "smb2_query_info", NULL,
	    smb2_query_info, NULL, 0, 0 },

	{  "smb2_set_info", NULL,
	    smb2_set_info, NULL, 0, 0 },

	{  "smb2_oplock_break_ack", NULL,
	    smb2_oplock_break_ack, NULL, 0, 0 },

	{  "smb2_invalid_cmd", NULL,
	    smb2_invalid_cmd, NULL, 0, 0,
	    SDDF_SUPPRESS_UID | SDDF_SUPPRESS_TID },
};

smb_sdrc_t
smb2_invalid_cmd(smb_request_t *sr)
{
#ifdef	DEBUG
	cmn_err(CE_NOTE, "clnt %s bad SMB2 cmd code",
	    sr->session->ip_addr_str);
#endif
	sr->smb2_status = NT_STATUS_INVALID_PARAMETER;
	return (SDRC_DROP_VC);
}

/*
 * This is the SMB2 handler for new smb requests, called from
 * smb_session_reader after SMB negotiate is done.  For most SMB2
 * requests, we just enqueue them for the smb_session_worker to
 * execute via the task queue, so they can block for resources
 * without stopping the reader thread.  A few protocol messages
 * are special cases and are handled directly here in the reader
 * thread so they don't wait for taskq scheduling.
 *
 * This function must either enqueue the new request for
 * execution via the task queue, or execute it directly
 * and then free it.  If this returns non-zero, the caller
 * will drop the session.
 */
int
smb2sr_newrq(smb_request_t *sr)
{
	struct mbuf_chain *mbc = &sr->command;
	taskqid_t tqid;
	uint32_t magic;
	int rc, skip;

	if (smb_mbc_peek(mbc, 0, "l", &magic) != 0)
		goto drop;

	/* 0xFD S M B */
	if (magic == SMB3_ENCRYPTED_MAGIC) {
		if (smb3_decrypt_msg(sr) != 0)
			goto drop;
		/*
		 * Should now be looking at an un-encrypted
		 * SMB2 message header.
		 */
		if (smb_mbc_peek(mbc, 0, "l", &magic) != 0)
			goto drop;
	}

	if (magic != SMB2_PROTOCOL_MAGIC)
		goto drop;

	/*
	 * Walk the SMB2 commands in this compound message and
	 * keep track of the range of message IDs it uses.
	 */
	for (;;) {
		if (smb2_decode_header(sr) != 0)
			goto drop;

		/*
		 * Cancel requests are special:  They refer to
		 * an earlier message ID (or an async. ID),
		 * never a new ID, and are never compounded.
		 * This is intentionally not "goto drop"
		 * because rc may be zero (success).
		 */
		if (sr->smb2_cmd_code == SMB2_CANCEL) {
			rc = smb2_newrq_cancel(sr);
			smb_request_free(sr);
			return (rc);
		}

		/*
		 * Keep track of the total credits in this compound
		 * and the first (real) message ID (not: 0, -1)
		 * While we're looking, verify that all (real) IDs
		 * are (first <= ID < (first + msg_credits))
		 */
		if (sr->smb2_credit_charge == 0)
			sr->smb2_credit_charge = 1;
		sr->smb2_total_credits += sr->smb2_credit_charge;

		if (sr->smb2_messageid != 0 &&
		    sr->smb2_messageid != UINT64_MAX) {

			if (sr->smb2_first_msgid == 0)
				sr->smb2_first_msgid = sr->smb2_messageid;

			if (sr->smb2_messageid < sr->smb2_first_msgid ||
			    sr->smb2_messageid >= (sr->smb2_first_msgid +
			    sr->smb2_total_credits)) {
				long long id = (long long) sr->smb2_messageid;
				cmn_err(CE_WARN, "clnt %s msg ID 0x%llx "
				    "out of sequence in compound",
				    sr->session->ip_addr_str, id);
			}
		}

		/* Normal loop exit on next == zero */
		if (sr->smb2_next_command == 0)
			break;

		/* Abundance of caution... */
		if (sr->smb2_next_command < SMB2_HDR_SIZE)
			goto drop;

		/* Advance to the next header. */
		skip = sr->smb2_next_command - SMB2_HDR_SIZE;
		if (MBC_ROOM_FOR(mbc, skip) == 0)
			goto drop;
		mbc->chain_offset += skip;
	}
	/* Rewind back to the top. */
	mbc->chain_offset = 0;

	/*
	 * Submit the request to the task queue, which calls
	 * smb2_tq_work when the workload permits.
	 */
	sr->sr_time_submitted = gethrtime();
	sr->sr_state = SMB_REQ_STATE_SUBMITTED;
	smb_srqueue_waitq_enter(sr->session->s_srqueue);
	tqid = taskq_dispatch(sr->sr_server->sv_worker_pool,
	    smb2_tq_work, sr, TQ_SLEEP);
	VERIFY(tqid != TASKQID_INVALID);

	return (0);

drop:
	smb_request_free(sr);
	return (-1);
}

static void
smb2_tq_work(void *arg)
{
	smb_request_t	*sr;
	smb_srqueue_t	*srq;

	sr = (smb_request_t *)arg;
	SMB_REQ_VALID(sr);

	srq = sr->session->s_srqueue;
	smb_srqueue_waitq_to_runq(srq);
	sr->sr_worker = curthread;
	sr->sr_time_active = gethrtime();

	/*
	 * Always dispatch to the work function, because cancelled
	 * requests need an error reply (NT_STATUS_CANCELLED).
	 */
	mutex_enter(&sr->sr_mutex);
	if (sr->sr_state == SMB_REQ_STATE_SUBMITTED)
		sr->sr_state = SMB_REQ_STATE_ACTIVE;
	mutex_exit(&sr->sr_mutex);

	smb2sr_work(sr);

	smb_srqueue_runq_exit(srq);
}

/*
 * Wrapper to setup a new mchain for the plaintext request that will
 * replace the encrypted one.  Returns non-zero to drop the connection.
 * Error return values here are just for visibility in dtrace.
 */
static int
smb3_decrypt_msg(smb_request_t *sr)
{
	struct mbuf_chain clear_mbc = {0};
	struct mbuf_chain tmp_mbc;
	mbuf_t *m;
	int clearsize;
	int rc;

	if (sr->session->dialect < SMB_VERS_3_0) {
		/* Encrypted message in SMB 2.x */
		return (-1);
	}
	if ((sr->session->srv_cap & SMB2_CAP_ENCRYPTION) == 0) {
		/* Should have srv_cap SMB2_CAP_ENCRYPTION flag set! */
		return (-2);
	}

	sr->encrypted = B_TRUE;
	if (sr->command.max_bytes <
	    (SMB3_TFORM_HDR_SIZE + SMB2_HDR_SIZE)) {
		/* Short transform header */
		return (-3);
	}
	clearsize = sr->command.max_bytes - SMB3_TFORM_HDR_SIZE;

	clear_mbc.max_bytes = clearsize;
	m = smb_mbuf_alloc_chain(clearsize);
	MBC_ATTACH_MBUF(&clear_mbc, m);

	rc = smb3_decrypt_sr(sr, &sr->command, &clear_mbc);
	if (rc != 0) {
		MBC_FLUSH(&clear_mbc);
		return (rc);
	}

	/* Swap clear_mbc in place of command */
	tmp_mbc = sr->command;
	sr->command = clear_mbc;
	MBC_FLUSH(&tmp_mbc);	// free old sr->command

	return (0);
}

/*
 * SMB2 credits determine how many simultaneous commands the
 * client may issue, and bounds the range of message IDs those
 * commands may use.  With multi-credit support, commands may
 * use ranges of message IDs, where the credits used by each
 * command are proportional to their data transfer size.
 *
 * Every command may request an increase or decrease of
 * the currently granted credits, based on the difference
 * between the credit request and the credit charge.
 * [MS-SMB2] 3.3.1.2 Algorithm for the Granting of Credits
 *
 * Most commands have credit_request=1, credit_charge=1,
 * which keeps the credit grant unchanged.
 *
 * All we're really doing here (for now) is reducing the
 * credit_response if the client requests a credit increase
 * that would take their credit over the maximum, and
 * limiting the decrease so they don't run out of credits.
 *
 * Later, this could do something dynamic based on load.
 *
 * One other non-obvious bit about credits: We keep the
 * session s_max_credits low until the 1st authentication,
 * at which point we'll set the normal maximum_credits.
 * Some clients ask for more credits with session setup,
 * and we need to handle that requested increase _after_
 * the command-specific handler returns so it won't be
 * restricted to the lower (pre-auth) limit.
 */
static inline void
smb2_credit_decrease(smb_request_t *sr)
{
	smb_session_t *session = sr->session;
	uint16_t cur, d;

	ASSERT3U(sr->smb2_credit_request, <, sr->smb2_credit_charge);

	mutex_enter(&session->s_credits_mutex);
	cur = session->s_cur_credits;
	ASSERT(cur > 0);

	/* Handle credit decrease. */
	d = sr->smb2_credit_charge - sr->smb2_credit_request;

	/*
	 * Prevent underflow of current credits, and
	 * enforce a minimum of one credit, per:
	 * [MS-SMB2] 3.3.1.2
	 */
	if (d >= cur) {
		/*
		 * Tried to give up more credits than we should.
		 * Reduce the decrement.
		 */
		d = cur - 1;
		cur = 1;
		DTRACE_PROBE1(smb2__credit__neg, smb_request_t *, sr);
	} else {
		cur -= d;
	}

	ASSERT3U(d, <=, sr->smb2_credit_charge);
	sr->smb2_credit_response = sr->smb2_credit_charge - d;

	DTRACE_PROBE3(smb2__credit__decrease,
	    smb_request_t *, sr, int, (int)cur,
	    int, (int)session->s_cur_credits);

	session->s_cur_credits = cur;
	mutex_exit(&session->s_credits_mutex);
}

/*
 * Second half of SMB2 credit handling (increases)
 */
static inline void
smb2_credit_increase(smb_request_t *sr)
{
	smb_session_t *session = sr->session;
	uint16_t cur, d;

	ASSERT3U(sr->smb2_credit_request, >, sr->smb2_credit_charge);

	mutex_enter(&session->s_credits_mutex);
	cur = session->s_cur_credits;

	/* Handle credit increase. */
	d = sr->smb2_credit_request - sr->smb2_credit_charge;

	/*
	 * If new credits would be above max,
	 * reduce the credit grant.
	 */
	if (d > (session->s_max_credits - cur)) {
		d = session->s_max_credits - cur;
		cur = session->s_max_credits;
		DTRACE_PROBE1(smb2__credit__max, smb_request_t *, sr);
	} else {
		cur += d;
	}
	sr->smb2_credit_response = sr->smb2_credit_charge + d;

	DTRACE_PROBE3(smb2__credit__increase,
	    smb_request_t *, sr, int, (int)cur,
	    int, (int)session->s_cur_credits);

	session->s_cur_credits = cur;
	mutex_exit(&session->s_credits_mutex);
}

/*
 * Record some statistics:  latency, rx bytes, tx bytes
 * per:  server, session & kshare.
 */
inline void
smb2_record_stats(smb_request_t *sr, smb_disp_stats_t *sds, boolean_t need_lat)
{
	int64_t		rxb;
	int64_t		txb;

	rxb = (int64_t)(sr->command.chain_offset - sr->smb2_cmd_hdr);
	txb = (int64_t)(sr->reply.chain_offset - sr->smb2_reply_hdr);
	txb += sr->smb2_async_txb;
	sr->smb2_async_txb = 0;

	smb_server_inc_req(sr->sr_server);
	atomic_add_64(&sds->sdt_rxb, rxb);
	atomic_add_64(&sds->sdt_txb, txb);

	if (need_lat) {
		hrtime_t dt;
		dt = gethrtime() - sr->sr_time_start;
		smb_latency_add_sample(&sds->sdt_lat, dt);
	}
}

/*
 * smb2sr_work
 *
 * This function processes each SMB command in the current request
 * (which may be a compound request) building a reply containing
 * SMB reply messages, one-to-one with the SMB commands.  Some SMB
 * commands (change notify, blocking locks) may require both an
 * "interim response" and a later "async response" at completion.
 * In such cases, we'll encode the interim response in the reply
 * compound we're building, and put the (now async) command on a
 * list of commands that need further processing.  After we've
 * finished processing the commands in this compound and building
 * the compound reply, we'll send the compound reply, and finally
 * process the list of async commands.
 *
 * As we work our way through the compound request and reply,
 * we need to keep track of the bounds of the current request
 * and reply.  For the request, this uses an MBC_SHADOW_CHAIN
 * that begins at smb2_cmd_hdr.  The reply is appended to the
 * sr->reply chain starting at smb2_reply_hdr.
 *
 * This function must always free the smb request, or arrange
 * for it to be completed and free'd later (if SDRC_SR_KEPT).
 */
void
smb2sr_work(struct smb_request *sr)
{
	const smb_disp_entry_t	*sdd;
	smb_disp_stats_t	*sds;
	smb_session_t		*session;
	uint32_t		msg_len;
	uint16_t		cmd_idx;
	int			rc = 0;
	boolean_t		disconnect = B_FALSE;
	boolean_t		related;

	session = sr->session;

	ASSERT(sr->smb2_async_mode == B_FALSE);
	ASSERT(sr->tid_tree == 0);
	ASSERT(sr->uid_user == 0);
	ASSERT(sr->fid_ofile == 0);
	sr->smb_fid = (uint16_t)-1;
	sr->smb2_status = 0;

	/* temporary until we identify a user */
	sr->user_cr = zone_kcred();

cmd_start:
	/*
	 * Note that we don't check sr_state here and abort the
	 * compound if cancelled (etc.) because some SMB2 command
	 * handlers need to do work even when cancelled.
	 *
	 * We treat some status codes as if "sticky", meaning
	 * once they're set after some command handler returns,
	 * all remaining commands get this status without even
	 * calling the command-specific handler.
	 */
	if (sr->smb2_status != NT_STATUS_CANCELLED &&
	    sr->smb2_status != NT_STATUS_INSUFFICIENT_RESOURCES)
		sr->smb2_status = 0;

	/*
	 * Decode the request header
	 *
	 * Most problems with decoding will result in the error
	 * STATUS_INVALID_PARAMETER.  If the decoding problem
	 * prevents continuing, we'll close the connection.
	 * [MS-SMB2] 3.3.5.2.6 Handling Incorrectly Formatted...
	 */
	sr->smb2_async_mode = B_FALSE;
	sr->smb2_cmd_hdr = sr->command.chain_offset;
	if ((rc = smb2_decode_header(sr)) != 0) {
		cmn_err(CE_WARN, "clnt %s bad SMB2 header",
		    session->ip_addr_str);
		disconnect = B_TRUE;
		goto cleanup;
	}

	/*
	 * The SMB2_FLAGS_SERVER_TO_REDIR should only appear
	 * in messages from the server back to the client.
	 */
	if ((sr->smb2_hdr_flags & SMB2_FLAGS_SERVER_TO_REDIR) != 0) {
		cmn_err(CE_WARN, "clnt %s bad SMB2 flags",
		    session->ip_addr_str);
		disconnect = B_TRUE;
		goto cleanup;
	}
	related = (sr->smb2_hdr_flags & SMB2_FLAGS_RELATED_OPERATIONS);
	sr->smb2_hdr_flags |= SMB2_FLAGS_SERVER_TO_REDIR;
	if (sr->smb2_hdr_flags & SMB2_FLAGS_ASYNC_COMMAND) {
		/* Probably an async cancel. */
		DTRACE_PROBE1(smb2__dispatch__async, smb_request_t *, sr);
	}

	/*
	 * In case we bail out with an error before we get to the
	 * section that computes the credit grant, initialize the
	 * response header fields so that credits won't change.
	 * Note: SMB 2.02 clients may send credit charge zero.
	 */
	if (sr->smb2_credit_charge == 0)
		sr->smb2_credit_charge = 1;
	sr->smb2_credit_response = sr->smb2_credit_charge;

	/*
	 * Write a tentative reply header.
	 *
	 * We could just leave this blank, but if we're using the
	 * mdb module feature that extracts packets, it's useful
	 * to have the header mostly correct here.
	 *
	 * If we have already exhausted the output space, then the
	 * client is trying something funny.  Log it and kill 'em.
	 */
	sr->smb2_next_reply = 0;
	ASSERT((sr->reply.chain_offset & 7) == 0);
	sr->smb2_reply_hdr = sr->reply.chain_offset;
	if ((rc = smb2_encode_header(sr, B_FALSE)) != 0) {
		cmn_err(CE_WARN, "clnt %s excessive reply",
		    session->ip_addr_str);
		disconnect = B_TRUE;
		goto cleanup;
	}

	/*
	 * Figure out the length of data following the SMB2 header.
	 * It ends at either the next SMB2 header if there is one
	 * (smb2_next_command != 0) or at the end of the message.
	 */
	if (sr->smb2_next_command != 0) {
		/* [MS-SMB2] says this is 8-byte aligned */
		msg_len = sr->smb2_next_command;
		if ((msg_len & 7) != 0 || (msg_len < SMB2_HDR_SIZE) ||
		    ((sr->smb2_cmd_hdr + msg_len) > sr->command.max_bytes)) {
			cmn_err(CE_WARN, "clnt %s bad SMB2 next cmd",
			    session->ip_addr_str);
			disconnect = B_TRUE;
			goto cleanup;
		}
	} else {
		msg_len = sr->command.max_bytes - sr->smb2_cmd_hdr;
	}

	/*
	 * Setup a shadow chain for this SMB2 command, starting
	 * with the header and ending at either the next command
	 * or the end of the message.  The signing check below
	 * needs the entire SMB2 command.  After that's done, we
	 * advance chain_offset to the end of the header where
	 * the command specific handlers continue decoding.
	 */
	(void) MBC_SHADOW_CHAIN(&sr->smb_data, &sr->command,
	    sr->smb2_cmd_hdr, msg_len);

	/*
	 * We will consume the data for this request from smb_data.
	 * That effectively consumes msg_len bytes from sr->command
	 * but doesn't update its chain_offset, so we need to update
	 * that here to make later received bytes accounting work.
	 */
	sr->command.chain_offset = sr->smb2_cmd_hdr + msg_len;
	ASSERT(sr->command.chain_offset <= sr->command.max_bytes);

	/*
	 * Validate the commmand code, get dispatch table entries.
	 * [MS-SMB2] 3.3.5.2.6 Handling Incorrectly Formatted...
	 *
	 * The last slot in the dispatch table is used to handle
	 * invalid commands.  Same for statistics.
	 */
	if (sr->smb2_cmd_code < SMB2_INVALID_CMD)
		cmd_idx = sr->smb2_cmd_code;
	else
		cmd_idx = SMB2_INVALID_CMD;
	sdd = &smb2_disp_table[cmd_idx];
	sds = &session->s_server->sv_disp_stats2[cmd_idx];

	/*
	 * If this command is NOT "related" to the previous,
	 * clear out the UID, TID, FID state that might be
	 * left over from the previous command.
	 *
	 * If the command IS related, any new IDs are ignored,
	 * and we simply continue with the previous user, tree,
	 * and open file.
	 */
	if (!related) {
		/*
		 * Drop user, tree, file; carefully ordered to
		 * avoid dangling references: file, tree, user
		 */
		if (sr->fid_ofile != NULL) {
			smb_ofile_release(sr->fid_ofile);
			sr->fid_ofile = NULL;
		}
		if (sr->tid_tree != NULL) {
			smb_tree_release(sr->tid_tree);
			sr->tid_tree = NULL;
		}
		if (sr->uid_user != NULL) {
			smb_user_release(sr->uid_user);
			sr->uid_user = NULL;
			sr->user_cr = zone_kcred();
		}
	}

	/*
	 * Make sure we have a user and tree as needed
	 * according to the flags for the this command.
	 * Note that we may have inherited these.
	 */
	if ((sdd->sdt_flags & SDDF_SUPPRESS_UID) == 0) {
		/*
		 * This command requires a user session.
		 */
		if (related) {
			/*
			 * Previous command should have given us a user.
			 * [MS-SMB2] 3.3.5.2 Handling Related Requests
			 */
			if (sr->uid_user == NULL) {
				smb2sr_put_error(sr,
				    NT_STATUS_INVALID_PARAMETER);
				goto cmd_done;
			}
			sr->smb2_ssnid = sr->uid_user->u_ssnid;
		} else {
			/*
			 * Lookup the UID
			 * [MS-SMB2] 3.3.5.2 Verifying the Session
			 */
			ASSERT(sr->uid_user == NULL);
			/*
			 * [MS-SMB2] 3.3.5.2.7 Handling Compounded Requests
			 *
			 * If this is an encrypted compound request,
			 * ensure that the ssnid in the request
			 * is the same as the tform ssnid if this
			 * message is not related.
			 *
			 * The reasons this is done seem to apply equally
			 * to uncompounded requests, so we apply it to all.
			 */

			if (sr->encrypted &&
			    sr->smb2_ssnid != sr->th_ssnid) {
				disconnect = B_TRUE;
				goto cleanup; /* just do this for now */
			}

			sr->uid_user = smb_session_lookup_ssnid(session,
			    sr->smb2_ssnid);
			if (sr->uid_user == NULL) {
				smb2sr_put_error(sr,
				    NT_STATUS_USER_SESSION_DELETED);
				goto cmd_done;
			}

			/*
			 * [MS-SMB2] 3.3.5.2.9 Verifying the Session
			 *
			 * If we're talking 3.x,
			 * RejectUnencryptedAccess is TRUE,
			 * Session.EncryptData is TRUE,
			 * and the message wasn't encrypted,
			 * return ACCESS_DENIED.
			 *
			 * Note that Session.EncryptData can only be TRUE when
			 * we're talking 3.x.
			 */
			if (sr->uid_user->u_encrypt == SMB_CONFIG_REQUIRED &&
			    !sr->encrypted) {
				smb2sr_put_error(sr,
				    NT_STATUS_ACCESS_DENIED);
				goto cmd_done;
			}

			sr->user_cr = smb_user_getcred(sr->uid_user);
		}
		ASSERT(sr->uid_user != NULL);

		/*
		 * Encrypt if:
		 * - The cmd is not SESSION_SETUP or NEGOTIATE; AND
		 * - Session.EncryptData is TRUE
		 *
		 * Those commands suppress UID, so they can't be the cmd here.
		 */
		if (sr->uid_user->u_encrypt != SMB_CONFIG_DISABLED &&
		    sr->th_sid_user == NULL) {
			smb_user_hold_internal(sr->uid_user);
			sr->th_sid_user = sr->uid_user;
			sr->th_ssnid = sr->smb2_ssnid;
		}
	}

	if ((sdd->sdt_flags & SDDF_SUPPRESS_TID) == 0) {
		/*
		 * This command requires a tree connection.
		 */
		if (related) {
			/*
			 * Previous command should have given us a tree.
			 * [MS-SMB2] 3.3.5.2 Handling Related Requests
			 */
			if (sr->tid_tree == NULL) {
				smb2sr_put_error(sr,
				    NT_STATUS_INVALID_PARAMETER);
				goto cmd_done;
			}
			sr->smb_tid = sr->tid_tree->t_tid;
		} else {
			/*
			 * Lookup the TID
			 * [MS-SMB2] 3.3.5.2 Verifying the Tree Connect
			 */
			ASSERT(sr->tid_tree == NULL);
			sr->tid_tree = smb_session_lookup_tree(session,
			    sr->smb_tid);
			if (sr->tid_tree == NULL) {
				smb2sr_put_error(sr,
				    NT_STATUS_NETWORK_NAME_DELETED);
				goto cmd_done;
			}

			/*
			 * [MS-SMB2] 3.3.5.2.11 Verifying the Tree Connect
			 *
			 * If we support 3.x, RejectUnencryptedAccess is TRUE,
			 * if Tcon.EncryptData is TRUE or
			 * global EncryptData is TRUE and
			 * the message wasn't encrypted, or
			 * if Tcon.EncryptData is TRUE or
			 * global EncryptData is TRUE or
			 * the request was encrypted and
			 * the connection doesn't support encryption,
			 * return ACCESS_DENIED.
			 *
			 * If RejectUnencryptedAccess is TRUE, we force
			 * max_protocol to at least 3.0. Additionally,
			 * if the tree requires encryption, we don't care
			 * what we support, we still enforce encryption.
			 * Since smb3_decrypt_msg() does check session->srv_cap,
			 * we only need to check sr->encrypted here.
			 */
			if (sr->tid_tree->t_encrypt == SMB_CONFIG_REQUIRED &&
			    !sr->encrypted) {
				smb2sr_put_error(sr,
				    NT_STATUS_ACCESS_DENIED);
				goto cmd_done;
			}
		}
		ASSERT(sr->tid_tree != NULL);

		/*
		 * Encrypt if:
		 * - The cmd is not TREE_CONNECT; AND
		 * - Tree.EncryptData is TRUE
		 *
		 * TREE_CONNECT suppresses TID, so that can't be the cmd here.
		 * NOTE: assumes we can't have a tree without a user
		 */
		if (sr->tid_tree->t_encrypt != SMB_CONFIG_DISABLED &&
		    sr->th_sid_user == NULL) {
			smb_user_hold_internal(sr->uid_user);
			sr->th_sid_user = sr->uid_user;
			sr->th_ssnid = sr->smb2_ssnid;
		}
	}

	/*
	 * SMB2 signature verification, two parts:
	 * (a) Require SMB2_FLAGS_SIGNED (for most request types)
	 * (b) If SMB2_FLAGS_SIGNED is set, check the signature.
	 * [MS-SMB2] 3.3.5.2.4 Verifying the Signature
	 */

	/*
	 * No user session means no signature check.  That's OK,
	 * i.e. for commands marked SDDF_SUPPRESS_UID above.
	 * Note, this also means we won't sign the reply.
	 */
	if (sr->uid_user == NULL)
		sr->smb2_hdr_flags &= ~SMB2_FLAGS_SIGNED;

	/*
	 * The SDDF_SUPPRESS_UID dispatch is set for requests that
	 * don't need a UID (user).  These also don't require a
	 * signature check here.
	 *
	 * [MS-SMB2] 3.3.5.2.4 Verifying the Signature
	 *
	 * If the packet was successfully decrypted, the message
	 * signature has already been verified, so we can skip this.
	 */
	if ((sdd->sdt_flags & SDDF_SUPPRESS_UID) == 0 &&
	    !sr->encrypted && sr->uid_user != NULL &&
	    (sr->uid_user->u_sign_flags & SMB_SIGNING_ENABLED) != 0) {
		/*
		 * If the request is signed, check the signature.
		 * Otherwise, if signing is required, deny access.
		 */
		if ((sr->smb2_hdr_flags & SMB2_FLAGS_SIGNED) != 0) {
			if (smb2_sign_check_request(sr) != 0) {
				smb2sr_put_error(sr, NT_STATUS_ACCESS_DENIED);
				DTRACE_PROBE1(smb2__sign__check,
				    smb_request_t *, sr);
				goto cmd_done;
			}
		} else if (
		    (sr->uid_user->u_sign_flags & SMB_SIGNING_CHECK) != 0) {
			smb2sr_put_error(sr, NT_STATUS_ACCESS_DENIED);
			goto cmd_done;
		}
	}

	/*
	 * Now that the signing check is done with smb_data,
	 * advance past the SMB2 header we decoded earlier.
	 * This leaves sr->smb_data correctly positioned
	 * for command-specific decoding in the dispatch
	 * function called next.
	 */
	sr->smb_data.chain_offset = sr->smb2_cmd_hdr + SMB2_HDR_SIZE;

	/*
	 * Credit adjustments (decrease)
	 */
	if (sr->smb2_credit_request < sr->smb2_credit_charge)
		smb2_credit_decrease(sr);

	/*
	 * The real work: call the SMB2 command handler
	 * (except for "sticky" smb2_status - see above)
	 */
	sr->sr_time_start = gethrtime();
	rc = SDRC_SUCCESS;
	if (sr->smb2_status == 0) {
		/* NB: not using pre_op */
		rc = (*sdd->sdt_function)(sr);
		/* NB: not using post_op */
	} else {
		smb2sr_put_error(sr, sr->smb2_status);
	}

	/*
	 * When the sdt_function returns SDRC_SR_KEPT, it means
	 * this SR may have been passed to another thread so we
	 * MUST NOT touch it anymore.
	 */
	if (rc == SDRC_SR_KEPT)
		return;

	MBC_FLUSH(&sr->raw_data);

	/*
	 * Credit adjustments (increase)
	 *
	 * If we've gone async, credit adjustments were done
	 * when we sent the interim reply.
	 */
	if (!sr->smb2_async_mode) {
		if (sr->smb2_credit_request > sr->smb2_credit_charge) {
			smb2_credit_increase(sr);
		}
	}

cmd_done:
	switch (rc) {
	case SDRC_SUCCESS:
		break;
	default:
		/*
		 * SMB2 does not use the other dispatch return codes.
		 * If we see something else, log an event so we'll
		 * know something is returning bogus status codes.
		 * If you see these in the log, use dtrace to find
		 * the code returning something else.
		 */
#ifdef	DEBUG
		cmn_err(CE_NOTE, "handler for %u returned 0x%x",
		    sr->smb2_cmd_code, rc);
#endif
		smb2sr_put_error(sr, NT_STATUS_INTERNAL_ERROR);
		break;
	case SDRC_ERROR:
		/*
		 * Many command handlers return SDRC_ERROR for any
		 * problems decoding the request, and don't bother
		 * setting smb2_status.  For those cases, the best
		 * status return would be "invalid parameter".
		 */
		if (sr->smb2_status == 0)
			sr->smb2_status = NT_STATUS_INVALID_PARAMETER;
		smb2sr_put_error(sr, sr->smb2_status);
		break;
	case SDRC_DROP_VC:
		disconnect = B_TRUE;
		goto cleanup;

	case SDRC_NO_REPLY:
		/* will free sr */
		goto cleanup;
	}

	/*
	 * Pad the reply to align(8) if there will be another.
	 */
	if (sr->smb2_next_command != 0)
		(void) smb_mbc_put_align(&sr->reply, 8);

	/*
	 * Record some statistics.  Uses:
	 *   rxb = command.chain_offset - smb2_cmd_hdr;
	 *   txb = reply.chain_offset - smb2_reply_hdr;
	 * which at this point represent the current cmd/reply.
	 */
	smb2_record_stats(sr, sds, B_TRUE);

	/*
	 * If there's a next command, figure out where it starts,
	 * and fill in the next header offset for the reply.
	 * Note: We sanity checked smb2_next_command above.
	 */
	if (sr->smb2_next_command != 0) {
		sr->command.chain_offset =
		    sr->smb2_cmd_hdr + sr->smb2_next_command;
		sr->smb2_next_reply =
		    sr->reply.chain_offset - sr->smb2_reply_hdr;
	} else {
		ASSERT(sr->smb2_next_reply == 0);
	}

	/*
	 * Overwrite the (now final) SMB2 header for this response.
	 */
	(void) smb2_encode_header(sr, B_TRUE);

	/*
	 * Cannot move this into smb2_session_setup() - encoded header required.
	 */
	if (session->dialect >= SMB_VERS_3_11 &&
	    sr->smb2_cmd_code == SMB2_SESSION_SETUP &&
	    sr->smb2_status == NT_STATUS_MORE_PROCESSING_REQUIRED) {
		if (smb31_preauth_sha512_calc(sr, &sr->reply,
		    sr->uid_user->u_preauth_hashval,
		    sr->uid_user->u_preauth_hashval) != 0)
			cmn_err(CE_WARN, "(3) Preauth hash calculation "
			    "failed");
	}

	/* Don't sign if we're going to encrypt */
	if (sr->th_sid_user == NULL &&
	    (sr->smb2_hdr_flags & SMB2_FLAGS_SIGNED) != 0)
		smb2_sign_reply(sr);

	if (sr->smb2_next_command != 0)
		goto cmd_start;

	/*
	 * If we have a durable handle, and this operation updated
	 * the nvlist, write it out (before smb2_send_reply).
	 */
	if (sr->dh_nvl_dirty) {
		sr->dh_nvl_dirty = B_FALSE;
		smb2_dh_update_nvfile(sr);
	}

	smb2_send_reply(sr);

cleanup:
	if (disconnect)
		smb_session_disconnect(session);

	/*
	 * Do "postwork" for oplock (and maybe other things)
	 */
	if (sr->sr_postwork != NULL)
		smb2sr_run_postwork(sr);

	mutex_enter(&sr->sr_mutex);
	sr->sr_state = SMB_REQ_STATE_COMPLETED;
	mutex_exit(&sr->sr_mutex);

	smb_request_free(sr);
}

/*
 * Build an interim response for a command that needs to 'go async'.
 * This is used by requests that block for a relatively long time.
 * Leave the SR state so that smb2sr_work() can continue its
 * processing of this compound in "async mode".
 *
 * If we agree to "go async", this should return STATUS_SUCCESS.
 * Otherwise return STATUS_INSUFFICIENT_RESOURCES for this and
 * all requests following this request.  (See the comments re.
 * "sticky" smb2_status values in smb2sr_work).
 *
 * Note: the Async ID we assign here is arbitrary, and need only
 * be unique among pending async responses on this connection, so
 * this just uses a modified messageID, which is already unique.
 *
 * Credits:  All credit changes should happen via the interim
 * response, so we have to manage credits here.  After this
 * returns to smb2sr_work, the final reply for this command
 * will have smb2_credit_response = 0
 * (meaning no further changes to the clients' credits).
 *
 * Compound responses:
 *
 * [MS-SMB2] 3.3.5.2.7.2 "Handling Compounded Related Requests" states:
 *
 * "When an operation requires asynchronous processing, all the subsequent
 * operations MUST also be processed asynchronously. The server MUST send an
 * interim response for all such operations as specified in section 3.3.4.2."
 *
 * One might expect that means we should also send interim responses for every
 * command following the current one. However, Windows does not do that, and at
 * least one client (mount.cifs) rejects such 'extra' responses as errors.
 * When asked for clarification, Microsoft Dochelp pointed to this part of
 * [MS-SMB2] 3.3.4.2 "Sending an Interim Response for an Asynchronous Operation"
 *
 * "The server MAY choose to send an interim response for any request..."
 *
 * Further experimentation on Windows reveals two separate behaviors, seemingly
 * distinguished by whether the request may block indefinitely:
 *
 * Bounded blocking (e.g. Oplock break):
 * - An interim response is sent before any part of the compound response.
 * - If multiple commands 'go async', they each send separate interim responses.
 * - A single compound response is sent when the final command completes.
 *
 * Indefinite blocking (e.g. Change Notify, Byte-Range Lock):
 * - If the command is not the last in the compound, it is rejected with
 * "NT_STATUS_INTERNAL_ERROR".
 * - All responses for commands prior to the one that 'goes async' are returned
 * immediately as a single compound response.
 * - An interim response is then sent separately from the compound response.
 *
 * We follow Windows behavior here for compatibility purposes, with one
 * exception: in the indefinite case, we deliver the interim response at the end
 * of the compound response, rather than separately, as we don't have the state
 * needed to modify the previous response's header to terminate the compound.
 * The command-specific handler chooses whether to allow this in the middle of
 * the compound.
 *
 * Errors here mean we failed to encode the interim response.
 * Rather than actually disconnect here, let's assume whatever problem we
 * encountered will be seen by the caller as they continue processing the
 * compound, and just restore everything and return an error.
 */
static uint32_t
smb2sr_send_interim(smb_request_t *sr)
{
	uint32_t saved_hdr_flags;
	boolean_t disconnect = B_FALSE;

	sr->smb2_async_id = SMB2_ASYNCID(sr);
	sr->smb2_status = NT_STATUS_PENDING;
	sr->smb2_hdr_flags |= SMB2_FLAGS_ASYNC_COMMAND;
	saved_hdr_flags = sr->smb2_hdr_flags;
	sr->smb2_next_reply = 0;

	if ((smb2_encode_header(sr, B_FALSE)) != 0) {
		cmn_err(CE_WARN, "clnt %s excessive reply",
		    sr->session->ip_addr_str);
		disconnect = B_TRUE;
		goto cleanup;
	}

	/*
	 * Don't change (user, tree, file) because we want them
	 * exactly as they were when we entered.
	 * Interim responses are NOT signed; we already checked the signature
	 * for this message, and will restore this flag on completion.
	 */
	sr->smb2_hdr_flags &= ~SMB2_FLAGS_SIGNED;

	smb2sr_put_error(sr, sr->smb2_status);

	/*
	 * Credit adjustments (increase)
	 *
	 * NOTE: interim responses are not signed.
	 * Any attacker can modify the credit grant
	 * in the response. Because of this property,
	 * it is no worse to assume the credit charge and grant
	 * are sane without verifying the signature,
	 * and that saves us a whole lot of work.
	 * If the credits WERE modified, we'll find out
	 * when we verify the signature later,
	 * which nullifies any changes caused here.
	 */
	if (sr->smb2_credit_request > sr->smb2_credit_charge) {
		smb2_credit_increase(sr);
	}

	DTRACE_PROBE1(go__async, smb_request_t *, sr);

	/*
	 * Overwrite the (now final) SMB2 header for this response.
	 */
	(void) smb2_encode_header(sr, B_TRUE);
	sr->smb2_async_txb =
	    (int64_t)(sr->reply.chain_offset - sr->smb2_reply_hdr);

	smb2_send_reply(sr);

	/*
	 * Now that we've responded (with credits granted),
	 * the final response shouldn't grant any.
	 */
	sr->smb2_credit_response = 0;

cleanup:
	sr->smb2_status = NT_STATUS_SUCCESS;
	sr->smb2_hdr_flags = saved_hdr_flags;

	if (disconnect) {
		sr->smb2_async_mode = B_FALSE;
		sr->smb2_hdr_flags &= SMB2_FLAGS_ASYNC_COMMAND;
		return (NT_STATUS_INVALID_PARAMETER);
	}

	return (NT_STATUS_SUCCESS);
}

/*
 * This variant of 'go async' sends the interim response on its own,
 * saving/restoring the response compound currently under construction.
 * See the longer comment above smb2sr_send_interim().
 */
uint32_t
smb2sr_go_async(smb_request_t *sr)
{
	struct mbuf_chain saved_reply;
	uint32_t saved_reply_hdr = sr->smb2_reply_hdr;
	uint32_t saved_next_reply = sr->smb2_next_reply;
	uint32_t status;

	if (sr->smb2_async_mode) {
		/* cmd is already being processed async */
		return (NT_STATUS_SUCCESS);
	}
	sr->smb2_async_mode = B_TRUE;

	/* The "server" session always runs async. */
	if (sr->session->sock == NULL)
		return (NT_STATUS_SUCCESS);

	/*
	 * All the encoding functions use sr and sr->reply.
	 * Keep a copy of the old reply MBC around so we can send out our
	 * interim response on its own, then restore the original.
	 */
	saved_reply = sr->reply; /* struct copy */
	MBC_INIT(&sr->reply, sr->reply.max_bytes);
	sr->smb2_reply_hdr = 0;

	status = smb2sr_send_interim(sr);

	MBC_FLUSH(&sr->reply); /* clear our interim reply */
	sr->reply = saved_reply; /* struct copy - restore original */
	sr->smb2_next_reply = saved_next_reply;
	sr->smb2_reply_hdr = saved_reply_hdr;

	return (status);
}

/*
 * This variant of 'go async' appends the interim response
 * to the response compound currently under construction.
 * See the longer comment above smb2sr_send_interim().
 */
uint32_t
smb2sr_go_async_indefinite(smb_request_t *sr)
{
	uint32_t saved_reply_hdr = sr->smb2_reply_hdr;
	uint32_t status;

	if (sr->smb2_async_mode) {
		/* cmd is already being processed async */
		return (NT_STATUS_SUCCESS);
	}
	sr->smb2_async_mode = B_TRUE;

	/* The "server" session always runs async. */
	if (sr->session->sock == NULL)
		return (NT_STATUS_SUCCESS);

	/* Rewind reply to start of header */
	sr->reply.chain_offset = saved_reply_hdr;

	status = smb2sr_send_interim(sr);
	if (status != NT_STATUS_SUCCESS)
		return (status);

	/* We've now consumed the reply; re-initialize it. */
	MBC_FLUSH(&sr->reply);
	ASSERT(sr->reply.max_bytes == sr->session->reply_max_bytes);
	ASSERT(sr->reply.chain_offset == 0);
	sr->smb2_reply_hdr = 0;
	(void) smb2_encode_header(sr, B_FALSE);

	return (NT_STATUS_SUCCESS);
}

int
smb2_decode_header(smb_request_t *sr)
{
	uint32_t pid, tid;
	uint16_t hdr_len;
	int rc;

	rc = smb_mbc_decodef(
	    &sr->command, "Nwww..wwllqllq16c",
	    &hdr_len,			/* w */
	    &sr->smb2_credit_charge,	/* w */
	    &sr->smb2_chan_seq,		/* w */
	    /* reserved			  .. */
	    &sr->smb2_cmd_code,		/* w */
	    &sr->smb2_credit_request,	/* w */
	    &sr->smb2_hdr_flags,	/* l */
	    &sr->smb2_next_command,	/* l */
	    &sr->smb2_messageid,	/* q */
	    &pid,			/* l */
	    &tid,			/* l */
	    &sr->smb2_ssnid,		/* q */
	    sr->smb2_sig);		/* 16c */
	if (rc)
		return (rc);

	if (hdr_len != SMB2_HDR_SIZE)
		return (-1);

	if (sr->smb2_hdr_flags & SMB2_FLAGS_ASYNC_COMMAND) {
		sr->smb2_async_id = pid |
		    ((uint64_t)tid) << 32;
		sr->smb_pid = 0;
		sr->smb_tid = 0;
	} else {
		sr->smb2_async_id = 0;
		sr->smb_pid = pid;
		sr->smb_tid = (uint16_t)tid; /* XXX wide TIDs */
	}

	return (rc);
}

int
smb2_encode_header(smb_request_t *sr, boolean_t overwrite)
{
	uint64_t pid_tid_aid; /* pid+tid, or async id */
	int rc;

	if (sr->smb2_hdr_flags & SMB2_FLAGS_ASYNC_COMMAND) {
		pid_tid_aid = sr->smb2_async_id;
	} else {
		pid_tid_aid = sr->smb_pid |
		    ((uint64_t)sr->smb_tid) << 32;
	}

	if (overwrite) {
		rc = smb_mbc_poke(&sr->reply,
		    sr->smb2_reply_hdr,
		    "Nwwlwwllqqq16c",
		    SMB2_HDR_SIZE,		/* w */
		    sr->smb2_credit_charge,	/* w */
		    sr->smb2_status,		/* l */
		    sr->smb2_cmd_code,		/* w */
		    sr->smb2_credit_response,	/* w */
		    sr->smb2_hdr_flags,		/* l */
		    sr->smb2_next_reply,	/* l */
		    sr->smb2_messageid,		/* q */
		    pid_tid_aid,		/* q */
		    sr->smb2_ssnid,		/* q */
		    sr->smb2_sig);		/* 16c */
	} else {
		rc = smb_mbc_encodef(&sr->reply,
		    "Nwwlwwllqqq16c",
		    SMB2_HDR_SIZE,		/* w */
		    sr->smb2_credit_charge,	/* w */
		    sr->smb2_status,		/* l */
		    sr->smb2_cmd_code,		/* w */
		    sr->smb2_credit_response,	/* w */
		    sr->smb2_hdr_flags,		/* l */
		    sr->smb2_next_reply,	/* l */
		    sr->smb2_messageid,		/* q */
		    pid_tid_aid,		/* q */
		    sr->smb2_ssnid,		/* q */
		    sr->smb2_sig);		/* 16c */
	}

	return (rc);
}

void
smb2_send_reply(smb_request_t *sr)
{
	struct mbuf_chain enc_reply;
	smb_session_t *session = sr->session;
	mbuf_t *m;

	/*
	 * [MS-SMB2] 3.3.4.1.4 Encrypting the Message
	 *
	 * When the connection supports encryption and the dialect
	 * is 3.x, encrypt if:
	 * - The request was encrypted OR
	 * - The cmd is not SESSION_SETUP or NEGOTIATE AND
	 * -- Session.EncryptData is TRUE OR
	 * -- The cmd is not TREE_CONNECT AND
	 * --- Tree.EncryptData is TRUE
	 *
	 * This boils down to sr->th_sid_user != NULL, and the rest
	 * is enforced when th_sid_user is set.
	 */

	if ((session->capabilities & SMB2_CAP_ENCRYPTION) == 0 ||
	    sr->th_sid_user == NULL) {
		(void) smb_session_send(sr->session, 0, &sr->reply);
		return;
	}

	/*
	 * Encrypted send
	 *
	 * Not doing in-place encryption because we may have
	 * loaned buffers (eg. from ZFS) that are read-only.
	 *
	 * Setup the transform header in its own mblk,
	 * with leading space for the netbios header.
	 */
	MBC_INIT(&enc_reply, SMB3_TFORM_HDR_SIZE);
	m = enc_reply.chain;
	m->m_len = SMB3_TFORM_HDR_SIZE;

	sr->th_msglen = sr->reply.chain_offset;
	m->m_next = smb_mbuf_alloc_chain(sr->th_msglen);
	enc_reply.max_bytes += sr->th_msglen;

	if (smb3_encrypt_sr(sr, &sr->reply, &enc_reply) != 0) {
		cmn_err(CE_WARN, "smb3 encryption failed");
		smb_session_disconnect(sr->session);
	} else {
		(void) smb_session_send(sr->session, 0, &enc_reply);
	}
	MBC_FLUSH(&enc_reply);
}

/*
 * This wrapper function exists to help catch calls to smbsr_status()
 * (which is SMB1-specific) in common code.  See smbsr_status().
 * If the log message below is seen, put a dtrace probe on this
 * function with a stack() action to see who is calling the SMB1
 * "put error" from common code, and fix it.
 */
void
smbsr_status_smb2(smb_request_t *sr, DWORD status)
{
	const char *name;

	if (sr->smb2_cmd_code < SMB2__NCMDS)
		name = smb2_disp_table[sr->smb2_cmd_code].sdt_name;
	else
		name = "<unknown>";
#ifdef	DEBUG
	cmn_err(CE_NOTE, "smbsr_status called for %s", name);
#endif

	smb2sr_put_error_data(sr, status, NULL);
}

void
smb2sr_put_errno(struct smb_request *sr, int errnum)
{
	uint32_t status = smb_errno2status(errnum);
	smb2sr_put_error_data(sr, status, NULL);
}

void
smb2sr_put_error(smb_request_t *sr, uint32_t status)
{
	smb2sr_put_error_data(sr, status, NULL);
}

/*
 * Build an SMB2 error response.  [MS-SMB2] 2.2.2
 */
void
smb2sr_put_error_data(smb_request_t *sr, uint32_t status, mbuf_chain_t *mbc)
{
	DWORD len;

	/*
	 * The common dispatch code writes this when it
	 * updates the SMB2 header before sending.
	 */
	sr->smb2_status = status;

	/* Rewind to the end of the SMB header. */
	sr->reply.chain_offset = sr->smb2_reply_hdr + SMB2_HDR_SIZE;

	/*
	 * NB: Must provide at least one byte of error data,
	 * per [MS-SMB2] 2.2.2
	 */
	if (mbc != NULL && (len = MBC_LENGTH(mbc)) != 0) {
		(void) smb_mbc_encodef(
		    &sr->reply,
		    "wwlC",
		    9,	/* StructSize */	/* w */
		    0,	/* reserved */		/* w */
		    len,			/* l */
		    mbc);			/* C */
	} else {
		(void) smb_mbc_encodef(
		    &sr->reply,
		    "wwl.",
		    9,	/* StructSize */	/* w */
		    0,	/* reserved */		/* w */
		    0);				/* l. */
	}
}

/*
 * Build an SMB2 error context response (dialect 3.1.1).
 */
void
smb2sr_put_error_ctx(smb_request_t *sr, uint32_t status, uint32_t errid,
    mbuf_chain_t *mbc)
{
	DWORD len;

	/*
	 * The common dispatch code writes this when it
	 * updates the SMB2 header before sending.
	 */
	sr->smb2_status = status;

	/* Rewind to the end of the SMB header. */
	sr->reply.chain_offset = sr->smb2_reply_hdr + SMB2_HDR_SIZE;

	/*
	 *  Error Context is 8-byte header plus encaps. data (ErrorContextData),
	 *  which can be zero-length.
	 */
	if (mbc != NULL && (len = MBC_LENGTH(mbc)) != 0) {
		(void) smb_mbc_encodef(
		    &sr->reply,
		    "wbblllC",
		    9,		/* StructSize */	/* w */
		    1,		/* ErrorContextCount */	/* b */
		    0,		/* reserved */		/* b */
		    8+len,	/* ByteCount */		/* l */
		    len,	/* ErrorDataLength */	/* l */
		    errid,	/* ErrorId */		/* l */
		    mbc);				/* C */
	} else {
		(void) smb_mbc_encodef(
		    &sr->reply,
		    "wbblll",
		    9,		/* StructSize */	/* w */
		    1,		/* ErrorContextCount */	/* b */
		    0,		/* reserved */		/* b */
		    8,		/* ByteCount */		/* l */
		    0,		/* ErrorDataLength */	/* l */
		    errid);	/* ErrorId */		/* l */
	}
}

/*
 * Build an SMB2 error context response with SMB2_ERROR_ID_DEFAULT ErrorId.
 *
 * This only handles the case we currently need, encapsulating a
 * single error data section inside an SMB2_ERROR_ID_DEFAULT
 * error context type (which is type zero, and that's what
 * the zero on the end of this function name refers to).
 */
void
smb2sr_put_error_ctx0(smb_request_t *sr, uint32_t status, mbuf_chain_t *mbc)
{
	return (smb2sr_put_error_ctx(sr, status, SMB2_ERROR_ID_DEFAULT, mbc));
}

/*
 * smb2sr_lookup_fid
 *
 * Setup sr->fid_ofile, either inherited from a related command,
 * or obtained via FID lookup.  Similar inheritance logic as in
 * smb2sr_work.
 */
uint32_t
smb2sr_lookup_fid(smb_request_t *sr, smb2fid_t *fid)
{
	boolean_t related = sr->smb2_hdr_flags &
	    SMB2_FLAGS_RELATED_OPERATIONS;

	if (related) {
		if (sr->fid_ofile == NULL)
			return (NT_STATUS_INVALID_PARAMETER);
		sr->smb_fid = sr->fid_ofile->f_fid;
		return (0);
	}

	/*
	 * If we could be sure this is called only once per cmd,
	 * we could simply ASSERT(sr->fid_ofile == NULL) here.
	 * However, there are cases where it can be called again
	 * handling the same command, so let's tolerate that.
	 */
	if (sr->fid_ofile == NULL) {
		sr->smb_fid = (uint16_t)fid->temporal;
		sr->fid_ofile = smb_ofile_lookup_by_fid(sr, sr->smb_fid);
	}
	if (sr->fid_ofile == NULL ||
	    sr->fid_ofile->f_persistid != fid->persistent)
		return (NT_STATUS_FILE_CLOSED);

	return (0);
}

/*
 * smb2_dispatch_stats_init
 *
 * Initializes dispatch statistics for SMB2.
 * See also smb_dispatch_stats_init(), which fills in
 * the lower part of the statistics array, from zero
 * through SMB_COM_NUM;
 */
void
smb2_dispatch_stats_init(smb_server_t *sv)
{
	smb_disp_stats_t *sds = sv->sv_disp_stats2;
	smb_kstat_req_t *ksr;
	int		i;

	ksr = ((smbsrv_kstats_t *)sv->sv_ksp->ks_data)->ks_reqs2;

	for (i = 0; i < SMB2__NCMDS; i++, ksr++) {
		smb_latency_init(&sds[i].sdt_lat);
		(void) strlcpy(ksr->kr_name, smb2_disp_table[i].sdt_name,
		    sizeof (ksr->kr_name));
	}
}

/*
 * smb2_dispatch_stats_fini
 *
 * Frees and destroyes the resources used for statistics.
 */
void
smb2_dispatch_stats_fini(smb_server_t *sv)
{
	smb_disp_stats_t *sds = sv->sv_disp_stats2;
	int	i;

	for (i = 0; i < SMB2__NCMDS; i++)
		smb_latency_destroy(&sds[i].sdt_lat);
}

void
smb2_dispatch_stats_update(smb_server_t *sv,
    smb_kstat_req_t *ksr, int first, int nreq)
{
	smb_disp_stats_t *sds = sv->sv_disp_stats2;
	int	i;
	int	last;

	last = first + nreq - 1;

	if ((first < SMB2__NCMDS) && (last < SMB2__NCMDS))  {
		for (i = first; i <= last; i++, ksr++) {
			ksr->kr_rxb = sds[i].sdt_rxb;
			ksr->kr_txb = sds[i].sdt_txb;
			mutex_enter(&sds[i].sdt_lat.ly_mutex);
			ksr->kr_nreq = sds[i].sdt_lat.ly_a_nreq;
			ksr->kr_sum = sds[i].sdt_lat.ly_a_sum;
			ksr->kr_a_mean = sds[i].sdt_lat.ly_a_mean;
			ksr->kr_a_stddev =
			    sds[i].sdt_lat.ly_a_stddev;
			ksr->kr_d_mean = sds[i].sdt_lat.ly_d_mean;
			ksr->kr_d_stddev =
			    sds[i].sdt_lat.ly_d_stddev;
			sds[i].sdt_lat.ly_d_mean = 0;
			sds[i].sdt_lat.ly_d_nreq = 0;
			sds[i].sdt_lat.ly_d_stddev = 0;
			sds[i].sdt_lat.ly_d_sum = 0;
			mutex_exit(&sds[i].sdt_lat.ly_mutex);
		}
	}
}

/*
 * Append new_sr to the postwork queue.  sr->smb2_cmd_code encodes
 * the action that should be run by this sr.
 *
 * This queue is rarely used (and normally empty) so we're OK
 * using a simple "walk to tail and insert" here.
 */
void
smb2sr_append_postwork(smb_request_t *top_sr, smb_request_t *new_sr)
{
	smb_request_t *last_sr;

	ASSERT(top_sr->session->dialect >= SMB_VERS_2_BASE);

	last_sr = top_sr;
	while (last_sr->sr_postwork != NULL)
		last_sr = last_sr->sr_postwork;

	last_sr->sr_postwork = new_sr;
}

/*
 * Run any "post work" that was appended to the main SR while it
 * was running.  This is called after the request has been sent
 * for the main SR, and used in cases i.e. the oplock code, where
 * we need to send something to the client only _after_ the main
 * sr request has gone out.
 */
static void
smb2sr_run_postwork(smb_request_t *top_sr)
{
	smb_request_t *post_sr;	/* the one we're running */
	smb_request_t *next_sr;

	while ((post_sr = top_sr->sr_postwork) != NULL) {
		next_sr = post_sr->sr_postwork;
		top_sr->sr_postwork = next_sr;
		post_sr->sr_postwork = NULL;

		post_sr->sr_worker = top_sr->sr_worker;
		post_sr->sr_state = SMB_REQ_STATE_ACTIVE;

		switch (post_sr->smb2_cmd_code) {
		case SMB2_OPLOCK_BREAK:
			smb_oplock_send_break(post_sr);
			break;
		default:
			ASSERT(0);
		}

		/*
		 * If we have a durable handle, and this operation
		 * updated the nvlist, write it out.
		 */
		if (post_sr->dh_nvl_dirty) {
			post_sr->dh_nvl_dirty = B_FALSE;
			smb2_dh_update_nvfile(post_sr);
		}

		post_sr->sr_state = SMB_REQ_STATE_COMPLETED;
		smb_request_free(post_sr);
	}
}