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
|
/*
* 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_iod.c,v 1.32 2005/02/12 00:17:09 lindak Exp $
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Portions Copyright (C) 2001 - 2013 Apple Inc. All rights reserved.
* Copyright 2019 Nexenta Systems, Inc. All rights reserved.
*/
#ifdef DEBUG
/* See sys/queue.h */
#define QUEUEDEBUG 1
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/atomic.h>
#include <sys/proc.h>
#include <sys/thread.h>
#include <sys/file.h>
#include <sys/kmem.h>
#include <sys/unistd.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/types.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/stream.h>
#include <sys/strsun.h>
#include <sys/time.h>
#include <sys/class.h>
#include <sys/disp.h>
#include <sys/cmn_err.h>
#include <sys/zone.h>
#include <sys/sdt.h>
#include <netsmb/smb_osdep.h>
#include <netsmb/smb.h>
#include <netsmb/smb2.h>
#include <netsmb/smb_conn.h>
#include <netsmb/smb_rq.h>
#include <netsmb/smb2_rq.h>
#include <netsmb/smb_subr.h>
#include <netsmb/smb_tran.h>
#include <netsmb/smb_trantcp.h>
/*
* SMB messages are up to 64K. Let's leave room for two.
* If we negotiate up to SMB2, increase these. XXX todo
*/
static int smb_tcpsndbuf = 0x20000;
static int smb_tcprcvbuf = 0x20000;
static int smb_connect_timeout = 10; /* seconds */
static int smb1_iod_process(smb_vc_t *, mblk_t *);
static int smb2_iod_process(smb_vc_t *, mblk_t *);
static int smb_iod_send_echo(smb_vc_t *, cred_t *cr);
static int smb_iod_logoff(struct smb_vc *vcp, cred_t *cr);
/*
* This is set/cleared when smbfs loads/unloads
* No locks should be necessary, because smbfs
* can't unload until all the mounts are gone.
*/
static smb_fscb_t *fscb;
void
smb_fscb_set(smb_fscb_t *cb)
{
fscb = cb;
}
static void
smb_iod_share_disconnected(smb_share_t *ssp)
{
smb_share_invalidate(ssp);
/*
* This is the only fscb hook smbfs currently uses.
* Replaces smbfs_dead() from Darwin.
*/
if (fscb && fscb->fscb_disconn) {
fscb->fscb_disconn(ssp);
}
}
/*
* State changes are important and infrequent.
* Make them easily observable via dtrace.
*/
void
smb_iod_newstate(struct smb_vc *vcp, int state)
{
vcp->vc_state = state;
}
/* Lock Held version of the next function. */
static inline void
smb_iod_rqprocessed_LH(
struct smb_rq *rqp,
int error,
int flags)
{
rqp->sr_flags |= flags;
rqp->sr_lerror = error;
rqp->sr_rpgen++;
rqp->sr_state = SMBRQ_NOTIFIED;
cv_broadcast(&rqp->sr_cond);
}
static void
smb_iod_rqprocessed(
struct smb_rq *rqp,
int error,
int flags)
{
SMBRQ_LOCK(rqp);
smb_iod_rqprocessed_LH(rqp, error, flags);
SMBRQ_UNLOCK(rqp);
}
static void
smb_iod_invrq(struct smb_vc *vcp)
{
struct smb_rq *rqp;
/*
* Invalidate all outstanding requests for this connection
* Also wakeup iod_muxwant waiters.
*/
rw_enter(&vcp->iod_rqlock, RW_READER);
TAILQ_FOREACH(rqp, &vcp->iod_rqlist, sr_link) {
smb_iod_rqprocessed(rqp, ENOTCONN, SMBR_RESTART);
}
rw_exit(&vcp->iod_rqlock);
cv_broadcast(&vcp->iod_muxwait);
}
/*
* Called by smb_vc_rele/smb_vc_kill on last ref, and by
* the driver close function if the IOD closes its minor.
* In those cases, the caller should be the IOD thread.
*
* Forcibly kill the connection.
*/
void
smb_iod_disconnect(struct smb_vc *vcp)
{
/*
* Inform everyone of the state change.
*/
SMB_VC_LOCK(vcp);
if (vcp->vc_state != SMBIOD_ST_DEAD) {
smb_iod_newstate(vcp, SMBIOD_ST_DEAD);
cv_broadcast(&vcp->vc_statechg);
}
SMB_VC_UNLOCK(vcp);
SMB_TRAN_DISCONNECT(vcp);
}
/*
* Send one request.
*
* SMB1 only
*
* Called by _addrq (for internal requests)
* and _sendall (via _addrq, _multirq, _waitrq)
* Errors are reported via the smb_rq, using:
* smb_iod_rqprocessed(rqp, ...)
*/
static void
smb1_iod_sendrq(struct smb_rq *rqp)
{
struct smb_vc *vcp = rqp->sr_vc;
mblk_t *m;
int error;
ASSERT(vcp);
ASSERT(RW_WRITE_HELD(&vcp->iod_rqlock));
ASSERT((vcp->vc_flags & SMBV_SMB2) == 0);
/*
* Internal requests are allowed in any state;
* otherwise should be active.
*/
if ((rqp->sr_flags & SMBR_INTERNAL) == 0 &&
vcp->vc_state != SMBIOD_ST_VCACTIVE) {
SMBIODEBUG("bad vc_state=%d\n", vcp->vc_state);
smb_iod_rqprocessed(rqp, ENOTCONN, SMBR_RESTART);
return;
}
/*
* Overwrite the SMB header with the assigned MID and
* (if we're signing) sign it.
*/
smb_rq_fillhdr(rqp);
if (rqp->sr_rqflags2 & SMB_FLAGS2_SECURITY_SIGNATURE) {
smb_rq_sign(rqp);
}
/*
* The transport send consumes the message and we'd
* prefer to keep a copy, so dupmsg() before sending.
*/
m = dupmsg(rqp->sr_rq.mb_top);
if (m == NULL) {
error = ENOBUFS;
goto fatal;
}
#ifdef DTRACE_PROBE2
DTRACE_PROBE2(iod_sendrq,
(smb_rq_t *), rqp, (mblk_t *), m);
#endif
error = SMB_TRAN_SEND(vcp, m);
m = 0; /* consumed by SEND */
rqp->sr_lerror = error;
if (error == 0) {
SMBRQ_LOCK(rqp);
rqp->sr_flags |= SMBR_SENT;
rqp->sr_state = SMBRQ_SENT;
SMBRQ_UNLOCK(rqp);
return;
}
/*
* Transport send returned an error.
* Was it a fatal one?
*/
if (SMB_TRAN_FATAL(vcp, error)) {
/*
* No further attempts should be made
*/
fatal:
SMBSDEBUG("TRAN_SEND returned fatal error %d\n", error);
smb_iod_rqprocessed(rqp, error, SMBR_RESTART);
return;
}
}
/*
* Send one request.
*
* SMB2 only
*
* Called by _addrq (for internal requests)
* and _sendall (via _addrq, _multirq, _waitrq)
* Errors are reported via the smb_rq, using:
* smb_iod_rqprocessed(rqp, ...)
*/
static void
smb2_iod_sendrq(struct smb_rq *rqp)
{
struct smb_rq *c_rqp; /* compound */
struct smb_vc *vcp = rqp->sr_vc;
mblk_t *top_m;
mblk_t *cur_m;
int error;
ASSERT(vcp);
ASSERT(RW_WRITE_HELD(&vcp->iod_rqlock));
ASSERT((vcp->vc_flags & SMBV_SMB2) != 0);
/*
* Internal requests are allowed in any state;
* otherwise should be active.
*/
if ((rqp->sr_flags & SMBR_INTERNAL) == 0 &&
vcp->vc_state != SMBIOD_ST_VCACTIVE) {
SMBIODEBUG("bad vc_state=%d\n", vcp->vc_state);
smb_iod_rqprocessed(rqp, ENOTCONN, SMBR_RESTART);
return;
}
/*
* Overwrite the SMB header with the assigned MID and
* (if we're signing) sign it. If there are compounded
* requests after the top one, do those too.
*/
smb2_rq_fillhdr(rqp);
if (rqp->sr2_rqflags & SMB2_FLAGS_SIGNED) {
smb2_rq_sign(rqp);
}
c_rqp = rqp->sr2_compound_next;
while (c_rqp != NULL) {
smb2_rq_fillhdr(c_rqp);
if (c_rqp->sr2_rqflags & SMB2_FLAGS_SIGNED) {
smb2_rq_sign(c_rqp);
}
c_rqp = c_rqp->sr2_compound_next;
}
/*
* The transport send consumes the message and we'd
* prefer to keep a copy, so dupmsg() before sending.
* We also need this to build the compound message
* that we'll actually send. The message offset at
* the start of each compounded message should be
* eight-byte aligned. The caller preparing the
* compounded request has to take care of that
* before we get here and sign messages etc.
*/
top_m = dupmsg(rqp->sr_rq.mb_top);
if (top_m == NULL) {
error = ENOBUFS;
goto fatal;
}
c_rqp = rqp->sr2_compound_next;
while (c_rqp != NULL) {
size_t len = msgdsize(top_m);
ASSERT((len & 7) == 0);
cur_m = dupmsg(c_rqp->sr_rq.mb_top);
if (cur_m == NULL) {
freemsg(top_m);
error = ENOBUFS;
goto fatal;
}
linkb(top_m, cur_m);
}
DTRACE_PROBE2(iod_sendrq,
(smb_rq_t *), rqp, (mblk_t *), top_m);
error = SMB_TRAN_SEND(vcp, top_m);
top_m = 0; /* consumed by SEND */
rqp->sr_lerror = error;
if (error == 0) {
SMBRQ_LOCK(rqp);
rqp->sr_flags |= SMBR_SENT;
rqp->sr_state = SMBRQ_SENT;
SMBRQ_UNLOCK(rqp);
return;
}
/*
* Transport send returned an error.
* Was it a fatal one?
*/
if (SMB_TRAN_FATAL(vcp, error)) {
/*
* No further attempts should be made
*/
fatal:
SMBSDEBUG("TRAN_SEND returned fatal error %d\n", error);
smb_iod_rqprocessed(rqp, error, SMBR_RESTART);
return;
}
}
/*
* Receive one NetBIOS (or NBT over TCP) message. If none have arrived,
* wait up to SMB_NBTIMO (15 sec.) for one to arrive, and then if still
* none have arrived, return ETIME.
*/
static int
smb_iod_recvmsg(struct smb_vc *vcp, mblk_t **mpp)
{
mblk_t *m;
int error;
top:
m = NULL;
error = SMB_TRAN_RECV(vcp, &m);
if (error == EAGAIN)
goto top;
if (error)
return (error);
ASSERT(m != NULL);
m = m_pullup(m, 4);
if (m == NULL) {
return (ENOSR);
}
*mpp = m;
return (0);
}
/*
* How long should we keep around an unused VC (connection)?
* There's usually a good chance connections will be reused,
* so the default is to keep such connections for 5 min.
*/
#ifdef DEBUG
int smb_iod_idle_keep_time = 60; /* seconds */
#else
int smb_iod_idle_keep_time = 300; /* seconds */
#endif
/*
* Process incoming packets
*
* This is the "reader" loop, run by the IOD thread. Normally we're in
* state SMBIOD_ST_VCACTIVE here, but during reconnect we're called in
* other states with poll==TRUE
*
* A non-zero error return here causes the IOD work loop to terminate.
*/
int
smb_iod_recvall(struct smb_vc *vcp, boolean_t poll)
{
mblk_t *m;
int error = 0;
int etime_idle = 0; /* How many 15 sec. "ticks" idle. */
int etime_count = 0; /* ... and when we have requests. */
for (;;) {
/*
* Check whether someone "killed" this VC,
* or is asking the IOD to terminate.
*/
if (vcp->iod_flags & SMBIOD_SHUTDOWN) {
SMBIODEBUG("SHUTDOWN set\n");
/* This IOD thread will terminate. */
SMB_VC_LOCK(vcp);
smb_iod_newstate(vcp, SMBIOD_ST_DEAD);
cv_broadcast(&vcp->vc_statechg);
SMB_VC_UNLOCK(vcp);
error = EINTR;
break;
}
m = NULL;
error = smb_iod_recvmsg(vcp, &m);
/*
* Internal requests (reconnecting) call this in a loop
* (with poll==TRUE) until the request completes.
*/
if (error == ETIME && poll)
break;
if (error == ETIME &&
vcp->iod_rqlist.tqh_first != NULL) {
/*
* Nothing received and requests waiting.
* Increment etime_count. If we were idle,
* skip the 1st tick, because we started
* waiting before there were any requests.
*/
if (etime_idle != 0) {
etime_idle = 0;
} else if (etime_count < INT16_MAX) {
etime_count++;
}
/*
* ETIME and requests in the queue.
* The first time (at 15 sec.)
* Log an error (just once).
*/
if (etime_count > 0 &&
vcp->iod_noresp == B_FALSE) {
vcp->iod_noresp = B_TRUE;
zprintf(vcp->vc_zoneid,
"SMB server %s not responding\n",
vcp->vc_srvname);
}
/*
* At 30 sec. try sending an echo, which
* should cause some response.
*/
if (etime_count == 2) {
SMBIODEBUG("send echo\n");
(void) smb_iod_send_echo(vcp, CRED());
}
/*
* At 45 sec. give up on the connection
* and try to reconnect.
*/
if (etime_count == 3) {
SMB_VC_LOCK(vcp);
smb_iod_newstate(vcp, SMBIOD_ST_RECONNECT);
SMB_VC_UNLOCK(vcp);
SMB_TRAN_DISCONNECT(vcp);
break;
}
continue;
} /* ETIME and requests in the queue */
if (error == ETIME) {
/*
* Nothing received and no active requests.
*
* If we've received nothing from the server for
* smb_iod_idle_keep_time seconds, and the IOD
* thread holds the last reference to this VC,
* move to state IDLE and drop the TCP session.
* The IDLE handler will destroy the VC unless
* vc_state goes to RECONNECT before then.
*/
etime_count = 0;
if (etime_idle < INT16_MAX)
etime_idle++;
if ((etime_idle * SMB_NBTIMO) <
smb_iod_idle_keep_time)
continue;
SMB_VC_LOCK(vcp);
if (vcp->vc_co.co_usecount == 1) {
smb_iod_newstate(vcp, SMBIOD_ST_IDLE);
SMB_VC_UNLOCK(vcp);
SMBIODEBUG("logoff & disconnect\n");
(void) smb_iod_logoff(vcp, CRED());
SMB_TRAN_DISCONNECT(vcp);
error = 0;
break;
}
SMB_VC_UNLOCK(vcp);
continue;
} /* error == ETIME */
if (error) {
/*
* The recv above returned an error indicating
* that our TCP session is no longer usable.
* Disconnect the session and get ready to
* reconnect. If we have pending requests,
* move to state reconnect immediately;
* otherwise move to state IDLE until a
* request is issued on this VC.
*/
SMB_VC_LOCK(vcp);
if (vcp->iod_rqlist.tqh_first != NULL)
smb_iod_newstate(vcp, SMBIOD_ST_RECONNECT);
else
smb_iod_newstate(vcp, SMBIOD_ST_IDLE);
cv_broadcast(&vcp->vc_statechg);
SMB_VC_UNLOCK(vcp);
SMB_TRAN_DISCONNECT(vcp);
break;
}
/*
* Received something. Yea!
*/
etime_count = 0;
etime_idle = 0;
/*
* If we just completed a reconnect after logging
* "SMB server %s not responding" then log OK now.
*/
if (vcp->iod_noresp) {
vcp->iod_noresp = B_FALSE;
zprintf(vcp->vc_zoneid, "SMB server %s OK\n",
vcp->vc_srvname);
}
if ((vcp->vc_flags & SMBV_SMB2) != 0) {
error = smb2_iod_process(vcp, m);
} else {
error = smb1_iod_process(vcp, m);
}
/*
* Reconnect calls this in a loop with poll=TRUE
* We've received a response, so break now.
*/
if (poll) {
error = 0;
break;
}
}
return (error);
}
/*
* Have what should be an SMB1 reply. Check and parse the header,
* then use the message ID to find the request this belongs to and
* post it on that request.
*
* Returns an error if the reader should give up.
* To be safe, error if we read garbage.
*/
static int
smb1_iod_process(smb_vc_t *vcp, mblk_t *m)
{
struct mdchain md;
struct smb_rq *rqp;
uint8_t cmd, sig[4];
uint16_t mid;
int err, skip;
m = m_pullup(m, SMB_HDRLEN);
if (m == NULL)
return (ENOMEM);
/*
* Note: Intentionally do NOT md_done(&md)
* because that would free the message and
* we just want to peek here.
*/
md_initm(&md, m);
/*
* Check the SMB header version and get the MID.
*
* The header version should be SMB1 except when we're
* doing SMB1-to-SMB2 negotiation, in which case we may
* see an SMB2 header with message ID=0 (only allowed in
* vc_state == SMBIOD_ST_CONNECTED -- negotiationg).
*/
err = md_get_mem(&md, sig, 4, MB_MSYSTEM);
if (err)
return (err);
if (sig[1] != 'S' || sig[2] != 'M' || sig[3] != 'B') {
goto bad_hdr;
}
switch (sig[0]) {
case SMB_HDR_V1: /* SMB1 */
md_get_uint8(&md, &cmd);
/* Skip to and get the MID. At offset 5 now. */
skip = SMB_HDR_OFF_MID - 5;
md_get_mem(&md, NULL, skip, MB_MSYSTEM);
err = md_get_uint16le(&md, &mid);
if (err)
return (err);
break;
case SMB_HDR_V2: /* SMB2+ */
if (vcp->vc_state == SMBIOD_ST_CONNECTED) {
/*
* No need to look, can only be
* MID=0, cmd=negotiate
*/
cmd = SMB_COM_NEGOTIATE;
mid = 0;
break;
}
/* FALLTHROUGH */
bad_hdr:
default:
SMBIODEBUG("Bad SMB hdr\n");
m_freem(m);
return (EPROTO);
}
/*
* Find the reqeuest and post the reply
*/
rw_enter(&vcp->iod_rqlock, RW_READER);
TAILQ_FOREACH(rqp, &vcp->iod_rqlist, sr_link) {
if (rqp->sr_mid != mid)
continue;
DTRACE_PROBE2(iod_post_reply,
(smb_rq_t *), rqp, (mblk_t *), m);
m_dumpm(m);
SMBRQ_LOCK(rqp);
if (rqp->sr_rp.md_top == NULL) {
md_initm(&rqp->sr_rp, m);
} else {
if (rqp->sr_flags & SMBR_MULTIPACKET) {
md_append_record(&rqp->sr_rp, m);
} else {
SMBRQ_UNLOCK(rqp);
rqp = NULL;
break;
}
}
smb_iod_rqprocessed_LH(rqp, 0, 0);
SMBRQ_UNLOCK(rqp);
break;
}
rw_exit(&vcp->iod_rqlock);
if (rqp == NULL) {
if (cmd != SMB_COM_ECHO) {
SMBSDEBUG("drop resp: MID 0x%04x\n", (uint_t)mid);
}
m_freem(m);
/*
* Keep going. It's possible this reply came
* after the request timed out and went away.
*/
}
return (0);
}
/*
* Have what should be an SMB2 reply. Check and parse the header,
* then use the message ID to find the request this belongs to and
* post it on that request.
*
* We also want to apply any credit grant in this reply now,
* rather than waiting for the owner to wake up.
*/
static int
smb2_iod_process(smb_vc_t *vcp, mblk_t *m)
{
struct mdchain md;
struct smb_rq *rqp;
uint8_t sig[4];
mblk_t *next_m = NULL;
uint64_t message_id, async_id;
uint32_t flags, next_cmd_off, status;
uint16_t command, credits_granted;
int err;
top:
m = m_pullup(m, SMB2_HDRLEN);
if (m == NULL)
return (ENOMEM);
/*
* Note: Intentionally do NOT md_done(&md)
* because that would free the message and
* we just want to peek here.
*/
md_initm(&md, m);
/*
* Check the SMB header. Must be SMB2
* (and later, could be SMB3 encrypted)
*/
err = md_get_mem(&md, sig, 4, MB_MSYSTEM);
if (err)
return (err);
if (sig[1] != 'S' || sig[2] != 'M' || sig[3] != 'B') {
goto bad_hdr;
}
switch (sig[0]) {
case SMB_HDR_V2:
break;
case SMB_HDR_V3E:
/*
* Todo: If encryption enabled, decrypt the message
* and restart processing on the cleartext.
*/
/* FALLTHROUGH */
bad_hdr:
default:
SMBIODEBUG("Bad SMB2 hdr\n");
m_freem(m);
return (EPROTO);
}
/*
* Parse the rest of the SMB2 header,
* skipping what we don't need.
*/
md_get_uint32le(&md, NULL); /* length, credit_charge */
md_get_uint32le(&md, &status);
md_get_uint16le(&md, &command);
md_get_uint16le(&md, &credits_granted);
md_get_uint32le(&md, &flags);
md_get_uint32le(&md, &next_cmd_off);
md_get_uint64le(&md, &message_id);
if (flags & SMB2_FLAGS_ASYNC_COMMAND) {
md_get_uint64le(&md, &async_id);
} else {
/* PID, TID (not needed) */
async_id = 0;
}
/*
* If this is a compound reply, split it.
* Next must be 8-byte aligned.
*/
if (next_cmd_off != 0) {
if ((next_cmd_off & 7) != 0)
SMBIODEBUG("Misaligned next cmd\n");
else
next_m = m_split(m, next_cmd_off, 1);
}
/*
* SMB2 Negotiate may return zero credits_granted,
* in which case we should assume it granted one.
*/
if (command == SMB2_NEGOTIATE && credits_granted == 0)
credits_granted = 1;
/*
* Apply the credit grant
*/
rw_enter(&vcp->iod_rqlock, RW_WRITER);
vcp->vc2_limit_message_id += credits_granted;
/*
* Find the reqeuest and post the reply
*/
rw_downgrade(&vcp->iod_rqlock);
TAILQ_FOREACH(rqp, &vcp->iod_rqlist, sr_link) {
if (rqp->sr2_messageid != message_id)
continue;
DTRACE_PROBE2(iod_post_reply,
(smb_rq_t *), rqp, (mblk_t *), m);
m_dumpm(m);
/*
* If this is an interim response, just save the
* async ID but don't wakup the request.
* Don't need SMBRQ_LOCK for this.
*/
if (status == NT_STATUS_PENDING && async_id != 0) {
rqp->sr2_rspasyncid = async_id;
m_freem(m);
break;
}
SMBRQ_LOCK(rqp);
if (rqp->sr_rp.md_top == NULL) {
md_initm(&rqp->sr_rp, m);
} else {
SMBRQ_UNLOCK(rqp);
rqp = NULL;
break;
}
smb_iod_rqprocessed_LH(rqp, 0, 0);
SMBRQ_UNLOCK(rqp);
break;
}
rw_exit(&vcp->iod_rqlock);
if (rqp == NULL) {
if (command != SMB2_ECHO) {
SMBSDEBUG("drop resp: MID %lld\n",
(long long)message_id);
}
m_freem(m);
/*
* Keep going. It's possible this reply came
* after the request timed out and went away.
*/
}
/*
* If we split a compound reply, continue with the
* next part of the compound.
*/
if (next_m != NULL) {
m = next_m;
goto top;
}
return (0);
}
/*
* The IOD receiver thread has requests pending and
* has not received anything in a while. Try to
* send an SMB echo request. It's tricky to do a
* send from the IOD thread because we can't block.
*
* Using tmo=SMBNOREPLYWAIT in the request
* so smb_rq_reply will skip smb_iod_waitrq.
* The smb_smb_echo call uses SMBR_INTERNAL
* to avoid calling smb_iod_sendall().
*/
static int
smb_iod_send_echo(smb_vc_t *vcp, cred_t *cr)
{
smb_cred_t scred;
int err, tmo = SMBNOREPLYWAIT;
ASSERT(vcp->iod_thr == curthread);
smb_credinit(&scred, cr);
if ((vcp->vc_flags & SMBV_SMB2) != 0) {
err = smb2_smb_echo(vcp, &scred, tmo);
} else {
err = smb_smb_echo(vcp, &scred, tmo);
}
smb_credrele(&scred);
return (err);
}
/*
* Helper for smb1_iod_addrq, smb2_iod_addrq
* Returns zero if interrupted, else 1.
*/
static int
smb_iod_muxwait(smb_vc_t *vcp, boolean_t sig_ok)
{
int rc;
SMB_VC_LOCK(vcp);
vcp->iod_muxwant++;
if (sig_ok) {
rc = cv_wait_sig(&vcp->iod_muxwait, &vcp->vc_lock);
} else {
cv_wait(&vcp->iod_muxwait, &vcp->vc_lock);
rc = 1;
}
vcp->iod_muxwant--;
SMB_VC_UNLOCK(vcp);
return (rc);
}
/*
* Place request in the queue, and send it.
* Called with no locks held.
*
* Called for SMB1 only
*
* The logic for how we limit active requests differs between
* SMB1 and SMB2. With SMB1 it's a simple counter ioc_muxcnt.
*/
int
smb1_iod_addrq(struct smb_rq *rqp)
{
struct smb_vc *vcp = rqp->sr_vc;
uint16_t need;
boolean_t sig_ok =
(rqp->sr_flags & SMBR_NOINTR_SEND) == 0;
ASSERT(rqp->sr_cred);
ASSERT((vcp->vc_flags & SMBV_SMB2) == 0);
rqp->sr_owner = curthread;
rw_enter(&vcp->iod_rqlock, RW_WRITER);
recheck:
/*
* Internal requests can be added in any state,
* but normal requests only in state active.
*/
if ((rqp->sr_flags & SMBR_INTERNAL) == 0 &&
vcp->vc_state != SMBIOD_ST_VCACTIVE) {
SMBIODEBUG("bad vc_state=%d\n", vcp->vc_state);
rw_exit(&vcp->iod_rqlock);
return (ENOTCONN);
}
/*
* If we're at the limit of active requests, block until
* enough requests complete so we can make ours active.
* Wakeup in smb_iod_removerq().
*
* Normal callers leave one slot free, so internal
* callers can have the last slot if needed.
*/
need = 1;
if ((rqp->sr_flags & SMBR_INTERNAL) == 0)
need++;
if ((vcp->iod_muxcnt + need) > vcp->vc_maxmux) {
rw_exit(&vcp->iod_rqlock);
if (rqp->sr_flags & SMBR_INTERNAL)
return (EBUSY);
if (smb_iod_muxwait(vcp, sig_ok) == 0)
return (EINTR);
rw_enter(&vcp->iod_rqlock, RW_WRITER);
goto recheck;
}
/*
* Add this request to the active list and send it.
* For SMB2 we may have a sequence of compounded
* requests, in which case we must add them all.
* They're sent as a compound in smb2_iod_sendrq.
*/
rqp->sr_mid = vcp->vc_next_mid++;
/* If signing, set the signing sequence numbers. */
if (vcp->vc_mackey != NULL && (rqp->sr_rqflags2 &
SMB_FLAGS2_SECURITY_SIGNATURE) != 0) {
rqp->sr_seqno = vcp->vc_next_seq++;
rqp->sr_rseqno = vcp->vc_next_seq++;
}
vcp->iod_muxcnt++;
TAILQ_INSERT_TAIL(&vcp->iod_rqlist, rqp, sr_link);
smb1_iod_sendrq(rqp);
rw_exit(&vcp->iod_rqlock);
return (0);
}
/*
* Place request in the queue, and send it.
* Called with no locks held.
*
* Called for SMB2 only.
*
* With SMB2 we have a range of valid message IDs, and we may
* only send requests when we can assign a message ID within
* the valid range. We may need to wait here for some active
* request to finish (and update vc2_limit_message_id) before
* we can get message IDs for our new request(s). Another
* difference is that the request sequence we're waiting to
* add here may require multipe message IDs, either due to
* either compounding or multi-credit requests. Therefore
* we need to wait for the availibility of how ever many
* message IDs are required by our request sequence.
*/
int
smb2_iod_addrq(struct smb_rq *rqp)
{
struct smb_vc *vcp = rqp->sr_vc;
struct smb_rq *c_rqp; /* compound req */
uint16_t charge;
boolean_t sig_ok =
(rqp->sr_flags & SMBR_NOINTR_SEND) == 0;
ASSERT(rqp->sr_cred != NULL);
ASSERT((vcp->vc_flags & SMBV_SMB2) != 0);
/*
* Figure out the credit charges
* No multi-credit messages yet.
*/
rqp->sr2_totalcreditcharge = rqp->sr2_creditcharge;
c_rqp = rqp->sr2_compound_next;
while (c_rqp != NULL) {
rqp->sr2_totalcreditcharge += c_rqp->sr2_creditcharge;
c_rqp = c_rqp->sr2_compound_next;
}
/*
* Internal request must not be compounded
* and should use exactly one credit.
*/
if (rqp->sr_flags & SMBR_INTERNAL) {
if (rqp->sr2_compound_next != NULL) {
ASSERT(0);
return (EINVAL);
}
}
rqp->sr_owner = curthread;
rw_enter(&vcp->iod_rqlock, RW_WRITER);
recheck:
/*
* Internal requests can be added in any state,
* but normal requests only in state active.
*/
if ((rqp->sr_flags & SMBR_INTERNAL) == 0 &&
vcp->vc_state != SMBIOD_ST_VCACTIVE) {
SMBIODEBUG("bad vc_state=%d\n", vcp->vc_state);
rw_exit(&vcp->iod_rqlock);
return (ENOTCONN);
}
/*
* If we're at the limit of active requests, block until
* enough requests complete so we can make ours active.
* Wakeup in smb_iod_removerq().
*
* Normal callers leave one slot free, so internal
* callers can have the last slot if needed.
*/
charge = rqp->sr2_totalcreditcharge;
if ((rqp->sr_flags & SMBR_INTERNAL) == 0)
charge++;
if ((vcp->vc2_next_message_id + charge) >
vcp->vc2_limit_message_id) {
rw_exit(&vcp->iod_rqlock);
if (rqp->sr_flags & SMBR_INTERNAL)
return (EBUSY);
if (smb_iod_muxwait(vcp, sig_ok) == 0)
return (EINTR);
rw_enter(&vcp->iod_rqlock, RW_WRITER);
goto recheck;
}
/*
* Add this request to the active list and send it.
* For SMB2 we may have a sequence of compounded
* requests, in which case we must add them all.
* They're sent as a compound in smb2_iod_sendrq.
*/
rqp->sr2_messageid = vcp->vc2_next_message_id;
vcp->vc2_next_message_id += rqp->sr2_creditcharge;
TAILQ_INSERT_TAIL(&vcp->iod_rqlist, rqp, sr_link);
c_rqp = rqp->sr2_compound_next;
while (c_rqp != NULL) {
c_rqp->sr2_messageid = vcp->vc2_next_message_id;
vcp->vc2_next_message_id += c_rqp->sr2_creditcharge;
TAILQ_INSERT_TAIL(&vcp->iod_rqlist, c_rqp, sr_link);
c_rqp = c_rqp->sr2_compound_next;
}
smb2_iod_sendrq(rqp);
rw_exit(&vcp->iod_rqlock);
return (0);
}
/*
* Mark an SMBR_MULTIPACKET request as
* needing another send. Similar to the
* "normal" part of smb1_iod_addrq.
* Only used by SMB1
*/
int
smb1_iod_multirq(struct smb_rq *rqp)
{
struct smb_vc *vcp = rqp->sr_vc;
ASSERT(rqp->sr_flags & SMBR_MULTIPACKET);
if (vcp->vc_flags & SMBV_SMB2) {
ASSERT("!SMB2?");
return (EINVAL);
}
if (rqp->sr_flags & SMBR_INTERNAL)
return (EINVAL);
if (vcp->vc_state != SMBIOD_ST_VCACTIVE) {
SMBIODEBUG("bad vc_state=%d\n", vcp->vc_state);
return (ENOTCONN);
}
rw_enter(&vcp->iod_rqlock, RW_WRITER);
/* Already on iod_rqlist, just reset state. */
rqp->sr_state = SMBRQ_NOTSENT;
smb1_iod_sendrq(rqp);
rw_exit(&vcp->iod_rqlock);
return (0);
}
/*
* Remove a request from the active list, and
* wake up requests waiting to go active.
*
* Shared by SMB1 + SMB2
*
* The logic for how we limit active requests differs between
* SMB1 and SMB2. With SMB1 it's a simple counter ioc_muxcnt.
* With SMB2 we have a range of valid message IDs, and when we
* retire the oldest request we need to keep track of what is
* now the oldest message ID. In both cases, after we take a
* request out of the list here, we should be able to wake up
* a request waiting to get in the active list.
*/
void
smb_iod_removerq(struct smb_rq *rqp)
{
struct smb_rq *rqp2;
struct smb_vc *vcp = rqp->sr_vc;
boolean_t was_head = B_FALSE;
rw_enter(&vcp->iod_rqlock, RW_WRITER);
#ifdef QUEUEDEBUG
/*
* Make sure we have not already removed it.
* See sys/queue.h QUEUEDEBUG_TAILQ_POSTREMOVE
* XXX: Don't like the constant 1 here...
*/
ASSERT(rqp->sr_link.tqe_next != (void *)1L);
#endif
if (TAILQ_FIRST(&vcp->iod_rqlist) == rqp)
was_head = B_TRUE;
TAILQ_REMOVE(&vcp->iod_rqlist, rqp, sr_link);
if (vcp->vc_flags & SMBV_SMB2) {
rqp2 = TAILQ_FIRST(&vcp->iod_rqlist);
if (was_head && rqp2 != NULL) {
/* Do we still need this? */
vcp->vc2_oldest_message_id =
rqp2->sr2_messageid;
}
} else {
ASSERT(vcp->iod_muxcnt > 0);
vcp->iod_muxcnt--;
}
rw_exit(&vcp->iod_rqlock);
/*
* If there are requests waiting for "mux" slots,
* wake one.
*/
SMB_VC_LOCK(vcp);
if (vcp->iod_muxwant != 0)
cv_signal(&vcp->iod_muxwait);
SMB_VC_UNLOCK(vcp);
}
/*
* Wait for a request to complete.
*/
int
smb_iod_waitrq(struct smb_rq *rqp)
{
struct smb_vc *vcp = rqp->sr_vc;
clock_t tr, tmo1, tmo2;
int error;
if (rqp->sr_flags & SMBR_INTERNAL) {
/* XXX - Do we ever take this path now? */
return (smb_iod_waitrq_int(rqp));
}
/*
* Make sure this is NOT the IOD thread,
* or the wait below will stop the reader.
*/
ASSERT(curthread != vcp->iod_thr);
SMBRQ_LOCK(rqp);
/*
* The request has been sent. Now wait for the response,
* with the timeout specified for this request.
* Compute all the deadlines now, so we effectively
* start the timer(s) after the request is sent.
*/
if (smb_timo_notice && (smb_timo_notice < rqp->sr_timo))
tmo1 = SEC_TO_TICK(smb_timo_notice);
else
tmo1 = 0;
tmo2 = ddi_get_lbolt() + SEC_TO_TICK(rqp->sr_timo);
/*
* As above, we don't want to allow interrupt for some
* requests like open, because we could miss a succesful
* response and therefore "leak" a FID. Such requests
* are marked SMBR_NOINTR_RECV to prevent that.
*
* If "slow server" warnings are enabled, wait first
* for the "notice" timeout, and warn if expired.
*/
if (tmo1 && rqp->sr_rpgen == rqp->sr_rplast) {
if (rqp->sr_flags & SMBR_NOINTR_RECV)
tr = cv_reltimedwait(&rqp->sr_cond,
&rqp->sr_lock, tmo1, TR_CLOCK_TICK);
else
tr = cv_reltimedwait_sig(&rqp->sr_cond,
&rqp->sr_lock, tmo1, TR_CLOCK_TICK);
if (tr == 0) {
error = EINTR;
goto out;
}
if (tr < 0) {
DTRACE_PROBE1(smb_iod_waitrq1,
(smb_rq_t *), rqp);
}
}
/*
* Keep waiting until tmo2 is expired.
*/
while (rqp->sr_rpgen == rqp->sr_rplast) {
if (rqp->sr_flags & SMBR_NOINTR_RECV)
tr = cv_timedwait(&rqp->sr_cond,
&rqp->sr_lock, tmo2);
else
tr = cv_timedwait_sig(&rqp->sr_cond,
&rqp->sr_lock, tmo2);
if (tr == 0) {
error = EINTR;
goto out;
}
if (tr < 0) {
DTRACE_PROBE1(smb_iod_waitrq2,
(smb_rq_t *), rqp);
error = ETIME;
goto out;
}
/* got wakeup */
}
error = rqp->sr_lerror;
rqp->sr_rplast++;
out:
SMBRQ_UNLOCK(rqp);
/*
* MULTIPACKET request must stay in the list.
* They may need additional responses.
*/
if ((rqp->sr_flags & SMBR_MULTIPACKET) == 0)
smb_iod_removerq(rqp);
return (error);
}
/*
* Internal variant of smb_iod_waitrq(), for use in
* requests run by the IOD (reader) thread itself.
* Block only long enough to receive one reply.
*/
int
smb_iod_waitrq_int(struct smb_rq *rqp)
{
struct smb_vc *vcp = rqp->sr_vc;
int timeleft = rqp->sr_timo;
int error;
ASSERT((rqp->sr_flags & SMBR_MULTIPACKET) == 0);
again:
error = smb_iod_recvall(vcp, B_TRUE);
if (error == ETIME) {
/* We waited SMB_NBTIMO sec. */
timeleft -= SMB_NBTIMO;
if (timeleft > 0)
goto again;
}
smb_iod_removerq(rqp);
if (rqp->sr_state != SMBRQ_NOTIFIED)
error = ETIME;
return (error);
}
/*
* Shutdown all outstanding I/O requests on the specified share with
* ENXIO; used when unmounting a share. (There shouldn't be any for a
* non-forced unmount; if this is a forced unmount, we have to shutdown
* the requests as part of the unmount process.)
*/
void
smb_iod_shutdown_share(struct smb_share *ssp)
{
struct smb_vc *vcp = SSTOVC(ssp);
struct smb_rq *rqp;
/*
* Loop through the list of requests and shutdown the ones
* that are for the specified share.
*/
rw_enter(&vcp->iod_rqlock, RW_READER);
TAILQ_FOREACH(rqp, &vcp->iod_rqlist, sr_link) {
if (rqp->sr_state != SMBRQ_NOTIFIED && rqp->sr_share == ssp)
smb_iod_rqprocessed(rqp, EIO, 0);
}
rw_exit(&vcp->iod_rqlock);
}
/*
* Ioctl functions called by the user-level I/O Deamon (IOD)
* to bring up and service a connection to some SMB server.
*/
/*
* Handle ioctl SMBIOC_IOD_CONNECT
*/
int
nsmb_iod_connect(struct smb_vc *vcp, cred_t *cr)
{
int err, val;
ASSERT(vcp->iod_thr == curthread);
if (vcp->vc_state != SMBIOD_ST_RECONNECT) {
cmn_err(CE_NOTE, "iod_connect: bad state %d", vcp->vc_state);
return (EINVAL);
}
/*
* Putting a TLI endpoint back in the right state for a new
* connection is a bit tricky. In theory, this could be:
* SMB_TRAN_DISCONNECT(vcp);
* SMB_TRAN_UNBIND(vcp);
* but that method often results in TOUTSTATE errors.
* It's easier to just close it and open a new endpoint.
*/
SMB_VC_LOCK(vcp);
if (vcp->vc_tdata)
SMB_TRAN_DONE(vcp);
err = SMB_TRAN_CREATE(vcp, cr);
SMB_VC_UNLOCK(vcp);
if (err != 0)
return (err);
/*
* Set various options on this endpoint.
* Keep going in spite of errors.
*/
val = smb_tcpsndbuf;
err = SMB_TRAN_SETPARAM(vcp, SMBTP_SNDBUF, &val);
if (err != 0) {
cmn_err(CE_NOTE, "iod_connect: setopt SNDBUF, err=%d", err);
}
val = smb_tcprcvbuf;
err = SMB_TRAN_SETPARAM(vcp, SMBTP_RCVBUF, &val);
if (err != 0) {
cmn_err(CE_NOTE, "iod_connect: setopt RCVBUF, err=%d", err);
}
val = 1;
err = SMB_TRAN_SETPARAM(vcp, SMBTP_KEEPALIVE, &val);
if (err != 0) {
cmn_err(CE_NOTE, "iod_connect: setopt KEEPALIVE, err=%d", err);
}
val = 1;
err = SMB_TRAN_SETPARAM(vcp, SMBTP_TCP_NODELAY, &val);
if (err != 0) {
cmn_err(CE_NOTE, "iod_connect: setopt TCP_NODELAY err=%d", err);
}
val = smb_connect_timeout * 1000;
err = SMB_TRAN_SETPARAM(vcp, SMBTP_TCP_CON_TMO, &val);
if (err != 0) {
cmn_err(CE_NOTE, "iod_connect: setopt TCP con tmo err=%d", err);
}
/*
* Bind and connect
*/
err = SMB_TRAN_BIND(vcp, NULL);
if (err != 0) {
cmn_err(CE_NOTE, "iod_connect: t_kbind: err=%d", err);
/* Continue on and try connect. */
}
err = SMB_TRAN_CONNECT(vcp, &vcp->vc_srvaddr.sa);
/*
* No cmn_err here, as connect failures are normal, i.e.
* when a server has multiple addresses and only some are
* routed for us. (libsmbfs tries them all)
*/
if (err == 0) {
SMB_VC_LOCK(vcp);
smb_iod_newstate(vcp, SMBIOD_ST_CONNECTED);
SMB_VC_UNLOCK(vcp);
} /* else stay in state reconnect */
return (err);
}
/*
* Handle ioctl SMBIOC_IOD_NEGOTIATE
* Do the whole SMB1/SMB2 negotiate
*
* This is where we send our first request to the server.
* If this is the first time we're talking to this server,
* (meaning not a reconnect) then we don't know whether
* the server supports SMB2, so we need to use the weird
* SMB1-to-SMB2 negotiation. That's where we send an SMB1
* negotiate including dialect "SMB 2.???" and if the
* server supports SMB2 we get an SMB2 reply -- Yes, an
* SMB2 reply to an SMB1 request. A strange protocol...
*
* If on the other hand we already know the server supports
* SMB2 (because this is a reconnect) or if the client side
* has disabled SMB1 entirely, we'll skip the SMB1 part.
*/
int
nsmb_iod_negotiate(struct smb_vc *vcp, cred_t *cr)
{
struct smb_sopt *sv = &vcp->vc_sopt;
smb_cred_t scred;
int err = 0;
ASSERT(vcp->iod_thr == curthread);
smb_credinit(&scred, cr);
if (vcp->vc_state != SMBIOD_ST_CONNECTED) {
cmn_err(CE_NOTE, "iod_negotiate: bad state %d", vcp->vc_state);
err = EINVAL;
goto out;
}
if (vcp->vc_maxver == 0 || vcp->vc_minver > vcp->vc_maxver) {
err = EINVAL;
goto out;
}
/*
* (Re)init negotiated values
*/
bzero(sv, sizeof (*sv));
vcp->vc2_next_message_id = 0;
vcp->vc2_limit_message_id = 1;
vcp->vc2_session_id = 0;
vcp->vc_next_seq = 0;
/*
* If this was reconnect, get rid of the old MAC key
* and session key.
*/
SMB_VC_LOCK(vcp);
if (vcp->vc_mackey != NULL) {
kmem_free(vcp->vc_mackey, vcp->vc_mackeylen);
vcp->vc_mackey = NULL;
vcp->vc_mackeylen = 0;
}
if (vcp->vc_ssnkey != NULL) {
kmem_free(vcp->vc_ssnkey, vcp->vc_ssnkeylen);
vcp->vc_ssnkey = NULL;
vcp->vc_ssnkeylen = 0;
}
SMB_VC_UNLOCK(vcp);
/*
* If this is not an SMB2 reconect (SMBV_SMB2 not set),
* and if SMB1 is enabled, do SMB1 neogotiate. Then
* if either SMB1-to-SMB2 negotiate tells us we should
* switch to SMB2, or the local configuration has
* disabled SMB1, set the SMBV_SMB2 flag.
*
* Note that vc_maxver is handled in smb_smb_negotiate
* so we never get sv_proto == SMB_DIALECT_SMB2_FF when
* the local configuration disables SMB2, and therefore
* we won't set the SMBV_SMB2 flag.
*/
if ((vcp->vc_flags & SMBV_SMB2) == 0) {
if (vcp->vc_minver < SMB2_DIALECT_BASE) {
/*
* SMB1 is enabled
*/
err = smb_smb_negotiate(vcp, &scred);
if (err != 0)
goto out;
}
/*
* If SMB1-to-SMB2 negotiate told us we should
* switch to SMB2, or if the local configuration
* disables SMB1, set the SMB2 flag.
*/
if (sv->sv_proto == SMB_DIALECT_SMB2_FF ||
vcp->vc_minver >= SMB2_DIALECT_BASE) {
/*
* Switch this VC to SMB2.
*/
SMB_VC_LOCK(vcp);
vcp->vc_flags |= SMBV_SMB2;
SMB_VC_UNLOCK(vcp);
}
}
/*
* If this is an SMB2 reconnect (SMBV_SMB2 was set before this
* function was called), or SMB1-to-SMB2 negotiate indicated
* we should switch to SMB2, or we have SMB1 disabled (both
* cases set SMBV_SMB2 above), then do SMB2 negotiate.
*/
if ((vcp->vc_flags & SMBV_SMB2) != 0) {
err = smb2_smb_negotiate(vcp, &scred);
}
out:
if (err == 0) {
SMB_VC_LOCK(vcp);
smb_iod_newstate(vcp, SMBIOD_ST_NEGOTIATED);
SMB_VC_UNLOCK(vcp);
}
/*
* (else) leave state as it was.
* User-level will either close this handle (if connecting
* for the first time) or call rcfail and then try again.
*/
smb_credrele(&scred);
return (err);
}
/*
* Handle ioctl SMBIOC_IOD_SSNSETUP
* Do either SMB1 or SMB2 session setup (one call/reply)
*/
int
nsmb_iod_ssnsetup(struct smb_vc *vcp, cred_t *cr)
{
smb_cred_t scred;
int err;
ASSERT(vcp->iod_thr == curthread);
switch (vcp->vc_state) {
case SMBIOD_ST_NEGOTIATED:
case SMBIOD_ST_AUTHCONT:
break;
default:
return (EINVAL);
}
smb_credinit(&scred, cr);
if (vcp->vc_flags & SMBV_SMB2)
err = smb2_smb_ssnsetup(vcp, &scred);
else
err = smb_smb_ssnsetup(vcp, &scred);
smb_credrele(&scred);
SMB_VC_LOCK(vcp);
switch (err) {
case 0:
smb_iod_newstate(vcp, SMBIOD_ST_AUTHOK);
break;
case EINPROGRESS: /* MORE_PROCESSING_REQUIRED */
smb_iod_newstate(vcp, SMBIOD_ST_AUTHCONT);
break;
default:
smb_iod_newstate(vcp, SMBIOD_ST_AUTHFAIL);
break;
}
SMB_VC_UNLOCK(vcp);
return (err);
}
static int
smb_iod_logoff(struct smb_vc *vcp, cred_t *cr)
{
smb_cred_t scred;
int err;
ASSERT(vcp->iod_thr == curthread);
smb_credinit(&scred, cr);
if (vcp->vc_flags & SMBV_SMB2)
err = smb2_smb_logoff(vcp, &scred);
else
err = smb_smb_logoff(vcp, &scred);
smb_credrele(&scred);
return (err);
}
/*
* Handle ioctl SMBIOC_IOD_WORK
*
* The smbiod agent calls this after authentication to become
* the reader for this session, so long as that's possible.
* This should only return non-zero if we want that agent to
* give up on this VC permanently.
*/
/* ARGSUSED */
int
smb_iod_vc_work(struct smb_vc *vcp, int flags, cred_t *cr)
{
smbioc_ssn_work_t *wk = &vcp->vc_work;
int err = 0;
/*
* This is called by the one-and-only
* IOD thread for this VC.
*/
ASSERT(vcp->iod_thr == curthread);
/*
* Should be in state...
*/
if (vcp->vc_state != SMBIOD_ST_AUTHOK) {
cmn_err(CE_NOTE, "iod_vc_work: bad state %d", vcp->vc_state);
return (EINVAL);
}
/*
* Update the session key and initialize SMB signing.
*
* This implementation does not use multiple SMB sessions per
* TCP connection (where only the first session key is used)
* so we always have a new session key here. Sanity check the
* length from user space. Normally 16 or 32.
*/
if (wk->wk_u_ssnkey_len > 1024) {
cmn_err(CE_NOTE, "iod_vc_work: ssn key too long");
return (EINVAL);
}
ASSERT(vcp->vc_ssnkey == NULL);
SMB_VC_LOCK(vcp);
if (wk->wk_u_ssnkey_len != 0 &&
wk->wk_u_ssnkey_buf.lp_ptr != NULL) {
vcp->vc_ssnkeylen = wk->wk_u_ssnkey_len;
vcp->vc_ssnkey = kmem_alloc(vcp->vc_ssnkeylen, KM_SLEEP);
if (ddi_copyin(wk->wk_u_ssnkey_buf.lp_ptr,
vcp->vc_ssnkey, vcp->vc_ssnkeylen, flags) != 0) {
err = EFAULT;
}
}
SMB_VC_UNLOCK(vcp);
if (err)
return (err);
/*
* If we have a session key, derive the MAC key for SMB signing.
* If this was a NULL session, we might have no session key.
*/
ASSERT(vcp->vc_mackey == NULL);
if (vcp->vc_ssnkey != NULL) {
if (vcp->vc_flags & SMBV_SMB2)
err = smb2_sign_init(vcp);
else
err = smb_sign_init(vcp);
if (err != 0)
return (err);
}
/*
* Tell any enqueued requests they can start.
*/
SMB_VC_LOCK(vcp);
vcp->vc_genid++; /* possibly new connection */
smb_iod_newstate(vcp, SMBIOD_ST_VCACTIVE);
cv_broadcast(&vcp->vc_statechg);
SMB_VC_UNLOCK(vcp);
/*
* The above cv_broadcast should be sufficient to
* get requests going again.
*
* If we have a callback function, run it.
* Was: smb_iod_notify_connected()
*/
if (fscb && fscb->fscb_connect)
smb_vc_walkshares(vcp, fscb->fscb_connect);
/*
* Run the "reader" loop. An error return here is normal
* (i.e. when we need to reconnect) so ignore errors.
* Note: This call updates the vc_state.
*/
(void) smb_iod_recvall(vcp, B_FALSE);
/*
* The reader loop returned, so we must have a
* new state. (disconnected or reconnecting)
*
* Notify shares of the disconnect.
* Was: smb_iod_notify_disconnect()
*/
smb_vc_walkshares(vcp, smb_iod_share_disconnected);
/*
* The reader loop function returns only when
* there's been an error on the connection, or
* this VC has no more references. It also
* updates the state before it returns.
*
* Tell any requests to give up or restart.
*/
smb_iod_invrq(vcp);
return (err);
}
/*
* Handle ioctl SMBIOC_IOD_IDLE
*
* Wait around for someone to ask to use this VC again after the
* TCP session has closed. When one of the connected trees adds a
* request, smb_iod_reconnect will set vc_state to RECONNECT and
* wake this cv_wait. When a VC ref. goes away in smb_vc_rele,
* that also signals this wait so we can re-check whether we
* now hold the last ref. on this VC (and can destroy it).
*/
int
smb_iod_vc_idle(struct smb_vc *vcp)
{
int err = 0;
boolean_t destroy = B_FALSE;
/*
* This is called by the one-and-only
* IOD thread for this VC.
*/
ASSERT(vcp->iod_thr == curthread);
/*
* Should be in state...
*/
if (vcp->vc_state != SMBIOD_ST_IDLE &&
vcp->vc_state != SMBIOD_ST_RECONNECT) {
cmn_err(CE_NOTE, "iod_vc_idle: bad state %d", vcp->vc_state);
return (EINVAL);
}
SMB_VC_LOCK(vcp);
while (vcp->vc_state == SMBIOD_ST_IDLE &&
vcp->vc_co.co_usecount > 1) {
if (cv_wait_sig(&vcp->iod_idle, &vcp->vc_lock) == 0) {
err = EINTR;
break;
}
}
if (vcp->vc_state == SMBIOD_ST_IDLE &&
vcp->vc_co.co_usecount == 1) {
/*
* We were woken because we now have the last ref.
* Arrange for this VC to be destroyed now.
* Set the "GONE" flag while holding the lock,
* to prevent a race with new references.
* The destroy happens after unlock.
*/
vcp->vc_flags |= SMBV_GONE;
destroy = B_TRUE;
}
SMB_VC_UNLOCK(vcp);
if (destroy) {
/* This sets vc_state = DEAD */
smb_iod_disconnect(vcp);
}
return (err);
}
/*
* Handle ioctl SMBIOC_IOD_RCFAIL
*
* After a failed reconnect attempt, smbiod will
* call this to make current requests error out.
*/
int
smb_iod_vc_rcfail(struct smb_vc *vcp)
{
clock_t tr;
int err = 0;
/*
* This is called by the one-and-only
* IOD thread for this VC.
*/
ASSERT(vcp->iod_thr == curthread);
SMB_VC_LOCK(vcp);
smb_iod_newstate(vcp, SMBIOD_ST_RCFAILED);
cv_broadcast(&vcp->vc_statechg);
/*
* Short wait here for two reasons:
* (1) Give requests a chance to error out.
* (2) Prevent immediate retry.
*/
tr = cv_reltimedwait_sig(&vcp->iod_idle, &vcp->vc_lock,
SEC_TO_TICK(5), TR_CLOCK_TICK);
if (tr == 0)
err = EINTR;
/*
* Normally we'll switch to state IDLE here. However,
* if something called smb_iod_reconnect() while we were
* waiting above, we'll be in in state reconnect already.
* In that case, keep state RECONNECT, so we essentially
* skip transition through state IDLE that would normally
* happen next.
*/
if (vcp->vc_state != SMBIOD_ST_RECONNECT) {
smb_iod_newstate(vcp, SMBIOD_ST_IDLE);
cv_broadcast(&vcp->vc_statechg);
}
SMB_VC_UNLOCK(vcp);
return (err);
}
/*
* Ask the IOD to reconnect (if not already underway)
* then wait for the reconnect to finish.
*/
int
smb_iod_reconnect(struct smb_vc *vcp)
{
int err = 0, rv;
SMB_VC_LOCK(vcp);
again:
switch (vcp->vc_state) {
case SMBIOD_ST_IDLE:
/* Tell the IOD thread it's no longer IDLE. */
smb_iod_newstate(vcp, SMBIOD_ST_RECONNECT);
cv_signal(&vcp->iod_idle);
/* FALLTHROUGH */
case SMBIOD_ST_RECONNECT:
case SMBIOD_ST_CONNECTED:
case SMBIOD_ST_NEGOTIATED:
case SMBIOD_ST_AUTHCONT:
case SMBIOD_ST_AUTHOK:
/* Wait for the VC state to become ACTIVE. */
rv = cv_wait_sig(&vcp->vc_statechg, &vcp->vc_lock);
if (rv == 0) {
err = EINTR;
break;
}
goto again;
case SMBIOD_ST_VCACTIVE:
err = 0; /* success! */
break;
case SMBIOD_ST_AUTHFAIL:
case SMBIOD_ST_RCFAILED:
case SMBIOD_ST_DEAD:
default:
err = ENOTCONN;
break;
}
SMB_VC_UNLOCK(vcp);
return (err);
}
|