-
Notifications
You must be signed in to change notification settings - Fork 7
/
smb_iod.c
4565 lines (4036 loc) · 162 KB
/
smb_iod.c
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
/*
* Copyright (c) 2000-2001 Boris Popov
* All rights reserved.
*
* Portions Copyright (C) 2001 - 2019 Apple Inc. 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.
*
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/kpi_mbuf.h>
#include <sys/unistd.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/reboot.h>
#include <sys/kauth.h>
#include <sys/smb_apple.h>
#include <netsmb/smb.h>
#include <netsmb/smb_2.h>
#include <netsmb/smb_rq.h>
#include <netsmb/smb_rq_2.h>
#include <netsmb/smb_conn.h>
#include <netsmb/smb_conn_2.h>
#include <netsmb/smb_rq.h>
#include <netsmb/smb_rq_2.h>
#include <netsmb/smb_tran.h>
#include <netsmb/smb_trantcp.h>
#include <netsmb/smb_subr.h>
#include <netsmb/smb_gss.h>
#include <netsmb/smb_dev_2.h>
#include <smbfs/smbfs.h>
#include <netsmb/smb_packets_2.h>
#include <smbclient/ntstatus.h>
#include <smbfs/smbfs_node.h>
#include <smbfs/smbfs_subr_2.h>
#include <smbfs/smb2_mc_support.h>
#include <IOKit/IOLib.h>
#include <IOKit/IOPlatformExpert.h>
#include <netsmb/smb_sleephandler.h>
#define MAX_CHANNEL_LIST (12)
#define OSIncrementAtomic64(a) \
(OSIncrementAtomic64(__SAFE_CAST_PTR(volatile SInt64*,a)))
struct smb_reconnect_stats smb_reconn_stats;
static int smb_iod_next = 0;
int smb_iod_sendall(struct smbiod *iod);
int smb_iod_get_interface_info(struct smbiod *iod);
static int smb_iod_main_ch_failover(struct smbiod *iod);
static void smb_iod_alt_ch_failover(struct smbiod *iod);
static int smb_iod_proclaim_main(struct smbiod *iod, struct smbiod *iod_main);
static void smb_iod_read_thread(void *arg);
static void smb_iod_detach_con_entry(struct smbiod *iod);
static void smb_iod_lease_dequeue(struct smbiod *iod);
/*
* Check to see if the share has a routine to handle going away, if so.
*/
static int isShareGoingAway(struct smb_share* share)
{
int goingAway = FALSE;
lck_mtx_lock(&share->ss_shlock);
if (share->ss_going_away) {
goingAway = share->ss_going_away(share);
}
lck_mtx_unlock(&share->ss_shlock);
return goingAway;
}
static int smb_iod_check_timeout(struct timespec *starttime, int SecondsTillTimeout)
{
struct timespec waittime, tsnow;
waittime.tv_sec = SecondsTillTimeout;
waittime.tv_nsec = 0;
timespecadd(&waittime, starttime);
nanouptime(&tsnow);
if (timespeccmp(&tsnow, &waittime, >))
return TRUE;
else return FALSE;
}
static int smb_iod_check_non_idempotent(struct smbiod *iod)
{
int error = 0;
struct smb_rq *rqp, *trqp;
struct smb_rq *tmp_rqp;
int found_create = 0;
int found_close = 0;
int found_non_idempotent = 0;
/* Search through the request list and check for non idempotent requests */
SMB_IOD_RQLOCK(iod);
TAILQ_FOREACH_SAFE(rqp, &iod->iod_rqlist, sr_link, trqp) {
SMBRQ_SLOCK(rqp);
if (!(rqp->sr_flags & SMBR_COMPOUND_RQ)) {
/* Single Request to be checked */
if (rqp->sr_extflags & SMB2_NON_IDEMPOTENT) {
/* Its a non idempotent request so return error */
switch (rqp->sr_command) {
case SMB2_CREATE:
smb_reconn_stats.fail_create_cnt += 1;
break;
case SMB2_CLOSE:
smb_reconn_stats.fail_close_cnt += 1;
break;
case SMB2_LOCK:
smb_reconn_stats.fail_lock_cnt += 1;
break;
case SMB2_IOCTL:
/* Only ioctl that has flag set is for set reparse points */
smb_reconn_stats.fail_set_reparse_cnt += 1;
break;
case SMB2_SET_INFO:
smb_reconn_stats.fail_set_info_cnt += 1;
break;
default:
SMBERROR("Unknown non idempotent command 0x%x \n", rqp->sr_command);
break;
}
error = ENETDOWN;
smb_reconn_stats.fail_non_idempotent_cnt += 1;
}
}
else {
/*
* Have to check all request in cmpd chain.
* The typical compound request would be Create/SomeOp/Close
* We have to check for
* 1. Whether a middle request is non idempotent
* 2. If there is an unmatched Create/Close pair
* Note that Create and Close have the SMB2_NON_IDEMPOTENT flag
* set on them by default. Its only non idempotent if its an
* unmatched Create/Close pair.
*/
found_create = 0;
found_close = 0;
found_non_idempotent = 0;
tmp_rqp = rqp;
while (tmp_rqp != NULL) {
switch (tmp_rqp->sr_command) {
case SMB2_CREATE:
found_create = 1;
break;
case SMB2_CLOSE:
found_close = 1;
break;
case SMB2_LOCK:
found_non_idempotent = 1;
smb_reconn_stats.fail_lock_cnt += 1;
break;
case SMB2_IOCTL:
/* Only ioctl that has flag set is for set reparse points */
found_non_idempotent = 1;
smb_reconn_stats.fail_set_reparse_cnt += 1;
break;
case SMB2_SET_INFO:
found_non_idempotent = 1;
smb_reconn_stats.fail_set_info_cnt += 1;
break;
default:
/* Just ignore other commands */
break;
}
if (found_non_idempotent == 1) {
smb_reconn_stats.fail_non_idempotent_cnt += 1;
error = ENETDOWN;
break;
}
tmp_rqp = tmp_rqp->sr_next_rqp;
}
/*
* Did we find a matching Create/Close pair?
* IE either both Create and Close are present or
* neither Create or Close are present
*/
if (found_create != found_close) {
smb_reconn_stats.fail_cmpd_create_cnt += 1;
error = ENETDOWN;
}
}
SMBRQ_SUNLOCK(rqp);
if (error) {
/* If we found non idempotent req, then we are done */
break;
}
}
SMB_IOD_RQUNLOCK(iod);
return (error);
}
static __inline void
smb_iod_rqprocessed(struct smb_rq *rqp, int error, int flags)
{
SMBRQ_SLOCK(rqp);
rqp->sr_flags |= flags;
rqp->sr_lerror = error;
rqp->sr_rpgen++;
rqp->sr_state = SMBRQ_NOTIFIED;
if (rqp->sr_flags & SMBR_ASYNC) {
DBG_ASSERT(rqp->sr_callback);
rqp->sr_callback(rqp->sr_callback_args);
}
else {
wakeup(&rqp->sr_state);
/* <74202808> smb_iod_waitrq could be waiting for internal rqp */
if (rqp->sr_flags & SMBR_INTERNAL) {
wakeup(&rqp->sr_iod->iod_flags);
}
}
SMBRQ_SUNLOCK(rqp);
}
/*
* Gets called from smb_iod_dead, smb_iod_negotiate and smb_iod_ssnsetup. This routine
* should never get called while we are in reconnect state. This routine just flushes
* any old messages left after a connection went down.
*/
static void smb_iod_invrq(struct smbiod *iod)
{
struct smb_rq *rqp, *trqp;
/*
* Invalidate all outstanding requests for this connection
*/
SMB_IOD_RQLOCK(iod);
TAILQ_FOREACH_SAFE(rqp, &iod->iod_rqlist, sr_link, trqp) {
smb_iod_rqprocessed(rqp, ENOTCONN, SMBR_DEAD);
}
SMB_IOD_RQUNLOCK(iod);
}
static void
smb_iod_closetran(struct smbiod *iod)
{
if (iod->iod_tdata == NULL) {
return;
}
/*
* Need to halt the read thread before we free the tcp transport.
* Read thread may be blocked in a socket read, so do a disconnect to
* unblock it.
*/
/*
* Wait for read thread to exit
*/
SMB_IOD_FLAGSLOCK(iod);
iod->iod_flags |= SMBIOD_READ_THREAD_STOP;
SMB_TRAN_DISCONNECT(iod);
for (;;) {
if (!(iod->iod_flags & SMBIOD_READ_THREAD_RUNNING)) {
SMB_IOD_FLAGSUNLOCK(iod);
break;
}
/* Tell read thread to exit */
iod->iod_flags |= SMBIOD_READ_THREAD_STOP;
wakeup(&(iod->iod_flags));
msleep(iod, SMB_IOD_FLAGSLOCKPTR(iod), PWAIT,
"iod-wait-read-exit", 0);
}
/* This will free the tcp transport! */
SMB_TRAN_DONE(iod);
}
static void
smb_iod_dead(struct smbiod *iod)
{
struct smb_rq *rqp, *trqp;
iod->iod_state = SMBIOD_ST_DEAD;
smb_iod_closetran(iod);
smb_iod_invrq(iod);
SMB_IOD_RQLOCK(iod);
TAILQ_FOREACH_SAFE(rqp, &iod->iod_rqlist, sr_link, trqp) {
if (rqp->sr_share) {
lck_mtx_lock(&rqp->sr_share->ss_shlock);
if (rqp->sr_share->ss_dead)
rqp->sr_share->ss_dead(rqp->sr_share);
lck_mtx_unlock(&rqp->sr_share->ss_shlock);
}
}
SMB_IOD_RQUNLOCK(iod);
}
/*
* We lost the connection. Set the session flag saying we need to do a reconnect and
* tell all the shares we are starting reconnect. At this point all non reconnect messages
* should block until the reconnect process is completed. This routine is always excuted
* from the main thread.
*/
void smb_iod_start_reconnect(struct smbiod *iod)
{
struct smb_share *share, *tshare;
struct smb_rq *rqp, *trqp;
SMBERROR("id %d start reconnect.\n", iod->iod_id);
/* This should never happen, but for testing lets leave it in */
if (iod->iod_flags & SMBIOD_START_RECONNECT) {
SMBWARNING("Already in start reconnect with %s\n", iod->iod_session->session_srvname);
return; /* Nothing to do here we are already in start reconnect mode */
}
/*
* Needed for soft mount timeouts. It will get reset when reconnect
* actually starts.
*/
nanouptime(&iod->reconnectStartTime);
/*
* Only start a reconnect on an active sessions or when a reconnect failed because we
* went to sleep. If we are in the middle of a connection then mark the connection
* as dead and get out.
*/
switch (iod->iod_state) {
case SMBIOD_ST_SESSION_ACTIVE: /* SMB session established */
case SMBIOD_ST_RECONNECT_AGAIN: /* betweeen reconnect attempts; sleep happened. */
case SMBIOD_ST_RECONNECT: /* reconnect has been started */
break;
case SMBIOD_ST_NOTCONN: /* no connect request was made */
case SMBIOD_ST_CONNECT: /* a connect attempt is in progress */
case SMBIOD_ST_TRANACTIVE: /* TCP transport level is connected, but SMB session not up yet */
case SMBIOD_ST_NEGOACTIVE: /* completed negotiation */
case SMBIOD_ST_SSNSETUP: /* started (a) session setup */
case SMBIOD_ST_DEAD: /* connection broken, transport is down */
SMBDEBUG("id %d %s: iod->iod_state = %x iod->iod_flags = 0x%x\n",
iod->iod_id, iod->iod_session->session_srvname, iod->iod_state, iod->iod_flags);
if (!(iod->iod_flags & SMBIOD_RECONNECT)) {
smb_iod_dead(iod);
return;
}
break;
}
/* Set the flag saying we are starting reconnect. */
SMB_IOD_FLAGSLOCK(iod);
iod->iod_flags |= SMBIOD_START_RECONNECT;
SMB_IOD_FLAGSUNLOCK(iod);
/*
* If multichannel is being used and we are already in reconnect, then we
* already tried to fail over to another channel and failed. No need to try
* again.
*/
if ((iod->iod_session->session_flags & SMBV_MULTICHANNEL_ON) &&
!(iod->iod_flags & SMBIOD_RECONNECT)) {
if (iod->iod_flags & SMBIOD_ALTERNATE_CHANNEL) {
lck_mtx_lock(&iod->iod_session->failover_lock); // One failover at a time
/* if alternate channel, see if we can utilize other channels */
smb_iod_alt_ch_failover(iod);
lck_mtx_unlock(&iod->iod_session->failover_lock);
goto done;
}
else {
/*
* <71930272> no connection trial should be on air when trying to
* assign another alt-ch as main or when doing reconnect.
* smb2_mc_resume_trials should be invoked after new main assigned
* or reconnect completed.
*/
SMB_LOG_MC("id %d pause trials before reconnect \n", iod->iod_id);
smb2_mc_pause_trials(&iod->iod_session->session_interface_table);
while (smb2_mc_check_for_active_trials(&iod->iod_session->session_interface_table))
{
SMB_LOG_MC("id %d still have ongoing trials \n", iod->iod_id);
smb2_mc_abort_trials(&iod->iod_session->session_interface_table);
struct timespec sleeptime;
sleeptime.tv_sec = 1;
sleeptime.tv_nsec = 0;
msleep(&iod->iod_flags, 0, PWAIT, "start reconnect wait for ongiong trials",
&sleeptime);
}
SMB_LOG_MC("id %d no more ongoing trials \n", iod->iod_id);
/*
* Multichannel main iod
* Assign another alt-ch as main or, if none available, then start
* reconnect
*/
int can_proclaim_main = smb_iod_main_ch_failover(iod);
if (can_proclaim_main) {
SMB_LOG_MC("id %d smb_iod_main_ch_failover is complete.\n",
iod->iod_id);
/*
* <71930272> new main assigned, failover as alternate channel.
* smb_iod_alt_ch_failover will trigger new connection trials
* so we should resume mc trials before we call it.
*/
smb2_mc_resume_trials(&iod->iod_session->session_interface_table);
/*
* <73475707> Only take the failover_lock AFTER calling
* smb_iod_request(SMBIOD_EV_PROCLAIM_MAIN) in smb_iod_main_ch_failover
* otherwise you can deadlock
*/
lck_mtx_lock(&iod->iod_session->failover_lock);
smb_iod_alt_ch_failover(iod);
lck_mtx_unlock(&iod->iod_session->failover_lock);
goto done;
}
}
SMB_LOG_MC("id %d All channels are down.\n", iod->iod_id);
}
/*
* remove all lease request enqueued by the iod to make sure
* no more lease break ack will be inserted into the iod request queue
*/
smb_iod_lease_dequeue(iod);
/* Search through the request list and set them to the correct state */
SMB_IOD_RQLOCK(iod);
TAILQ_FOREACH_SAFE(rqp, &iod->iod_rqlist, sr_link, trqp) {
SMBRQ_SLOCK(rqp);
/* Clear any internal or async request out of the queue */
if (rqp->sr_flags & (SMBR_INTERNAL | SMBR_ASYNC)) {
/* pretend like it did not get sent to recover SMB 2/3 credits */
rqp->sr_extflags &= ~SMB2_REQ_SENT;
SMBRQ_SUNLOCK(rqp);
// error out rqp and wake the pending thread to clears them off iod_rqlist
if (rqp->sr_flags & SMBR_ASYNC) {
smb_iod_rqprocessed(rqp, ETIMEDOUT, 0);
}
else {
smb_iod_rqprocessed(rqp, ENOTCONN, SMBR_DEAD);
}
}
else {
/* Not INTERNAL or ASYNC */
/* If SMB 2/3 and soft mount, cancel all requests with ETIMEDOUT */
if ((rqp->sr_share) &&
(rqp->sr_extflags & SMB2_REQUEST) &&
(rqp->sr_share->ss_soft_timer) &&
(smb_iod_check_timeout(&iod->reconnectStartTime, rqp->sr_share->ss_soft_timer))) {
/*
* If reconnect has not finished for time longer than ss_soft_timer,
* and we're in soft-mount, then error out the rqp
* Pretend like it did not get sent to recover SMB 2/3 credits
*/
rqp->sr_extflags &= ~SMB2_REQ_SENT;
SMBRQ_SUNLOCK(rqp);
SMBDEBUG("id %u: Soft Mount timed out! cmd 0x%x message_id %lld \n", iod->iod_id,
(UInt32) rqp->sr_command, rqp->sr_messageid);
smb_iod_rqprocessed(rqp, ETIMEDOUT, 0);
}
else {
/*
* Let the upper layer know that this message was processed
* while we were in reconnect mode. If they receive an error
* they may want to handle this message differently.
*/
rqp->sr_flags |= SMBR_RECONNECTED;
/* If we have not received a reply set the state to reconnect */
if (rqp->sr_state != SMBRQ_NOTIFIED) {
rqp->sr_extflags &= ~SMB2_REQ_SENT; /* clear the SMB 2/3 sent flag */
rqp->sr_state = SMBRQ_RECONNECT; /* Wait for reconnect to complete */
rqp->sr_flags |= SMBR_REXMIT; /* Tell the upper layer this message was resent */
rqp->sr_lerror = 0; /* We are going to resend clear the error */
}
SMBRQ_SUNLOCK(rqp);
}
}
}
/* We are already in reconnect, so we are done */
if (iod->iod_flags & SMBIOD_RECONNECT) {
SMB_IOD_RQUNLOCK(iod);
goto done;
}
/* Set our flag saying we need to do a reconnect, but not until we finish the work in this routine. */
SMB_IOD_FLAGSLOCK(iod);
iod->iod_flags |= SMBIOD_RECONNECT;
SMB_IOD_FLAGSUNLOCK(iod);
SMB_IOD_RQUNLOCK(iod);
/*
* We have the session list locked so the shares can't be remove and they can't
* go away. If the share is not gone then mark that we are in reconnect mode.
*/
smb_session_lock(iod->iod_session);
SMBCO_FOREACH_SAFE(share, SESSION_TO_CP(iod->iod_session), tshare) {
lck_mtx_lock(&share->ss_stlock);
if (!(share->ss_flags & SMBO_GONE)) {
share->ss_flags |= SMBS_RECONNECTING;
}
lck_mtx_unlock(&(share)->ss_stlock);
}
smb_session_unlock(iod->iod_session);
done:
/* Ok now we can do the reconnect */
SMB_IOD_FLAGSLOCK(iod);
iod->iod_state = SMBIOD_ST_RECONNECT;
iod->iod_flags &= ~SMBIOD_START_RECONNECT;
SMB_IOD_FLAGSUNLOCK(iod);
}
/*
* smb_iod_main_ch_failover()
* The main-channel of a multichannel session has got disconnected.
* In case another channel is connected, declare it as main (return 1).
* Otherwise, start a reconnect procedure (return 0).
*/
static int smb_iod_main_ch_failover(struct smbiod *iod)
{
int error = 0;
struct smbiod *alt_iod = NULL;
int ret_val = 1, attempts = 10;
again:
/*
* Find an alternate channel
* Note: In future, maybe we should try to find a different iod every
* time we look for an alt_iod? Should be ok for now.
*/
error = smb_iod_get_non_main_iod(iod->iod_session, &alt_iod, __FUNCTION__, 0);
if (error || (!alt_iod)) {
SMB_LOG_MC("id %u: Main channel can't find a valid alt iod. Start reconnect \n",
iod->iod_id);
return(0);
}
/*
* <76538390> If the alternate channel is inactive, we need to change the
* iod and connection states to active.
*/
if (alt_iod->iod_flags & SMBIOD_INACTIVE_CHANNEL) {
smb_iod_active(alt_iod);
smb2_mc_inform_connection_active_state_change(
&alt_iod->iod_session->session_interface_table,
alt_iod->iod_conn_entry.con_entry,
true);
}
/* Note that we now have a iod_ref_cnt on alt_iod */
/*
* Transfer main parameters to selected iod.
* We found an alt iod that will be promoted to main, but the alt iod
* could currently be in shutdown and return an error. If so, then loop
* around and try to find another alt iod that we can promote. Only try
* finding another alt iod to use for "attempts" number of times. I'm
* assuming each failed iod will not be found again, so try 10 different
* iods.
*/
SMB_LOG_MC("id %d Main channel failing over to alt iod %d \n",
iod->iod_id, alt_iod->iod_id);
error = smb_iod_request(alt_iod, SMBIOD_EV_PROCLAIM_MAIN | SMBIOD_EV_SYNC,
iod);
if (error) {
SMB_LOG_MC("id %u: Failed (%d) to promote alt_iod: %d. Try again (attempts %d). \n",
iod->iod_id, alt_iod->iod_id, error, attempts);
smb_iod_rel(alt_iod, NULL, __FUNCTION__);
attempts -= 1;
if (attempts > 0) {
/* Try again and see if there is another iod that we can promote */
goto again;
}
SMB_LOG_MC("id %u: Failed to find an iod to promote after %d attempts. Start reconnect \n",
iod->iod_id, error);
ret_val = 0;
goto exit;
}
exit:
smb_iod_rel(alt_iod, NULL, __FUNCTION__);
return ret_val;
}
/*
* smb_iod_alt_ch_failover()
* An alternate channel has got disconnected. Transfer all outstanding requests to
* another channel and abort this iod.
*/
static void smb_iod_alt_ch_failover(struct smbiod *iod)
{
struct smb_rq *rqp, *trqp;
SMB_LOG_MC("id %d Transfer pending requests \n", iod->iod_id);
/* Mark iod with shutdown to prevent new inserts to the rqlist */
SMB_IOD_FLAGSLOCK(iod);
iod->iod_flags |= SMBIOD_SHUTDOWN;
SMB_IOD_FLAGSUNLOCK(iod);
/* Search through the request list and set them to the correct state */
SMB_IOD_RQLOCK(iod);
TAILQ_FOREACH_SAFE(rqp, &iod->iod_rqlist, sr_link, trqp) {
SMBRQ_SLOCK(rqp);
/* Clear any internal or async request out of the queue */
if (rqp->sr_flags & (SMBR_INTERNAL | SMBR_ASYNC)) {
/* pretend like it did not get sent to recover SMB 2/3 credits */
rqp->sr_extflags &= ~SMB2_REQ_SENT;
SMBRQ_SUNLOCK(rqp);
/*
* Error out rqp and wake the pending thread to clear them off
* iod_rqlist
*/
if (rqp->sr_flags & SMBR_ASYNC) {
SMB_LOG_MC("id %u set ETIMEDOUT messageid %llu cmd %u ref_cnt %u.\n",
iod->iod_id, rqp->sr_messageid, rqp->sr_command,
iod->iod_ref_cnt);
smb_iod_rqprocessed(rqp, ETIMEDOUT, 0);
}
else {
SMB_LOG_MC("id %u set ENOTCONN on messageid %llu cmd %u ref_cnt %u.\n",
iod->iod_id, rqp->sr_messageid, rqp->sr_command,
iod->iod_ref_cnt);
smb_iod_rqprocessed(rqp, ENOTCONN, SMBR_DEAD);
}
}
else {
/* Not INTERNAL or ASYNC */
rqp->sr_flags |= (SMBR_RECONNECTED | SMBR_ALT_CH_DISCON);
if (rqp->sr_state != SMBRQ_NOTIFIED) {
rqp->sr_extflags &= ~SMB2_REQ_SENT; /* clear the SMB 2/3 sent flag */
rqp->sr_state = SMBRQ_RECONNECT; /* Wait for reconnect to complete */
rqp->sr_flags |= SMBR_REXMIT; /* Tell the upper layer this message was resent */
rqp->sr_lerror = 0; /* We are going to resend clear the error */
SMB_LOG_MC("id %u reposting messageid %llu cmd %u ref_cnt %u.\n",
iod->iod_id, rqp->sr_messageid, rqp->sr_command,
iod->iod_ref_cnt);
SMBRQ_SUNLOCK(rqp);
smb_iod_rqprocessed(rqp, EAGAIN, 0);
}
else {
/*
* Request already has its reply and callback has been done,
* just let this request finish by itself.
*/
SMBRQ_SUNLOCK(rqp);
}
}
}
/* We are already in reconnect, so we are done */
if (iod->iod_flags & SMBIOD_RECONNECT) {
SMB_IOD_RQUNLOCK(iod);
goto done;
}
SMB_IOD_RQUNLOCK(iod);
/* Inform mc_support that the channel has been disconnected */
smb_iod_detach_con_entry(iod);
// Check if additional channels can be established.
smb_iod_establish_alt_ch(iod);
done:
return;
}
static int
smb_iod_negotiate(struct smbiod *iod, vfs_context_t user_context)
{
int error;
kern_return_t result;
thread_t thread;
SMBIODEBUG("iod id %d state %d\n", iod->iod_id, iod->iod_state);
switch(iod->iod_state) {
case SMBIOD_ST_TRANACTIVE:
case SMBIOD_ST_NEGOACTIVE:
case SMBIOD_ST_SSNSETUP:
SMBERROR("smb_iod_negotiate is invalid now, state=%d\n", iod->iod_state);
return EINVAL;
case SMBIOD_ST_SESSION_ACTIVE:
SMBERROR("smb_iod_negotiate called when connected\n");
return EISCONN;
case SMBIOD_ST_DEAD:
return ENOTCONN; /* XXX: last error code ? */
default:
break;
}
iod->iod_state = SMBIOD_ST_CONNECT;
error = SMB_TRAN_CREATE(iod);
if (error) {
goto errorOut;
}
SMBIODEBUG("tcreate\n");
/*
* Start up read thread if not already running
*/
SMB_IOD_FLAGSLOCK(iod);
if (!(iod->iod_flags & SMBIOD_READ_THREAD_RUNNING)) {
SMBIODEBUG("id %d: Starting read thread \n", iod->iod_id);
result = kernel_thread_start((thread_continue_t)smb_iod_read_thread,
iod, &thread);
if (result != KERN_SUCCESS) {
/* Should never happen */
SMB_IOD_FLAGSUNLOCK(iod);
SMBERROR("id %d: can't start read thread. result = %d\n", iod->iod_id, result);
error = ENOTCONN;
goto errorOut;
}
else {
thread_deallocate(thread);
}
/*
* <79580211> kernel_thread_start does not start smb_iod_read_thread immediately.
* It will push this thread into the thread queue and wait for kernel scheduling
* to start this thread. We must wait here until read thread will actually start
* and set the SMBIOD_READ_THREAD_RUNNING flag. o.w we will be exposed to a race
* where in error cases the iod thread will free resources that will be used after
* free by the read thread. We assume here that if kernel_thread_start returned
* KERN_SUCCESS, read thread must run at some point and set the flag to break this
* loop. Therefore we should not have a timeout and error out this loop as we will
* be exposed to the same race again.
*/
for (;;) {
if (iod->iod_flags & SMBIOD_READ_THREAD_RUNNING) {
SMB_IOD_FLAGSUNLOCK(iod);
break;
}
msleep(iod, SMB_IOD_FLAGSLOCKPTR(iod), PWAIT,
"iod-wait-read-start-running", 0);
}
}
else {
SMB_IOD_FLAGSUNLOCK(iod);
}
/* We only bind when doing a NetBIOS connection */
if (iod->iod_saddr->sa_family == AF_NETBIOS) {
error = SMB_TRAN_BIND(iod, iod->iod_laddr);
if (error) {
goto errorOut;
}
SMBIODEBUG("tbind\n");
}
error = SMB_TRAN_CONNECT(iod, iod->iod_saddr);
if (error == 0) {
iod->iod_state = SMBIOD_ST_TRANACTIVE;
SMBIODEBUG("tconnect\n");
SMB_IOD_FLAGSLOCK(iod);
iod->iod_flags &= ~SMBIOD_READ_THREAD_ERROR;
SMB_IOD_FLAGSUNLOCK(iod);
/* Wake up the read thread */
smb_iod_wakeup(iod);
error = smb_smb_negotiate(iod, user_context, FALSE, iod->iod_context);
}
if (error) {
goto errorOut;
}
iod->iod_state = SMBIOD_ST_NEGOACTIVE;
SMBIODEBUG("completed\n");
smb_iod_invrq(iod);
return 0;
errorOut:
smb_iod_dead(iod);
return error;
}
static int
smb_iod_ssnsetup(struct smbiod *iod, int inReconnect)
{
int error;
SMBIODEBUG("id %d, state %d\n", iod->iod_id, iod->iod_state);
switch(iod->iod_state) {
case SMBIOD_ST_NEGOACTIVE:
break;
case SMBIOD_ST_DEAD:
return ENOTCONN; /* XXX: last error code ? */
case SMBIOD_ST_SESSION_ACTIVE:
SMBERROR("smb_iod_ssnsetup called when connected\n");
return EISCONN;
default:
SMBERROR("smb_iod_ssnsetup is invalid now, state=%d\n",
iod->iod_state);
return EINVAL;
}
iod->iod_state = SMBIOD_ST_SSNSETUP;
error = smb_smb_ssnsetup(iod, inReconnect, iod->iod_context);
if (error) {
/*
* We no longer call smb_io_dead here, the session could still be
* alive. Allow for other attempt to authenticate on this same
* session. If the connect went down let the call process
* decide what to do with the session.
*
* Now all we do is reset the iod state back to what it was, but only if
* it hasn't change from the time we came in here. If the connection goes
* down(server dies) then we shouldn't change the state.
*/
if (iod->iod_state == SMBIOD_ST_SSNSETUP)
iod->iod_state = SMBIOD_ST_NEGOACTIVE;
} else {
iod->iod_state = SMBIOD_ST_SESSION_ACTIVE;
SMBIODEBUG("completed\n");
nanotime(&iod->iod_session_setup_time);
if (inReconnect)
{
iod->iod_session->session_reconnect_count++;
iod->iod_session->session_reconnect_time = iod->iod_session_setup_time;
} else {
iod->iod_session->session_setup_time = iod->iod_session_setup_time;
}
/* Don't flush the queue if we are in reconnect state. We need to resend those messages. */
if ((iod->iod_flags & SMBIOD_RECONNECT) != SMBIOD_RECONNECT)
smb_iod_invrq(iod);
}
return error;
}
static int
smb_iod_disconnect(struct smbiod *iod)
{
struct smb_session *sessionp = iod->iod_session;
SMBIODEBUG("id %u\n", iod->iod_id);
if (iod->iod_state == SMBIOD_ST_SESSION_ACTIVE) {
smb_smb_ssnclose(sessionp, iod->iod_context);
/*
* Instead of going to SMBIOD_ST_TRANACTIVE, set SMBIOD_ST_NOTCONN
* as we are going to disconnect now anyways. This keeps the read
* thread from running inbetween now and setting SMBIOD_ST_NOTCONN
*/
iod->iod_state = SMBIOD_ST_NOTCONN;
}
sessionp->session_smbuid = SMB_UID_UNKNOWN;
smb_iod_closetran(iod);
iod->iod_state = SMBIOD_ST_NOTCONN;
return 0;
}
static int
smb_iod_sendrq(struct smbiod *iod, struct smb_rq *rqp)
{
struct smb_session *sessionp = iod->iod_session;
mbuf_t m, m2;
int error = 0;
struct smb_rq *tmp_rqp;
struct mbchain *mbp;
size_t len = 0;
SMBIODEBUG("id %d iod_state = %d\n", iod->iod_id, iod->iod_state);
switch (iod->iod_state) {
case SMBIOD_ST_NOTCONN:
smb_iod_rqprocessed(rqp, ENOTCONN, 0);
return 0;
case SMBIOD_ST_DEAD:
/* This is what keeps the iod itself from sending more */
smb_iod_rqprocessed(rqp, ENOTCONN, 0);
return 0;
case SMBIOD_ST_CONNECT:
return 0;
case SMBIOD_ST_NEGOACTIVE:
SMBERROR("smb_iod_sendrq in unexpected state(%d)\n",
iod->iod_state);
default:
break;
}
if (rqp->sr_extflags & SMB2_REQUEST) {
/* filled in by smb2_rq_init_internal */
smb_rq_getrequest(rqp, &mbp);
len = mb_fixhdr(mbp);
if (rqp->sr_flags & SMBR_COMPOUND_RQ) {
/*
* Compound request to send. The first rqp has its sr_next_rq set to
* point to the next request to send and so on. The last request will
* have sr_next_rq set to NULL. The next_command fields should already
* be filled in with correct offsets. Have to copy all the requests
* into a single mbuf chain before sending it.
*
* ONLY the first rqp in the chain will have its sr_ fields updated.
*/
DBG_ASSERT(rqp->sr_next_rqp != NULL);
#if 0
/* Message ID and credit checking debugging code */
if (rqp->sr_creditcharge != letohs(*rqp->sr_creditchargep)) {
SMBERROR("MID: cmpd Credit charge mismatch %d != %d \n",
rqp->sr_creditcharge,
letohs(rqp->sr_creditchargep));
return ENOTCONN;
}
if (rqp->sr_messageid != letohq(*rqp->sr_messageidp)) {
SMBERROR("MID: cmpd mid mismatch %llu != %llu \n",
rqp->sr_messageid,
letohq(rqp->sr_messageidp));
return ENOTCONN;
}
lck_mtx_lock(&sessionp->session_mid_lock);
if (sessionp->session_expected_mid != rqp->sr_messageid) {
SMBERROR("MID: cmpd not expected mid %llu != %llu cmd %d \n",
sessionp->session_expected_mid, rqp->sr_messageid,
rqp->sr_command);
SMBERROR("MID: cmpd last mid %llu, charge %d, cmd %d \n",
sessionp->session_last_sent_mid,
sessionp->session_last_credit_charge,
sessionp->session_last_command);
return ENOTCONN;
}
else {
/* Set the next expected mid */
sessionp->session_last_sent_mid = rqp->sr_messageid;
sessionp->session_last_credit_charge = rqp->sr_creditcharge;
sessionp->session_last_command = rqp->sr_command;