1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/fs.h>
22#include <linux/net.h>
23#include <linux/string.h>
24#include <linux/sched/mm.h>
25#include <linux/sched/signal.h>
26#include <linux/list.h>
27#include <linux/wait.h>
28#include <linux/slab.h>
29#include <linux/pagemap.h>
30#include <linux/ctype.h>
31#include <linux/utsname.h>
32#include <linux/mempool.h>
33#include <linux/delay.h>
34#include <linux/completion.h>
35#include <linux/kthread.h>
36#include <linux/pagevec.h>
37#include <linux/freezer.h>
38#include <linux/namei.h>
39#include <linux/uuid.h>
40#include <linux/uaccess.h>
41#include <asm/processor.h>
42#include <linux/inet.h>
43#include <linux/module.h>
44#include <keys/user-type.h>
45#include <net/ipv6.h>
46#include <linux/bvec.h>
47#include "cifspdu.h"
48#include "cifsglob.h"
49#include "cifsproto.h"
50#include "cifs_unicode.h"
51#include "cifs_debug.h"
52#include "cifs_fs_sb.h"
53#include "ntlmssp.h"
54#include "nterr.h"
55#include "rfc1002pdu.h"
56#include "fscache.h"
57#include "smb2proto.h"
58#include "smbdirect.h"
59#include "dns_resolve.h"
60#ifdef CONFIG_CIFS_DFS_UPCALL
61#include "dfs_cache.h"
62#endif
63#include "fs_context.h"
64#ifdef CONFIG_CIFS_SWN_UPCALL
65#include "cifs_swn.h"
66#endif
67
68extern mempool_t *cifs_req_poolp;
69extern bool disable_legacy_dialects;
70
71
72#define TLINK_ERROR_EXPIRE (1 * HZ)
73#define TLINK_IDLE_EXPIRE (600 * HZ)
74
75
76#define NUM_STATUS_IO_TIMEOUT 5
77
78static int ip_connect(struct TCP_Server_Info *server);
79static int generic_ip_connect(struct TCP_Server_Info *server);
80static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
81static void cifs_prune_tlinks(struct work_struct *work);
82
83
84
85
86
87
88
89static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server)
90{
91 int rc;
92 int len;
93 char *unc, *ipaddr = NULL;
94
95 if (!server->hostname)
96 return -EINVAL;
97
98 len = strlen(server->hostname) + 3;
99
100 unc = kmalloc(len, GFP_KERNEL);
101 if (!unc) {
102 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
103 return -ENOMEM;
104 }
105 scnprintf(unc, len, "\\\\%s", server->hostname);
106
107 rc = dns_resolve_server_name_to_ip(unc, &ipaddr);
108 kfree(unc);
109
110 if (rc < 0) {
111 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
112 __func__, server->hostname, rc);
113 return rc;
114 }
115
116 spin_lock(&cifs_tcp_ses_lock);
117 rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
118 strlen(ipaddr));
119 spin_unlock(&cifs_tcp_ses_lock);
120 kfree(ipaddr);
121
122 return !rc ? -1 : 0;
123}
124
125#ifdef CONFIG_CIFS_DFS_UPCALL
126
127static void reconn_set_next_dfs_target(struct TCP_Server_Info *server,
128 struct cifs_sb_info *cifs_sb,
129 struct dfs_cache_tgt_list *tgt_list,
130 struct dfs_cache_tgt_iterator **tgt_it)
131{
132 const char *name;
133 int rc;
134
135 if (!cifs_sb || !cifs_sb->origin_fullpath)
136 return;
137
138 if (!*tgt_it) {
139 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
140 } else {
141 *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
142 if (!*tgt_it)
143 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
144 }
145
146 cifs_dbg(FYI, "%s: UNC: %s\n", __func__, cifs_sb->origin_fullpath);
147
148 name = dfs_cache_get_tgt_name(*tgt_it);
149
150 kfree(server->hostname);
151
152 server->hostname = extract_hostname(name);
153 if (IS_ERR(server->hostname)) {
154 cifs_dbg(FYI,
155 "%s: failed to extract hostname from target: %ld\n",
156 __func__, PTR_ERR(server->hostname));
157 }
158
159 rc = reconn_set_ipaddr_from_hostname(server);
160 if (rc) {
161 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
162 __func__, rc);
163 }
164}
165
166static inline int reconn_setup_dfs_targets(struct cifs_sb_info *cifs_sb,
167 struct dfs_cache_tgt_list *tl)
168{
169 if (!cifs_sb->origin_fullpath)
170 return -EOPNOTSUPP;
171 return dfs_cache_noreq_find(cifs_sb->origin_fullpath + 1, NULL, tl);
172}
173#endif
174
175
176
177
178
179
180
181
182
183int
184cifs_reconnect(struct TCP_Server_Info *server)
185{
186 int rc = 0;
187 struct list_head *tmp, *tmp2;
188 struct cifs_ses *ses;
189 struct cifs_tcon *tcon;
190 struct mid_q_entry *mid_entry;
191 struct list_head retry_list;
192#ifdef CONFIG_CIFS_DFS_UPCALL
193 struct super_block *sb = NULL;
194 struct cifs_sb_info *cifs_sb = NULL;
195 struct dfs_cache_tgt_list tgt_list = {0};
196 struct dfs_cache_tgt_iterator *tgt_it = NULL;
197#endif
198
199 spin_lock(&GlobalMid_Lock);
200 server->nr_targets = 1;
201#ifdef CONFIG_CIFS_DFS_UPCALL
202 spin_unlock(&GlobalMid_Lock);
203 sb = cifs_get_tcp_super(server);
204 if (IS_ERR(sb)) {
205 rc = PTR_ERR(sb);
206 cifs_dbg(FYI, "%s: will not do DFS failover: rc = %d\n",
207 __func__, rc);
208 sb = NULL;
209 } else {
210 cifs_sb = CIFS_SB(sb);
211 rc = reconn_setup_dfs_targets(cifs_sb, &tgt_list);
212 if (rc) {
213 cifs_sb = NULL;
214 if (rc != -EOPNOTSUPP) {
215 cifs_server_dbg(VFS, "%s: no target servers for DFS failover\n",
216 __func__);
217 }
218 } else {
219 server->nr_targets = dfs_cache_get_nr_tgts(&tgt_list);
220 }
221 }
222 cifs_dbg(FYI, "%s: will retry %d target(s)\n", __func__,
223 server->nr_targets);
224 spin_lock(&GlobalMid_Lock);
225#endif
226 if (server->tcpStatus == CifsExiting) {
227
228
229 spin_unlock(&GlobalMid_Lock);
230#ifdef CONFIG_CIFS_DFS_UPCALL
231 dfs_cache_free_tgts(&tgt_list);
232 cifs_put_tcp_super(sb);
233#endif
234 wake_up(&server->response_q);
235 return rc;
236 } else
237 server->tcpStatus = CifsNeedReconnect;
238 spin_unlock(&GlobalMid_Lock);
239 server->maxBuf = 0;
240 server->max_read = 0;
241
242 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
243 trace_smb3_reconnect(server->CurrentMid, server->hostname);
244
245
246
247 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
248 __func__);
249 spin_lock(&cifs_tcp_ses_lock);
250 list_for_each(tmp, &server->smb_ses_list) {
251 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
252 ses->need_reconnect = true;
253 list_for_each(tmp2, &ses->tcon_list) {
254 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
255 tcon->need_reconnect = true;
256 }
257 if (ses->tcon_ipc)
258 ses->tcon_ipc->need_reconnect = true;
259 }
260 spin_unlock(&cifs_tcp_ses_lock);
261
262
263 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
264 mutex_lock(&server->srv_mutex);
265 if (server->ssocket) {
266 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
267 server->ssocket->state, server->ssocket->flags);
268 kernel_sock_shutdown(server->ssocket, SHUT_WR);
269 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
270 server->ssocket->state, server->ssocket->flags);
271 sock_release(server->ssocket);
272 server->ssocket = NULL;
273 }
274 server->sequence_number = 0;
275 server->session_estab = false;
276 kfree(server->session_key.response);
277 server->session_key.response = NULL;
278 server->session_key.len = 0;
279 server->lstrp = jiffies;
280
281
282 INIT_LIST_HEAD(&retry_list);
283 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
284 spin_lock(&GlobalMid_Lock);
285 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
286 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
287 kref_get(&mid_entry->refcount);
288 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
289 mid_entry->mid_state = MID_RETRY_NEEDED;
290 list_move(&mid_entry->qhead, &retry_list);
291 mid_entry->mid_flags |= MID_DELETED;
292 }
293 spin_unlock(&GlobalMid_Lock);
294 mutex_unlock(&server->srv_mutex);
295
296 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
297 list_for_each_safe(tmp, tmp2, &retry_list) {
298 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
299 list_del_init(&mid_entry->qhead);
300 mid_entry->callback(mid_entry);
301 cifs_mid_q_entry_release(mid_entry);
302 }
303
304 if (cifs_rdma_enabled(server)) {
305 mutex_lock(&server->srv_mutex);
306 smbd_destroy(server);
307 mutex_unlock(&server->srv_mutex);
308 }
309
310 do {
311 try_to_freeze();
312
313 mutex_lock(&server->srv_mutex);
314
315#ifdef CONFIG_CIFS_SWN_UPCALL
316 if (server->use_swn_dstaddr) {
317 server->dstaddr = server->swn_dstaddr;
318 } else {
319#endif
320
321#ifdef CONFIG_CIFS_DFS_UPCALL
322 if (cifs_sb && cifs_sb->origin_fullpath)
323
324
325
326
327
328 reconn_set_next_dfs_target(server, cifs_sb, &tgt_list, &tgt_it);
329 else {
330#endif
331
332
333
334 rc = reconn_set_ipaddr_from_hostname(server);
335 if (rc) {
336 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
337 __func__, rc);
338 }
339
340#ifdef CONFIG_CIFS_DFS_UPCALL
341 }
342#endif
343
344
345#ifdef CONFIG_CIFS_SWN_UPCALL
346 }
347#endif
348
349 if (cifs_rdma_enabled(server))
350 rc = smbd_reconnect(server);
351 else
352 rc = generic_ip_connect(server);
353 if (rc) {
354 cifs_dbg(FYI, "reconnect error %d\n", rc);
355 mutex_unlock(&server->srv_mutex);
356 msleep(3000);
357 } else {
358 atomic_inc(&tcpSesReconnectCount);
359 set_credits(server, 1);
360 spin_lock(&GlobalMid_Lock);
361 if (server->tcpStatus != CifsExiting)
362 server->tcpStatus = CifsNeedNegotiate;
363 spin_unlock(&GlobalMid_Lock);
364#ifdef CONFIG_CIFS_SWN_UPCALL
365 server->use_swn_dstaddr = false;
366#endif
367 mutex_unlock(&server->srv_mutex);
368 }
369 } while (server->tcpStatus == CifsNeedReconnect);
370
371#ifdef CONFIG_CIFS_DFS_UPCALL
372 if (tgt_it) {
373 rc = dfs_cache_noreq_update_tgthint(cifs_sb->origin_fullpath + 1,
374 tgt_it);
375 if (rc) {
376 cifs_server_dbg(VFS, "%s: failed to update DFS target hint: rc = %d\n",
377 __func__, rc);
378 }
379 dfs_cache_free_tgts(&tgt_list);
380 }
381
382 cifs_put_tcp_super(sb);
383#endif
384 if (server->tcpStatus == CifsNeedNegotiate)
385 mod_delayed_work(cifsiod_wq, &server->echo, 0);
386
387 wake_up(&server->response_q);
388 return rc;
389}
390
391static void
392cifs_echo_request(struct work_struct *work)
393{
394 int rc;
395 struct TCP_Server_Info *server = container_of(work,
396 struct TCP_Server_Info, echo.work);
397 unsigned long echo_interval;
398
399
400
401
402
403 if (server->tcpStatus == CifsNeedNegotiate)
404 echo_interval = 0;
405 else
406 echo_interval = server->echo_interval;
407
408
409
410
411
412
413 if (server->tcpStatus == CifsNeedReconnect ||
414 server->tcpStatus == CifsExiting ||
415 server->tcpStatus == CifsNew ||
416 (server->ops->can_echo && !server->ops->can_echo(server)) ||
417 time_before(jiffies, server->lstrp + echo_interval - HZ))
418 goto requeue_echo;
419
420 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
421 if (rc)
422 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
423 server->hostname);
424
425#ifdef CONFIG_CIFS_SWN_UPCALL
426
427 cifs_swn_check();
428#endif
429
430requeue_echo:
431 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
432}
433
434static bool
435allocate_buffers(struct TCP_Server_Info *server)
436{
437 if (!server->bigbuf) {
438 server->bigbuf = (char *)cifs_buf_get();
439 if (!server->bigbuf) {
440 cifs_server_dbg(VFS, "No memory for large SMB response\n");
441 msleep(3000);
442
443 return false;
444 }
445 } else if (server->large_buf) {
446
447 memset(server->bigbuf, 0, HEADER_SIZE(server));
448 }
449
450 if (!server->smallbuf) {
451 server->smallbuf = (char *)cifs_small_buf_get();
452 if (!server->smallbuf) {
453 cifs_server_dbg(VFS, "No memory for SMB response\n");
454 msleep(1000);
455
456 return false;
457 }
458
459 } else {
460
461 memset(server->smallbuf, 0, HEADER_SIZE(server));
462 }
463
464 return true;
465}
466
467static bool
468server_unresponsive(struct TCP_Server_Info *server)
469{
470
471
472
473
474
475
476
477
478
479
480
481 if ((server->tcpStatus == CifsGood ||
482 server->tcpStatus == CifsNeedNegotiate) &&
483 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
484 cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
485 (3 * server->echo_interval) / HZ);
486 cifs_reconnect(server);
487 return true;
488 }
489
490 return false;
491}
492
493static inline bool
494zero_credits(struct TCP_Server_Info *server)
495{
496 int val;
497
498 spin_lock(&server->req_lock);
499 val = server->credits + server->echo_credits + server->oplock_credits;
500 if (server->in_flight == 0 && val == 0) {
501 spin_unlock(&server->req_lock);
502 return true;
503 }
504 spin_unlock(&server->req_lock);
505 return false;
506}
507
508static int
509cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
510{
511 int length = 0;
512 int total_read;
513
514 smb_msg->msg_control = NULL;
515 smb_msg->msg_controllen = 0;
516
517 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
518 try_to_freeze();
519
520
521 if (zero_credits(server)) {
522 cifs_reconnect(server);
523 return -ECONNABORTED;
524 }
525
526 if (server_unresponsive(server))
527 return -ECONNABORTED;
528 if (cifs_rdma_enabled(server) && server->smbd_conn)
529 length = smbd_recv(server->smbd_conn, smb_msg);
530 else
531 length = sock_recvmsg(server->ssocket, smb_msg, 0);
532
533 if (server->tcpStatus == CifsExiting)
534 return -ESHUTDOWN;
535
536 if (server->tcpStatus == CifsNeedReconnect) {
537 cifs_reconnect(server);
538 return -ECONNABORTED;
539 }
540
541 if (length == -ERESTARTSYS ||
542 length == -EAGAIN ||
543 length == -EINTR) {
544
545
546
547
548
549 usleep_range(1000, 2000);
550 length = 0;
551 continue;
552 }
553
554 if (length <= 0) {
555 cifs_dbg(FYI, "Received no data or error: %d\n", length);
556 cifs_reconnect(server);
557 return -ECONNABORTED;
558 }
559 }
560 return total_read;
561}
562
563int
564cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
565 unsigned int to_read)
566{
567 struct msghdr smb_msg;
568 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
569 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
570
571 return cifs_readv_from_socket(server, &smb_msg);
572}
573
574int
575cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
576 unsigned int page_offset, unsigned int to_read)
577{
578 struct msghdr smb_msg;
579 struct bio_vec bv = {
580 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
581 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
582 return cifs_readv_from_socket(server, &smb_msg);
583}
584
585static bool
586is_smb_response(struct TCP_Server_Info *server, unsigned char type)
587{
588
589
590
591
592
593 switch (type) {
594 case RFC1002_SESSION_MESSAGE:
595
596 return true;
597 case RFC1002_SESSION_KEEP_ALIVE:
598 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
599 break;
600 case RFC1002_POSITIVE_SESSION_RESPONSE:
601 cifs_dbg(FYI, "RFC 1002 positive session response\n");
602 break;
603 case RFC1002_NEGATIVE_SESSION_RESPONSE:
604
605
606
607
608 cifs_dbg(FYI, "RFC 1002 negative session response\n");
609
610 msleep(1000);
611
612
613
614
615
616
617 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
618 cifs_reconnect(server);
619 break;
620 default:
621 cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
622 cifs_reconnect(server);
623 }
624
625 return false;
626}
627
628void
629dequeue_mid(struct mid_q_entry *mid, bool malformed)
630{
631#ifdef CONFIG_CIFS_STATS2
632 mid->when_received = jiffies;
633#endif
634 spin_lock(&GlobalMid_Lock);
635 if (!malformed)
636 mid->mid_state = MID_RESPONSE_RECEIVED;
637 else
638 mid->mid_state = MID_RESPONSE_MALFORMED;
639
640
641
642
643 if (mid->mid_flags & MID_DELETED)
644 pr_warn_once("trying to dequeue a deleted mid\n");
645 else {
646 list_del_init(&mid->qhead);
647 mid->mid_flags |= MID_DELETED;
648 }
649 spin_unlock(&GlobalMid_Lock);
650}
651
652static unsigned int
653smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
654{
655 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buffer;
656
657
658
659
660 if (server->vals->header_preamble_size)
661 return 0;
662
663 return le16_to_cpu(shdr->CreditRequest);
664}
665
666static void
667handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
668 char *buf, int malformed)
669{
670 if (server->ops->check_trans2 &&
671 server->ops->check_trans2(mid, server, buf, malformed))
672 return;
673 mid->credits_received = smb2_get_credits_from_hdr(buf, server);
674 mid->resp_buf = buf;
675 mid->large_buf = server->large_buf;
676
677 if (!mid->multiRsp) {
678
679 if (server->large_buf)
680 server->bigbuf = NULL;
681 else
682 server->smallbuf = NULL;
683 }
684 dequeue_mid(mid, malformed);
685}
686
687static void clean_demultiplex_info(struct TCP_Server_Info *server)
688{
689 int length;
690
691
692 spin_lock(&cifs_tcp_ses_lock);
693 list_del_init(&server->tcp_ses_list);
694 spin_unlock(&cifs_tcp_ses_lock);
695
696 cancel_delayed_work_sync(&server->echo);
697
698 spin_lock(&GlobalMid_Lock);
699 server->tcpStatus = CifsExiting;
700 spin_unlock(&GlobalMid_Lock);
701 wake_up_all(&server->response_q);
702
703
704 spin_lock(&server->req_lock);
705 if (server->credits <= 0)
706 server->credits = 1;
707 spin_unlock(&server->req_lock);
708
709
710
711
712
713
714
715 wake_up_all(&server->request_q);
716
717 msleep(125);
718 if (cifs_rdma_enabled(server))
719 smbd_destroy(server);
720 if (server->ssocket) {
721 sock_release(server->ssocket);
722 server->ssocket = NULL;
723 }
724
725 if (!list_empty(&server->pending_mid_q)) {
726 struct list_head dispose_list;
727 struct mid_q_entry *mid_entry;
728 struct list_head *tmp, *tmp2;
729
730 INIT_LIST_HEAD(&dispose_list);
731 spin_lock(&GlobalMid_Lock);
732 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
733 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
734 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
735 kref_get(&mid_entry->refcount);
736 mid_entry->mid_state = MID_SHUTDOWN;
737 list_move(&mid_entry->qhead, &dispose_list);
738 mid_entry->mid_flags |= MID_DELETED;
739 }
740 spin_unlock(&GlobalMid_Lock);
741
742
743 list_for_each_safe(tmp, tmp2, &dispose_list) {
744 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
745 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
746 list_del_init(&mid_entry->qhead);
747 mid_entry->callback(mid_entry);
748 cifs_mid_q_entry_release(mid_entry);
749 }
750
751 msleep(125);
752 }
753
754 if (!list_empty(&server->pending_mid_q)) {
755
756
757
758
759
760
761
762
763 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
764 msleep(46000);
765
766
767
768
769 }
770
771 kfree(server);
772
773 length = atomic_dec_return(&tcpSesAllocCount);
774 if (length > 0)
775 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
776}
777
778static int
779standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
780{
781 int length;
782 char *buf = server->smallbuf;
783 unsigned int pdu_length = server->pdu_size;
784
785
786 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
787 server->vals->header_preamble_size) {
788 cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
789 cifs_reconnect(server);
790 return -ECONNABORTED;
791 }
792
793
794 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
795 server->large_buf = true;
796 memcpy(server->bigbuf, buf, server->total_read);
797 buf = server->bigbuf;
798 }
799
800
801 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
802 pdu_length - HEADER_SIZE(server) + 1
803 + server->vals->header_preamble_size);
804
805 if (length < 0)
806 return length;
807 server->total_read += length;
808
809 dump_smb(buf, server->total_read);
810
811 return cifs_handle_standard(server, mid);
812}
813
814int
815cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
816{
817 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
818 int length;
819
820
821
822
823
824
825
826
827
828
829 length = server->ops->check_message(buf, server->total_read, server);
830 if (length != 0)
831 cifs_dump_mem("Bad SMB: ", buf,
832 min_t(unsigned int, server->total_read, 48));
833
834 if (server->ops->is_session_expired &&
835 server->ops->is_session_expired(buf)) {
836 cifs_reconnect(server);
837 return -1;
838 }
839
840 if (server->ops->is_status_pending &&
841 server->ops->is_status_pending(buf, server))
842 return -1;
843
844 if (!mid)
845 return length;
846
847 handle_mid(mid, server, buf, length);
848 return 0;
849}
850
851static void
852smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
853{
854 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buffer;
855
856
857
858
859 if (server->vals->header_preamble_size)
860 return;
861
862 if (shdr->CreditRequest) {
863 spin_lock(&server->req_lock);
864 server->credits += le16_to_cpu(shdr->CreditRequest);
865 spin_unlock(&server->req_lock);
866 wake_up(&server->request_q);
867 }
868}
869
870
871static int
872cifs_demultiplex_thread(void *p)
873{
874 int i, num_mids, length;
875 struct TCP_Server_Info *server = p;
876 unsigned int pdu_length;
877 unsigned int next_offset;
878 char *buf = NULL;
879 struct task_struct *task_to_wake = NULL;
880 struct mid_q_entry *mids[MAX_COMPOUND];
881 char *bufs[MAX_COMPOUND];
882 unsigned int noreclaim_flag, num_io_timeout = 0;
883
884 noreclaim_flag = memalloc_noreclaim_save();
885 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
886
887 length = atomic_inc_return(&tcpSesAllocCount);
888 if (length > 1)
889 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
890
891 set_freezable();
892 allow_kernel_signal(SIGKILL);
893 while (server->tcpStatus != CifsExiting) {
894 if (try_to_freeze())
895 continue;
896
897 if (!allocate_buffers(server))
898 continue;
899
900 server->large_buf = false;
901 buf = server->smallbuf;
902 pdu_length = 4;
903
904 length = cifs_read_from_socket(server, buf, pdu_length);
905 if (length < 0)
906 continue;
907
908 if (server->vals->header_preamble_size == 0)
909 server->total_read = 0;
910 else
911 server->total_read = length;
912
913
914
915
916
917 pdu_length = get_rfc1002_length(buf);
918
919 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
920 if (!is_smb_response(server, buf[0]))
921 continue;
922next_pdu:
923 server->pdu_size = pdu_length;
924
925
926 if (server->pdu_size < HEADER_SIZE(server) - 1 -
927 server->vals->header_preamble_size) {
928 cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
929 server->pdu_size);
930 cifs_reconnect(server);
931 continue;
932 }
933
934
935 length = cifs_read_from_socket(server,
936 buf + server->vals->header_preamble_size,
937 HEADER_SIZE(server) - 1
938 - server->vals->header_preamble_size);
939 if (length < 0)
940 continue;
941 server->total_read += length;
942
943 if (server->ops->next_header) {
944 next_offset = server->ops->next_header(buf);
945 if (next_offset)
946 server->pdu_size = next_offset;
947 }
948
949 memset(mids, 0, sizeof(mids));
950 memset(bufs, 0, sizeof(bufs));
951 num_mids = 0;
952
953 if (server->ops->is_transform_hdr &&
954 server->ops->receive_transform &&
955 server->ops->is_transform_hdr(buf)) {
956 length = server->ops->receive_transform(server,
957 mids,
958 bufs,
959 &num_mids);
960 } else {
961 mids[0] = server->ops->find_mid(server, buf);
962 bufs[0] = buf;
963 num_mids = 1;
964
965 if (!mids[0] || !mids[0]->receive)
966 length = standard_receive3(server, mids[0]);
967 else
968 length = mids[0]->receive(server, mids[0]);
969 }
970
971 if (length < 0) {
972 for (i = 0; i < num_mids; i++)
973 if (mids[i])
974 cifs_mid_q_entry_release(mids[i]);
975 continue;
976 }
977
978 if (server->ops->is_status_io_timeout &&
979 server->ops->is_status_io_timeout(buf)) {
980 num_io_timeout++;
981 if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) {
982 cifs_reconnect(server);
983 num_io_timeout = 0;
984 continue;
985 }
986 }
987
988 server->lstrp = jiffies;
989
990 for (i = 0; i < num_mids; i++) {
991 if (mids[i] != NULL) {
992 mids[i]->resp_buf_size = server->pdu_size;
993
994 if (!mids[i]->multiRsp || mids[i]->multiEnd)
995 mids[i]->callback(mids[i]);
996
997 cifs_mid_q_entry_release(mids[i]);
998 } else if (server->ops->is_oplock_break &&
999 server->ops->is_oplock_break(bufs[i],
1000 server)) {
1001 smb2_add_credits_from_hdr(bufs[i], server);
1002 cifs_dbg(FYI, "Received oplock break\n");
1003 } else {
1004 cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
1005 atomic_read(&midCount));
1006 cifs_dump_mem("Received Data is: ", bufs[i],
1007 HEADER_SIZE(server));
1008 smb2_add_credits_from_hdr(bufs[i], server);
1009#ifdef CONFIG_CIFS_DEBUG2
1010 if (server->ops->dump_detail)
1011 server->ops->dump_detail(bufs[i],
1012 server);
1013 cifs_dump_mids(server);
1014#endif
1015 }
1016 }
1017
1018 if (pdu_length > server->pdu_size) {
1019 if (!allocate_buffers(server))
1020 continue;
1021 pdu_length -= server->pdu_size;
1022 server->total_read = 0;
1023 server->large_buf = false;
1024 buf = server->smallbuf;
1025 goto next_pdu;
1026 }
1027 }
1028
1029
1030 cifs_buf_release(server->bigbuf);
1031 if (server->smallbuf)
1032 cifs_small_buf_release(server->smallbuf);
1033
1034 task_to_wake = xchg(&server->tsk, NULL);
1035 clean_demultiplex_info(server);
1036
1037
1038 if (!task_to_wake) {
1039 set_current_state(TASK_INTERRUPTIBLE);
1040 while (!signal_pending(current)) {
1041 schedule();
1042 set_current_state(TASK_INTERRUPTIBLE);
1043 }
1044 set_current_state(TASK_RUNNING);
1045 }
1046
1047 memalloc_noreclaim_restore(noreclaim_flag);
1048 module_put_and_exit(0);
1049}
1050
1051
1052
1053
1054
1055bool
1056cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
1057{
1058 switch (srcaddr->sa_family) {
1059 case AF_UNSPEC:
1060 return (rhs->sa_family == AF_UNSPEC);
1061 case AF_INET: {
1062 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1063 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1064 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1065 }
1066 case AF_INET6: {
1067 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
1068 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
1069 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
1070 }
1071 default:
1072 WARN_ON(1);
1073 return false;
1074 }
1075}
1076
1077
1078
1079
1080
1081
1082static bool
1083match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
1084{
1085 __be16 port, *sport;
1086
1087
1088 if (server->rdma)
1089 return true;
1090
1091 switch (addr->sa_family) {
1092 case AF_INET:
1093 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
1094 port = ((struct sockaddr_in *) addr)->sin_port;
1095 break;
1096 case AF_INET6:
1097 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
1098 port = ((struct sockaddr_in6 *) addr)->sin6_port;
1099 break;
1100 default:
1101 WARN_ON(1);
1102 return false;
1103 }
1104
1105 if (!port) {
1106 port = htons(CIFS_PORT);
1107 if (port == *sport)
1108 return true;
1109
1110 port = htons(RFC1001_PORT);
1111 }
1112
1113 return port == *sport;
1114}
1115
1116static bool
1117match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
1118 struct sockaddr *srcaddr)
1119{
1120 switch (addr->sa_family) {
1121 case AF_INET: {
1122 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
1123 struct sockaddr_in *srv_addr4 =
1124 (struct sockaddr_in *)&server->dstaddr;
1125
1126 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
1127 return false;
1128 break;
1129 }
1130 case AF_INET6: {
1131 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
1132 struct sockaddr_in6 *srv_addr6 =
1133 (struct sockaddr_in6 *)&server->dstaddr;
1134
1135 if (!ipv6_addr_equal(&addr6->sin6_addr,
1136 &srv_addr6->sin6_addr))
1137 return false;
1138 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
1139 return false;
1140 break;
1141 }
1142 default:
1143 WARN_ON(1);
1144 return false;
1145 }
1146
1147 if (!cifs_match_ipaddr(srcaddr, (struct sockaddr *)&server->srcaddr))
1148 return false;
1149
1150 return true;
1151}
1152
1153static bool
1154match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1155{
1156
1157
1158
1159
1160
1161 if (server->ops->select_sectype(server, ctx->sectype)
1162 == Unspecified)
1163 return false;
1164
1165
1166
1167
1168
1169
1170 if (ctx->sign && !server->sign)
1171 return false;
1172
1173 return true;
1174}
1175
1176static int match_server(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1177{
1178 struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
1179
1180 if (ctx->nosharesock)
1181 return 0;
1182
1183
1184 if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
1185 if (server->vals->protocol_id < SMB30_PROT_ID)
1186 return 0;
1187 } else if (strcmp(ctx->vals->version_string,
1188 SMBDEFAULT_VERSION_STRING) == 0) {
1189 if (server->vals->protocol_id < SMB21_PROT_ID)
1190 return 0;
1191 } else if ((server->vals != ctx->vals) || (server->ops != ctx->ops))
1192 return 0;
1193
1194 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
1195 return 0;
1196
1197 if (strcasecmp(server->hostname, ctx->server_hostname))
1198 return 0;
1199
1200 if (!match_address(server, addr,
1201 (struct sockaddr *)&ctx->srcaddr))
1202 return 0;
1203
1204 if (!match_port(server, addr))
1205 return 0;
1206
1207 if (!match_security(server, ctx))
1208 return 0;
1209
1210 if (server->echo_interval != ctx->echo_interval * HZ)
1211 return 0;
1212
1213 if (server->rdma != ctx->rdma)
1214 return 0;
1215
1216 if (server->ignore_signature != ctx->ignore_signature)
1217 return 0;
1218
1219 if (server->min_offload != ctx->min_offload)
1220 return 0;
1221
1222 return 1;
1223}
1224
1225struct TCP_Server_Info *
1226cifs_find_tcp_session(struct smb3_fs_context *ctx)
1227{
1228 struct TCP_Server_Info *server;
1229
1230 spin_lock(&cifs_tcp_ses_lock);
1231 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
1232
1233
1234
1235
1236 if (server->is_channel || !match_server(server, ctx))
1237 continue;
1238
1239 ++server->srv_count;
1240 spin_unlock(&cifs_tcp_ses_lock);
1241 cifs_dbg(FYI, "Existing tcp session with server found\n");
1242 return server;
1243 }
1244 spin_unlock(&cifs_tcp_ses_lock);
1245 return NULL;
1246}
1247
1248void
1249cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
1250{
1251 struct task_struct *task;
1252
1253 spin_lock(&cifs_tcp_ses_lock);
1254 if (--server->srv_count > 0) {
1255 spin_unlock(&cifs_tcp_ses_lock);
1256 return;
1257 }
1258
1259 put_net(cifs_net_ns(server));
1260
1261 list_del_init(&server->tcp_ses_list);
1262 spin_unlock(&cifs_tcp_ses_lock);
1263
1264 cancel_delayed_work_sync(&server->echo);
1265
1266 if (from_reconnect)
1267
1268
1269
1270
1271
1272
1273 cancel_delayed_work(&server->reconnect);
1274 else
1275 cancel_delayed_work_sync(&server->reconnect);
1276
1277 spin_lock(&GlobalMid_Lock);
1278 server->tcpStatus = CifsExiting;
1279 spin_unlock(&GlobalMid_Lock);
1280
1281 cifs_crypto_secmech_release(server);
1282 cifs_fscache_release_client_cookie(server);
1283
1284 kfree(server->session_key.response);
1285 server->session_key.response = NULL;
1286 server->session_key.len = 0;
1287 kfree(server->hostname);
1288
1289 task = xchg(&server->tsk, NULL);
1290 if (task)
1291 force_sig(SIGKILL, task);
1292}
1293
1294struct TCP_Server_Info *
1295cifs_get_tcp_session(struct smb3_fs_context *ctx)
1296{
1297 struct TCP_Server_Info *tcp_ses = NULL;
1298 int rc;
1299
1300 cifs_dbg(FYI, "UNC: %s\n", ctx->UNC);
1301
1302
1303 tcp_ses = cifs_find_tcp_session(ctx);
1304 if (tcp_ses)
1305 return tcp_ses;
1306
1307 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
1308 if (!tcp_ses) {
1309 rc = -ENOMEM;
1310 goto out_err;
1311 }
1312
1313 tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL);
1314 if (!tcp_ses->hostname) {
1315 rc = -ENOMEM;
1316 goto out_err;
1317 }
1318
1319 tcp_ses->ops = ctx->ops;
1320 tcp_ses->vals = ctx->vals;
1321 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
1322
1323 tcp_ses->noblockcnt = ctx->rootfs;
1324 tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs;
1325 tcp_ses->noautotune = ctx->noautotune;
1326 tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay;
1327 tcp_ses->rdma = ctx->rdma;
1328 tcp_ses->in_flight = 0;
1329 tcp_ses->max_in_flight = 0;
1330 tcp_ses->credits = 1;
1331 init_waitqueue_head(&tcp_ses->response_q);
1332 init_waitqueue_head(&tcp_ses->request_q);
1333 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
1334 mutex_init(&tcp_ses->srv_mutex);
1335 memcpy(tcp_ses->workstation_RFC1001_name,
1336 ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
1337 memcpy(tcp_ses->server_RFC1001_name,
1338 ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
1339 tcp_ses->session_estab = false;
1340 tcp_ses->sequence_number = 0;
1341 tcp_ses->reconnect_instance = 1;
1342 tcp_ses->lstrp = jiffies;
1343 tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression);
1344 spin_lock_init(&tcp_ses->req_lock);
1345 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
1346 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
1347 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
1348 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
1349 mutex_init(&tcp_ses->reconnect_mutex);
1350 memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
1351 sizeof(tcp_ses->srcaddr));
1352 memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
1353 sizeof(tcp_ses->dstaddr));
1354 if (ctx->use_client_guid)
1355 memcpy(tcp_ses->client_guid, ctx->client_guid,
1356 SMB2_CLIENT_GUID_SIZE);
1357 else
1358 generate_random_uuid(tcp_ses->client_guid);
1359
1360
1361
1362
1363
1364 tcp_ses->tcpStatus = CifsNew;
1365 ++tcp_ses->srv_count;
1366
1367 if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
1368 ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX)
1369 tcp_ses->echo_interval = ctx->echo_interval * HZ;
1370 else
1371 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
1372 if (tcp_ses->rdma) {
1373#ifndef CONFIG_CIFS_SMB_DIRECT
1374 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
1375 rc = -ENOENT;
1376 goto out_err_crypto_release;
1377#endif
1378 tcp_ses->smbd_conn = smbd_get_connection(
1379 tcp_ses, (struct sockaddr *)&ctx->dstaddr);
1380 if (tcp_ses->smbd_conn) {
1381 cifs_dbg(VFS, "RDMA transport established\n");
1382 rc = 0;
1383 goto smbd_connected;
1384 } else {
1385 rc = -ENOENT;
1386 goto out_err_crypto_release;
1387 }
1388 }
1389 rc = ip_connect(tcp_ses);
1390 if (rc < 0) {
1391 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
1392 goto out_err_crypto_release;
1393 }
1394smbd_connected:
1395
1396
1397
1398
1399 __module_get(THIS_MODULE);
1400 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
1401 tcp_ses, "cifsd");
1402 if (IS_ERR(tcp_ses->tsk)) {
1403 rc = PTR_ERR(tcp_ses->tsk);
1404 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
1405 module_put(THIS_MODULE);
1406 goto out_err_crypto_release;
1407 }
1408 tcp_ses->min_offload = ctx->min_offload;
1409 tcp_ses->tcpStatus = CifsNeedNegotiate;
1410
1411 tcp_ses->nr_targets = 1;
1412 tcp_ses->ignore_signature = ctx->ignore_signature;
1413
1414 spin_lock(&cifs_tcp_ses_lock);
1415 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
1416 spin_unlock(&cifs_tcp_ses_lock);
1417
1418 cifs_fscache_get_client_cookie(tcp_ses);
1419
1420
1421 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
1422
1423 return tcp_ses;
1424
1425out_err_crypto_release:
1426 cifs_crypto_secmech_release(tcp_ses);
1427
1428 put_net(cifs_net_ns(tcp_ses));
1429
1430out_err:
1431 if (tcp_ses) {
1432 kfree(tcp_ses->hostname);
1433 if (tcp_ses->ssocket)
1434 sock_release(tcp_ses->ssocket);
1435 kfree(tcp_ses);
1436 }
1437 return ERR_PTR(rc);
1438}
1439
1440static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx)
1441{
1442 if (ctx->sectype != Unspecified &&
1443 ctx->sectype != ses->sectype)
1444 return 0;
1445
1446
1447
1448
1449
1450 if (ses->chan_max < ctx->max_channels)
1451 return 0;
1452
1453 switch (ses->sectype) {
1454 case Kerberos:
1455 if (!uid_eq(ctx->cred_uid, ses->cred_uid))
1456 return 0;
1457 break;
1458 default:
1459
1460 if (ses->user_name == NULL) {
1461 if (!ctx->nullauth)
1462 return 0;
1463 break;
1464 }
1465
1466
1467 if (strncmp(ses->user_name,
1468 ctx->username ? ctx->username : "",
1469 CIFS_MAX_USERNAME_LEN))
1470 return 0;
1471 if ((ctx->username && strlen(ctx->username) != 0) &&
1472 ses->password != NULL &&
1473 strncmp(ses->password,
1474 ctx->password ? ctx->password : "",
1475 CIFS_MAX_PASSWORD_LEN))
1476 return 0;
1477 }
1478 return 1;
1479}
1480
1481
1482
1483
1484
1485
1486
1487static int
1488cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
1489{
1490 int rc = 0, xid;
1491 struct cifs_tcon *tcon;
1492 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
1493 bool seal = false;
1494 struct TCP_Server_Info *server = ses->server;
1495
1496
1497
1498
1499
1500 if (ctx->seal) {
1501 if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
1502 seal = true;
1503 else {
1504 cifs_server_dbg(VFS,
1505 "IPC: server doesn't support encryption\n");
1506 return -EOPNOTSUPP;
1507 }
1508 }
1509
1510 tcon = tconInfoAlloc();
1511 if (tcon == NULL)
1512 return -ENOMEM;
1513
1514 scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
1515
1516 xid = get_xid();
1517 tcon->ses = ses;
1518 tcon->ipc = true;
1519 tcon->seal = seal;
1520 rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls);
1521 free_xid(xid);
1522
1523 if (rc) {
1524 cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
1525 tconInfoFree(tcon);
1526 goto out;
1527 }
1528
1529 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
1530
1531 ses->tcon_ipc = tcon;
1532out:
1533 return rc;
1534}
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547static int
1548cifs_free_ipc(struct cifs_ses *ses)
1549{
1550 struct cifs_tcon *tcon = ses->tcon_ipc;
1551
1552 if (tcon == NULL)
1553 return 0;
1554
1555 tconInfoFree(tcon);
1556 ses->tcon_ipc = NULL;
1557 return 0;
1558}
1559
1560static struct cifs_ses *
1561cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1562{
1563 struct cifs_ses *ses;
1564
1565 spin_lock(&cifs_tcp_ses_lock);
1566 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1567 if (ses->status == CifsExiting)
1568 continue;
1569 if (!match_session(ses, ctx))
1570 continue;
1571 ++ses->ses_count;
1572 spin_unlock(&cifs_tcp_ses_lock);
1573 return ses;
1574 }
1575 spin_unlock(&cifs_tcp_ses_lock);
1576 return NULL;
1577}
1578
1579void cifs_put_smb_ses(struct cifs_ses *ses)
1580{
1581 unsigned int rc, xid;
1582 struct TCP_Server_Info *server = ses->server;
1583 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
1584
1585 spin_lock(&cifs_tcp_ses_lock);
1586 if (ses->status == CifsExiting) {
1587 spin_unlock(&cifs_tcp_ses_lock);
1588 return;
1589 }
1590
1591 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
1592 cifs_dbg(FYI, "%s: ses ipc: %s\n", __func__, ses->tcon_ipc ? ses->tcon_ipc->treeName : "NONE");
1593
1594 if (--ses->ses_count > 0) {
1595 spin_unlock(&cifs_tcp_ses_lock);
1596 return;
1597 }
1598 if (ses->status == CifsGood)
1599 ses->status = CifsExiting;
1600 spin_unlock(&cifs_tcp_ses_lock);
1601
1602 cifs_free_ipc(ses);
1603
1604 if (ses->status == CifsExiting && server->ops->logoff) {
1605 xid = get_xid();
1606 rc = server->ops->logoff(xid, ses);
1607 if (rc)
1608 cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
1609 __func__, rc);
1610 _free_xid(xid);
1611 }
1612
1613 spin_lock(&cifs_tcp_ses_lock);
1614 list_del_init(&ses->smb_ses_list);
1615 spin_unlock(&cifs_tcp_ses_lock);
1616
1617
1618 if (ses->chan_count > 1) {
1619 int i;
1620
1621 for (i = 1; i < ses->chan_count; i++)
1622 cifs_put_tcp_session(ses->chans[i].server, 0);
1623 }
1624
1625 sesInfoFree(ses);
1626 cifs_put_tcp_session(server, 0);
1627}
1628
1629#ifdef CONFIG_KEYS
1630
1631
1632#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
1633
1634
1635static int
1636cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
1637{
1638 int rc = 0;
1639 int is_domain = 0;
1640 const char *delim, *payload;
1641 char *desc;
1642 ssize_t len;
1643 struct key *key;
1644 struct TCP_Server_Info *server = ses->server;
1645 struct sockaddr_in *sa;
1646 struct sockaddr_in6 *sa6;
1647 const struct user_key_payload *upayload;
1648
1649 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
1650 if (!desc)
1651 return -ENOMEM;
1652
1653
1654 switch (server->dstaddr.ss_family) {
1655 case AF_INET:
1656 sa = (struct sockaddr_in *)&server->dstaddr;
1657 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
1658 break;
1659 case AF_INET6:
1660 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
1661 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
1662 break;
1663 default:
1664 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
1665 server->dstaddr.ss_family);
1666 rc = -EINVAL;
1667 goto out_err;
1668 }
1669
1670 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
1671 key = request_key(&key_type_logon, desc, "");
1672 if (IS_ERR(key)) {
1673 if (!ses->domainName) {
1674 cifs_dbg(FYI, "domainName is NULL\n");
1675 rc = PTR_ERR(key);
1676 goto out_err;
1677 }
1678
1679
1680 sprintf(desc, "cifs:d:%s", ses->domainName);
1681 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
1682 key = request_key(&key_type_logon, desc, "");
1683 if (IS_ERR(key)) {
1684 rc = PTR_ERR(key);
1685 goto out_err;
1686 }
1687 is_domain = 1;
1688 }
1689
1690 down_read(&key->sem);
1691 upayload = user_key_payload_locked(key);
1692 if (IS_ERR_OR_NULL(upayload)) {
1693 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
1694 goto out_key_put;
1695 }
1696
1697
1698 payload = upayload->data;
1699 delim = strnchr(payload, upayload->datalen, ':');
1700 cifs_dbg(FYI, "payload=%s\n", payload);
1701 if (!delim) {
1702 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
1703 upayload->datalen);
1704 rc = -EINVAL;
1705 goto out_key_put;
1706 }
1707
1708 len = delim - payload;
1709 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
1710 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
1711 len);
1712 rc = -EINVAL;
1713 goto out_key_put;
1714 }
1715
1716 ctx->username = kstrndup(payload, len, GFP_KERNEL);
1717 if (!ctx->username) {
1718 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
1719 len);
1720 rc = -ENOMEM;
1721 goto out_key_put;
1722 }
1723 cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username);
1724
1725 len = key->datalen - (len + 1);
1726 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
1727 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
1728 rc = -EINVAL;
1729 kfree(ctx->username);
1730 ctx->username = NULL;
1731 goto out_key_put;
1732 }
1733
1734 ++delim;
1735 ctx->password = kstrndup(delim, len, GFP_KERNEL);
1736 if (!ctx->password) {
1737 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
1738 len);
1739 rc = -ENOMEM;
1740 kfree(ctx->username);
1741 ctx->username = NULL;
1742 goto out_key_put;
1743 }
1744
1745
1746
1747
1748
1749 if (is_domain && ses->domainName) {
1750 ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL);
1751 if (!ctx->domainname) {
1752 cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
1753 len);
1754 rc = -ENOMEM;
1755 kfree(ctx->username);
1756 ctx->username = NULL;
1757 kfree_sensitive(ctx->password);
1758 ctx->password = NULL;
1759 goto out_key_put;
1760 }
1761 }
1762
1763out_key_put:
1764 up_read(&key->sem);
1765 key_put(key);
1766out_err:
1767 kfree(desc);
1768 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
1769 return rc;
1770}
1771#else
1772static inline int
1773cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
1774 struct cifs_ses *ses __attribute__((unused)))
1775{
1776 return -ENOSYS;
1777}
1778#endif
1779
1780
1781
1782
1783
1784
1785
1786
1787struct cifs_ses *
1788cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1789{
1790 int rc = -ENOMEM;
1791 unsigned int xid;
1792 struct cifs_ses *ses;
1793 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
1794 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
1795
1796 xid = get_xid();
1797
1798 ses = cifs_find_smb_ses(server, ctx);
1799 if (ses) {
1800 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
1801 ses->status);
1802
1803 mutex_lock(&ses->session_mutex);
1804 rc = cifs_negotiate_protocol(xid, ses);
1805 if (rc) {
1806 mutex_unlock(&ses->session_mutex);
1807
1808 cifs_put_smb_ses(ses);
1809 free_xid(xid);
1810 return ERR_PTR(rc);
1811 }
1812 if (ses->need_reconnect) {
1813 cifs_dbg(FYI, "Session needs reconnect\n");
1814 rc = cifs_setup_session(xid, ses,
1815 ctx->local_nls);
1816 if (rc) {
1817 mutex_unlock(&ses->session_mutex);
1818
1819 cifs_put_smb_ses(ses);
1820 free_xid(xid);
1821 return ERR_PTR(rc);
1822 }
1823 }
1824 mutex_unlock(&ses->session_mutex);
1825
1826
1827 cifs_put_tcp_session(server, 0);
1828 free_xid(xid);
1829 return ses;
1830 }
1831
1832 cifs_dbg(FYI, "Existing smb sess not found\n");
1833 ses = sesInfoAlloc();
1834 if (ses == NULL)
1835 goto get_ses_fail;
1836
1837
1838 ses->server = server;
1839 if (server->dstaddr.ss_family == AF_INET6)
1840 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
1841 else
1842 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
1843
1844 if (ctx->username) {
1845 ses->user_name = kstrdup(ctx->username, GFP_KERNEL);
1846 if (!ses->user_name)
1847 goto get_ses_fail;
1848 }
1849
1850
1851 if (ctx->password) {
1852 ses->password = kstrdup(ctx->password, GFP_KERNEL);
1853 if (!ses->password)
1854 goto get_ses_fail;
1855 }
1856 if (ctx->domainname) {
1857 ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL);
1858 if (!ses->domainName)
1859 goto get_ses_fail;
1860 }
1861 if (ctx->domainauto)
1862 ses->domainAuto = ctx->domainauto;
1863 ses->cred_uid = ctx->cred_uid;
1864 ses->linux_uid = ctx->linux_uid;
1865
1866 ses->sectype = ctx->sectype;
1867 ses->sign = ctx->sign;
1868 mutex_lock(&ses->session_mutex);
1869
1870
1871 ses->chans[0].server = server;
1872 ses->chan_count = 1;
1873 ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
1874
1875 rc = cifs_negotiate_protocol(xid, ses);
1876 if (!rc)
1877 rc = cifs_setup_session(xid, ses, ctx->local_nls);
1878
1879
1880 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
1881 sizeof(ses->smb3signingkey));
1882
1883 mutex_unlock(&ses->session_mutex);
1884 if (rc)
1885 goto get_ses_fail;
1886
1887
1888 spin_lock(&cifs_tcp_ses_lock);
1889 list_add(&ses->smb_ses_list, &server->smb_ses_list);
1890 spin_unlock(&cifs_tcp_ses_lock);
1891
1892 free_xid(xid);
1893
1894 cifs_setup_ipc(ses, ctx);
1895
1896 return ses;
1897
1898get_ses_fail:
1899 sesInfoFree(ses);
1900 free_xid(xid);
1901 return ERR_PTR(rc);
1902}
1903
1904static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
1905{
1906 if (tcon->tidStatus == CifsExiting)
1907 return 0;
1908 if (strncmp(tcon->treeName, ctx->UNC, MAX_TREE_SIZE))
1909 return 0;
1910 if (tcon->seal != ctx->seal)
1911 return 0;
1912 if (tcon->snapshot_time != ctx->snapshot_time)
1913 return 0;
1914 if (tcon->handle_timeout != ctx->handle_timeout)
1915 return 0;
1916 if (tcon->no_lease != ctx->no_lease)
1917 return 0;
1918 if (tcon->nodelete != ctx->nodelete)
1919 return 0;
1920 return 1;
1921}
1922
1923static struct cifs_tcon *
1924cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
1925{
1926 struct list_head *tmp;
1927 struct cifs_tcon *tcon;
1928
1929 spin_lock(&cifs_tcp_ses_lock);
1930 list_for_each(tmp, &ses->tcon_list) {
1931 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
1932#ifdef CONFIG_CIFS_DFS_UPCALL
1933 if (tcon->dfs_path)
1934 continue;
1935#endif
1936 if (!match_tcon(tcon, ctx))
1937 continue;
1938 ++tcon->tc_count;
1939 spin_unlock(&cifs_tcp_ses_lock);
1940 return tcon;
1941 }
1942 spin_unlock(&cifs_tcp_ses_lock);
1943 return NULL;
1944}
1945
1946void
1947cifs_put_tcon(struct cifs_tcon *tcon)
1948{
1949 unsigned int xid;
1950 struct cifs_ses *ses;
1951
1952
1953
1954
1955
1956 if (tcon == NULL || tcon->ipc)
1957 return;
1958
1959 ses = tcon->ses;
1960 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
1961 spin_lock(&cifs_tcp_ses_lock);
1962 if (--tcon->tc_count > 0) {
1963 spin_unlock(&cifs_tcp_ses_lock);
1964 return;
1965 }
1966
1967#ifdef CONFIG_CIFS_SWN_UPCALL
1968 if (tcon->use_witness) {
1969 int rc;
1970
1971 rc = cifs_swn_unregister(tcon);
1972 if (rc < 0) {
1973 cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
1974 __func__, rc);
1975 }
1976 }
1977#endif
1978
1979 list_del_init(&tcon->tcon_list);
1980 spin_unlock(&cifs_tcp_ses_lock);
1981
1982 xid = get_xid();
1983 if (ses->server->ops->tree_disconnect)
1984 ses->server->ops->tree_disconnect(xid, tcon);
1985 _free_xid(xid);
1986
1987 cifs_fscache_release_super_cookie(tcon);
1988 tconInfoFree(tcon);
1989 cifs_put_smb_ses(ses);
1990}
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012static struct cifs_tcon *
2013cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
2014{
2015 int rc, xid;
2016 struct cifs_tcon *tcon;
2017
2018 tcon = cifs_find_tcon(ses, ctx);
2019 if (tcon) {
2020
2021
2022
2023
2024 cifs_dbg(FYI, "Found match on UNC path\n");
2025 cifs_put_smb_ses(ses);
2026 return tcon;
2027 }
2028
2029 if (!ses->server->ops->tree_connect) {
2030 rc = -ENOSYS;
2031 goto out_fail;
2032 }
2033
2034 tcon = tconInfoAlloc();
2035 if (tcon == NULL) {
2036 rc = -ENOMEM;
2037 goto out_fail;
2038 }
2039
2040 if (ctx->snapshot_time) {
2041 if (ses->server->vals->protocol_id == 0) {
2042 cifs_dbg(VFS,
2043 "Use SMB2 or later for snapshot mount option\n");
2044 rc = -EOPNOTSUPP;
2045 goto out_fail;
2046 } else
2047 tcon->snapshot_time = ctx->snapshot_time;
2048 }
2049
2050 if (ctx->handle_timeout) {
2051 if (ses->server->vals->protocol_id == 0) {
2052 cifs_dbg(VFS,
2053 "Use SMB2.1 or later for handle timeout option\n");
2054 rc = -EOPNOTSUPP;
2055 goto out_fail;
2056 } else
2057 tcon->handle_timeout = ctx->handle_timeout;
2058 }
2059
2060 tcon->ses = ses;
2061 if (ctx->password) {
2062 tcon->password = kstrdup(ctx->password, GFP_KERNEL);
2063 if (!tcon->password) {
2064 rc = -ENOMEM;
2065 goto out_fail;
2066 }
2067 }
2068
2069 if (ctx->seal) {
2070 if (ses->server->vals->protocol_id == 0) {
2071 cifs_dbg(VFS,
2072 "SMB3 or later required for encryption\n");
2073 rc = -EOPNOTSUPP;
2074 goto out_fail;
2075 } else if (tcon->ses->server->capabilities &
2076 SMB2_GLOBAL_CAP_ENCRYPTION)
2077 tcon->seal = true;
2078 else {
2079 cifs_dbg(VFS, "Encryption is not supported on share\n");
2080 rc = -EOPNOTSUPP;
2081 goto out_fail;
2082 }
2083 }
2084
2085 if (ctx->linux_ext) {
2086 if (ses->server->posix_ext_supported) {
2087 tcon->posix_extensions = true;
2088 pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
2089 } else {
2090 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
2091 rc = -EOPNOTSUPP;
2092 goto out_fail;
2093 }
2094 }
2095
2096
2097
2098
2099
2100 xid = get_xid();
2101 rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon,
2102 ctx->local_nls);
2103 free_xid(xid);
2104 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
2105 if (rc)
2106 goto out_fail;
2107
2108 tcon->use_persistent = false;
2109
2110 if (ctx->persistent) {
2111 if (ses->server->vals->protocol_id == 0) {
2112 cifs_dbg(VFS,
2113 "SMB3 or later required for persistent handles\n");
2114 rc = -EOPNOTSUPP;
2115 goto out_fail;
2116 } else if (ses->server->capabilities &
2117 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2118 tcon->use_persistent = true;
2119 else {
2120 cifs_dbg(VFS,
2121 "Persistent handles not supported on share\n");
2122 rc = -EOPNOTSUPP;
2123 goto out_fail;
2124 }
2125 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2126 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2127 && (ctx->nopersistent == false)) {
2128 cifs_dbg(FYI, "enabling persistent handles\n");
2129 tcon->use_persistent = true;
2130 } else if (ctx->resilient) {
2131 if (ses->server->vals->protocol_id == 0) {
2132 cifs_dbg(VFS,
2133 "SMB2.1 or later required for resilient handles\n");
2134 rc = -EOPNOTSUPP;
2135 goto out_fail;
2136 }
2137 tcon->use_resilient = true;
2138 }
2139#ifdef CONFIG_CIFS_SWN_UPCALL
2140 tcon->use_witness = false;
2141 if (ctx->witness) {
2142 if (ses->server->vals->protocol_id >= SMB30_PROT_ID) {
2143 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) {
2144
2145
2146
2147
2148 tcon->use_witness = true;
2149
2150 rc = cifs_swn_register(tcon);
2151 if (rc < 0) {
2152 cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc);
2153 goto out_fail;
2154 }
2155 } else {
2156
2157 cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n");
2158 rc = -EOPNOTSUPP;
2159 goto out_fail;
2160 }
2161 } else {
2162 cifs_dbg(VFS, "SMB3 or later required for witness option\n");
2163 rc = -EOPNOTSUPP;
2164 goto out_fail;
2165 }
2166 }
2167#endif
2168
2169
2170 if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
2171 if (ctx->cache_ro)
2172 cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
2173 else if (ctx->cache_rw)
2174 cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
2175 }
2176
2177 if (ctx->no_lease) {
2178 if (ses->server->vals->protocol_id == 0) {
2179 cifs_dbg(VFS,
2180 "SMB2 or later required for nolease option\n");
2181 rc = -EOPNOTSUPP;
2182 goto out_fail;
2183 } else
2184 tcon->no_lease = ctx->no_lease;
2185 }
2186
2187
2188
2189
2190
2191
2192 tcon->retry = ctx->retry;
2193 tcon->nocase = ctx->nocase;
2194 if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)
2195 tcon->nohandlecache = ctx->nohandlecache;
2196 else
2197 tcon->nohandlecache = 1;
2198 tcon->nodelete = ctx->nodelete;
2199 tcon->local_lease = ctx->local_lease;
2200 INIT_LIST_HEAD(&tcon->pending_opens);
2201
2202 spin_lock(&cifs_tcp_ses_lock);
2203 list_add(&tcon->tcon_list, &ses->tcon_list);
2204 spin_unlock(&cifs_tcp_ses_lock);
2205
2206 cifs_fscache_get_super_cookie(tcon);
2207
2208 return tcon;
2209
2210out_fail:
2211 tconInfoFree(tcon);
2212 return ERR_PTR(rc);
2213}
2214
2215void
2216cifs_put_tlink(struct tcon_link *tlink)
2217{
2218 if (!tlink || IS_ERR(tlink))
2219 return;
2220
2221 if (!atomic_dec_and_test(&tlink->tl_count) ||
2222 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2223 tlink->tl_time = jiffies;
2224 return;
2225 }
2226
2227 if (!IS_ERR(tlink_tcon(tlink)))
2228 cifs_put_tcon(tlink_tcon(tlink));
2229 kfree(tlink);
2230 return;
2231}
2232
2233static int
2234compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2235{
2236 struct cifs_sb_info *old = CIFS_SB(sb);
2237 struct cifs_sb_info *new = mnt_data->cifs_sb;
2238 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
2239 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
2240
2241 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2242 return 0;
2243
2244 if (old->mnt_cifs_serverino_autodisabled)
2245 newflags &= ~CIFS_MOUNT_SERVER_INUM;
2246
2247 if (oldflags != newflags)
2248 return 0;
2249
2250
2251
2252
2253
2254 if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize)
2255 return 0;
2256
2257 if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize)
2258 return 0;
2259
2260 if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) ||
2261 !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid))
2262 return 0;
2263
2264 if (old->ctx->file_mode != new->ctx->file_mode ||
2265 old->ctx->dir_mode != new->ctx->dir_mode)
2266 return 0;
2267
2268 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2269 return 0;
2270
2271 if (old->ctx->acregmax != new->ctx->acregmax)
2272 return 0;
2273 if (old->ctx->acdirmax != new->ctx->acdirmax)
2274 return 0;
2275
2276 return 1;
2277}
2278
2279static int
2280match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2281{
2282 struct cifs_sb_info *old = CIFS_SB(sb);
2283 struct cifs_sb_info *new = mnt_data->cifs_sb;
2284 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2285 old->prepath;
2286 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2287 new->prepath;
2288
2289 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
2290 return 1;
2291 else if (!old_set && !new_set)
2292 return 1;
2293
2294 return 0;
2295}
2296
2297int
2298cifs_match_super(struct super_block *sb, void *data)
2299{
2300 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
2301 struct smb3_fs_context *ctx;
2302 struct cifs_sb_info *cifs_sb;
2303 struct TCP_Server_Info *tcp_srv;
2304 struct cifs_ses *ses;
2305 struct cifs_tcon *tcon;
2306 struct tcon_link *tlink;
2307 int rc = 0;
2308
2309 spin_lock(&cifs_tcp_ses_lock);
2310 cifs_sb = CIFS_SB(sb);
2311 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2312 if (IS_ERR(tlink)) {
2313 spin_unlock(&cifs_tcp_ses_lock);
2314 return rc;
2315 }
2316 tcon = tlink_tcon(tlink);
2317 ses = tcon->ses;
2318 tcp_srv = ses->server;
2319
2320 ctx = mnt_data->ctx;
2321
2322 if (!match_server(tcp_srv, ctx) ||
2323 !match_session(ses, ctx) ||
2324 !match_tcon(tcon, ctx) ||
2325 !match_prepath(sb, mnt_data)) {
2326 rc = 0;
2327 goto out;
2328 }
2329
2330 rc = compare_mount_options(sb, mnt_data);
2331out:
2332 spin_unlock(&cifs_tcp_ses_lock);
2333 cifs_put_tlink(tlink);
2334 return rc;
2335}
2336
2337#ifdef CONFIG_DEBUG_LOCK_ALLOC
2338static struct lock_class_key cifs_key[2];
2339static struct lock_class_key cifs_slock_key[2];
2340
2341static inline void
2342cifs_reclassify_socket4(struct socket *sock)
2343{
2344 struct sock *sk = sock->sk;
2345 BUG_ON(!sock_allow_reclassification(sk));
2346 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
2347 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
2348}
2349
2350static inline void
2351cifs_reclassify_socket6(struct socket *sock)
2352{
2353 struct sock *sk = sock->sk;
2354 BUG_ON(!sock_allow_reclassification(sk));
2355 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
2356 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
2357}
2358#else
2359static inline void
2360cifs_reclassify_socket4(struct socket *sock)
2361{
2362}
2363
2364static inline void
2365cifs_reclassify_socket6(struct socket *sock)
2366{
2367}
2368#endif
2369
2370
2371static void rfc1002mangle(char *target, char *source, unsigned int length)
2372{
2373 unsigned int i, j;
2374
2375 for (i = 0, j = 0; i < (length); i++) {
2376
2377 target[j] = 'A' + (0x0F & (source[i] >> 4));
2378 target[j+1] = 'A' + (0x0F & source[i]);
2379 j += 2;
2380 }
2381
2382}
2383
2384static int
2385bind_socket(struct TCP_Server_Info *server)
2386{
2387 int rc = 0;
2388 if (server->srcaddr.ss_family != AF_UNSPEC) {
2389
2390 struct socket *socket = server->ssocket;
2391 rc = socket->ops->bind(socket,
2392 (struct sockaddr *) &server->srcaddr,
2393 sizeof(server->srcaddr));
2394 if (rc < 0) {
2395 struct sockaddr_in *saddr4;
2396 struct sockaddr_in6 *saddr6;
2397 saddr4 = (struct sockaddr_in *)&server->srcaddr;
2398 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
2399 if (saddr6->sin6_family == AF_INET6)
2400 cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
2401 &saddr6->sin6_addr, rc);
2402 else
2403 cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
2404 &saddr4->sin_addr.s_addr, rc);
2405 }
2406 }
2407 return rc;
2408}
2409
2410static int
2411ip_rfc1001_connect(struct TCP_Server_Info *server)
2412{
2413 int rc = 0;
2414
2415
2416
2417
2418
2419 struct rfc1002_session_packet *ses_init_buf;
2420 struct smb_hdr *smb_buf;
2421 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
2422 GFP_KERNEL);
2423 if (ses_init_buf) {
2424 ses_init_buf->trailer.session_req.called_len = 32;
2425
2426 if (server->server_RFC1001_name[0] != 0)
2427 rfc1002mangle(ses_init_buf->trailer.
2428 session_req.called_name,
2429 server->server_RFC1001_name,
2430 RFC1001_NAME_LEN_WITH_NULL);
2431 else
2432 rfc1002mangle(ses_init_buf->trailer.
2433 session_req.called_name,
2434 DEFAULT_CIFS_CALLED_NAME,
2435 RFC1001_NAME_LEN_WITH_NULL);
2436
2437 ses_init_buf->trailer.session_req.calling_len = 32;
2438
2439
2440
2441
2442
2443 if (server->workstation_RFC1001_name[0] != 0)
2444 rfc1002mangle(ses_init_buf->trailer.
2445 session_req.calling_name,
2446 server->workstation_RFC1001_name,
2447 RFC1001_NAME_LEN_WITH_NULL);
2448 else
2449 rfc1002mangle(ses_init_buf->trailer.
2450 session_req.calling_name,
2451 "LINUX_CIFS_CLNT",
2452 RFC1001_NAME_LEN_WITH_NULL);
2453
2454 ses_init_buf->trailer.session_req.scope1 = 0;
2455 ses_init_buf->trailer.session_req.scope2 = 0;
2456 smb_buf = (struct smb_hdr *)ses_init_buf;
2457
2458
2459 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
2460 rc = smb_send(server, smb_buf, 0x44);
2461 kfree(ses_init_buf);
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472 usleep_range(1000, 2000);
2473 }
2474
2475
2476
2477
2478
2479 return rc;
2480}
2481
2482static int
2483generic_ip_connect(struct TCP_Server_Info *server)
2484{
2485 int rc = 0;
2486 __be16 sport;
2487 int slen, sfamily;
2488 struct socket *socket = server->ssocket;
2489 struct sockaddr *saddr;
2490
2491 saddr = (struct sockaddr *) &server->dstaddr;
2492
2493 if (server->dstaddr.ss_family == AF_INET6) {
2494 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
2495
2496 sport = ipv6->sin6_port;
2497 slen = sizeof(struct sockaddr_in6);
2498 sfamily = AF_INET6;
2499 cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
2500 ntohs(sport));
2501 } else {
2502 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
2503
2504 sport = ipv4->sin_port;
2505 slen = sizeof(struct sockaddr_in);
2506 sfamily = AF_INET;
2507 cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
2508 ntohs(sport));
2509 }
2510
2511 if (socket == NULL) {
2512 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
2513 IPPROTO_TCP, &socket, 1);
2514 if (rc < 0) {
2515 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
2516 server->ssocket = NULL;
2517 return rc;
2518 }
2519
2520
2521 cifs_dbg(FYI, "Socket created\n");
2522 server->ssocket = socket;
2523 socket->sk->sk_allocation = GFP_NOFS;
2524 if (sfamily == AF_INET6)
2525 cifs_reclassify_socket6(socket);
2526 else
2527 cifs_reclassify_socket4(socket);
2528 }
2529
2530 rc = bind_socket(server);
2531 if (rc < 0)
2532 return rc;
2533
2534
2535
2536
2537
2538
2539 socket->sk->sk_rcvtimeo = 7 * HZ;
2540 socket->sk->sk_sndtimeo = 5 * HZ;
2541
2542
2543 if (server->noautotune) {
2544 if (socket->sk->sk_sndbuf < (200 * 1024))
2545 socket->sk->sk_sndbuf = 200 * 1024;
2546 if (socket->sk->sk_rcvbuf < (140 * 1024))
2547 socket->sk->sk_rcvbuf = 140 * 1024;
2548 }
2549
2550 if (server->tcp_nodelay)
2551 tcp_sock_set_nodelay(socket->sk);
2552
2553 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
2554 socket->sk->sk_sndbuf,
2555 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
2556
2557 rc = socket->ops->connect(socket, saddr, slen,
2558 server->noblockcnt ? O_NONBLOCK : 0);
2559
2560
2561
2562
2563
2564 if (server->noblockcnt && rc == -EINPROGRESS)
2565 rc = 0;
2566 if (rc < 0) {
2567 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
2568 sock_release(socket);
2569 server->ssocket = NULL;
2570 return rc;
2571 }
2572
2573 if (sport == htons(RFC1001_PORT))
2574 rc = ip_rfc1001_connect(server);
2575
2576 return rc;
2577}
2578
2579static int
2580ip_connect(struct TCP_Server_Info *server)
2581{
2582 __be16 *sport;
2583 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2584 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2585
2586 if (server->dstaddr.ss_family == AF_INET6)
2587 sport = &addr6->sin6_port;
2588 else
2589 sport = &addr->sin_port;
2590
2591 if (*sport == 0) {
2592 int rc;
2593
2594
2595 *sport = htons(CIFS_PORT);
2596
2597 rc = generic_ip_connect(server);
2598 if (rc >= 0)
2599 return rc;
2600
2601
2602 *sport = htons(RFC1001_PORT);
2603 }
2604
2605 return generic_ip_connect(server);
2606}
2607
2608void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
2609 struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
2610{
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
2623
2624 if (ctx && ctx->no_linux_ext) {
2625 tcon->fsUnixInfo.Capability = 0;
2626 tcon->unix_ext = 0;
2627 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
2628 return;
2629 } else if (ctx)
2630 tcon->unix_ext = 1;
2631
2632 if (tcon->unix_ext == 0) {
2633 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
2634 return;
2635 }
2636
2637 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
2638 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
2639 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
2640
2641
2642
2643
2644 if (ctx == NULL) {
2645
2646
2647
2648
2649 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
2650 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
2651 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
2652 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
2653 cifs_dbg(VFS, "POSIXPATH support change\n");
2654 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
2655 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
2656 cifs_dbg(VFS, "possible reconnect error\n");
2657 cifs_dbg(VFS, "server disabled POSIX path support\n");
2658 }
2659 }
2660
2661 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
2662 cifs_dbg(VFS, "per-share encryption not supported yet\n");
2663
2664 cap &= CIFS_UNIX_CAP_MASK;
2665 if (ctx && ctx->no_psx_acl)
2666 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
2667 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
2668 cifs_dbg(FYI, "negotiated posix acl support\n");
2669 if (cifs_sb)
2670 cifs_sb->mnt_cifs_flags |=
2671 CIFS_MOUNT_POSIXACL;
2672 }
2673
2674 if (ctx && ctx->posix_paths == 0)
2675 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
2676 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2677 cifs_dbg(FYI, "negotiate posix pathnames\n");
2678 if (cifs_sb)
2679 cifs_sb->mnt_cifs_flags |=
2680 CIFS_MOUNT_POSIX_PATHS;
2681 }
2682
2683 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
2684#ifdef CONFIG_CIFS_DEBUG2
2685 if (cap & CIFS_UNIX_FCNTL_CAP)
2686 cifs_dbg(FYI, "FCNTL cap\n");
2687 if (cap & CIFS_UNIX_EXTATTR_CAP)
2688 cifs_dbg(FYI, "EXTATTR cap\n");
2689 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
2690 cifs_dbg(FYI, "POSIX path cap\n");
2691 if (cap & CIFS_UNIX_XATTR_CAP)
2692 cifs_dbg(FYI, "XATTR cap\n");
2693 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
2694 cifs_dbg(FYI, "POSIX ACL cap\n");
2695 if (cap & CIFS_UNIX_LARGE_READ_CAP)
2696 cifs_dbg(FYI, "very large read cap\n");
2697 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
2698 cifs_dbg(FYI, "very large write cap\n");
2699 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
2700 cifs_dbg(FYI, "transport encryption cap\n");
2701 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
2702 cifs_dbg(FYI, "mandatory transport encryption cap\n");
2703#endif
2704 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
2705 if (ctx == NULL)
2706 cifs_dbg(FYI, "resetting capabilities failed\n");
2707 else
2708 cifs_dbg(VFS, "Negotiating Unix capabilities with the server failed. Consider mounting with the Unix Extensions disabled if problems are found by specifying the nounix mount option.\n");
2709
2710 }
2711 }
2712}
2713
2714int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb)
2715{
2716 struct smb3_fs_context *ctx = cifs_sb->ctx;
2717
2718 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
2719
2720 spin_lock_init(&cifs_sb->tlink_tree_lock);
2721 cifs_sb->tlink_tree = RB_ROOT;
2722
2723 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
2724 ctx->file_mode, ctx->dir_mode);
2725
2726
2727 if (ctx->iocharset == NULL) {
2728
2729 cifs_sb->local_nls = load_nls_default();
2730 } else {
2731 cifs_sb->local_nls = load_nls(ctx->iocharset);
2732 if (cifs_sb->local_nls == NULL) {
2733 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
2734 ctx->iocharset);
2735 return -ELIBACC;
2736 }
2737 }
2738 ctx->local_nls = cifs_sb->local_nls;
2739
2740 smb3_update_mnt_flags(cifs_sb);
2741
2742 if (ctx->direct_io)
2743 cifs_dbg(FYI, "mounting share using direct i/o\n");
2744 if (ctx->cache_ro) {
2745 cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
2746 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
2747 } else if (ctx->cache_rw) {
2748 cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
2749 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
2750 CIFS_MOUNT_RW_CACHE);
2751 }
2752
2753 if ((ctx->cifs_acl) && (ctx->dynperm))
2754 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
2755
2756 if (ctx->prepath) {
2757 cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL);
2758 if (cifs_sb->prepath == NULL)
2759 return -ENOMEM;
2760 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
2761 }
2762
2763 return 0;
2764}
2765
2766
2767static inline void mount_put_conns(struct cifs_sb_info *cifs_sb,
2768 unsigned int xid,
2769 struct TCP_Server_Info *server,
2770 struct cifs_ses *ses, struct cifs_tcon *tcon)
2771{
2772 int rc = 0;
2773
2774 if (tcon)
2775 cifs_put_tcon(tcon);
2776 else if (ses)
2777 cifs_put_smb_ses(ses);
2778 else if (server)
2779 cifs_put_tcp_session(server, 0);
2780 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
2781 free_xid(xid);
2782}
2783
2784
2785static int mount_get_conns(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb,
2786 unsigned int *xid,
2787 struct TCP_Server_Info **nserver,
2788 struct cifs_ses **nses, struct cifs_tcon **ntcon)
2789{
2790 int rc = 0;
2791 struct TCP_Server_Info *server;
2792 struct cifs_ses *ses;
2793 struct cifs_tcon *tcon;
2794
2795 *nserver = NULL;
2796 *nses = NULL;
2797 *ntcon = NULL;
2798
2799 *xid = get_xid();
2800
2801
2802 server = cifs_get_tcp_session(ctx);
2803 if (IS_ERR(server)) {
2804 rc = PTR_ERR(server);
2805 return rc;
2806 }
2807
2808 *nserver = server;
2809
2810 if ((ctx->max_credits < 20) || (ctx->max_credits > 60000))
2811 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
2812 else
2813 server->max_credits = ctx->max_credits;
2814
2815
2816 ses = cifs_get_smb_ses(server, ctx);
2817 if (IS_ERR(ses)) {
2818 rc = PTR_ERR(ses);
2819 return rc;
2820 }
2821
2822 *nses = ses;
2823
2824 if ((ctx->persistent == true) && (!(ses->server->capabilities &
2825 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
2826 cifs_server_dbg(VFS, "persistent handles not supported by server\n");
2827 return -EOPNOTSUPP;
2828 }
2829
2830
2831 tcon = cifs_get_tcon(ses, ctx);
2832 if (IS_ERR(tcon)) {
2833 rc = PTR_ERR(tcon);
2834 return rc;
2835 }
2836
2837 *ntcon = tcon;
2838
2839
2840 if (tcon->posix_extensions)
2841 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
2842
2843
2844 if (cap_unix(tcon->ses)) {
2845
2846
2847
2848
2849 reset_cifs_unix_caps(*xid, tcon, cifs_sb, ctx);
2850 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
2851 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
2852 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP))
2853 return -EACCES;
2854 } else
2855 tcon->unix_ext = 0;
2856
2857
2858 if (!tcon->pipe && server->ops->qfs_tcon) {
2859 server->ops->qfs_tcon(*xid, tcon, cifs_sb);
2860 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
2861 if (tcon->fsDevInfo.DeviceCharacteristics &
2862 cpu_to_le32(FILE_READ_ONLY_DEVICE))
2863 cifs_dbg(VFS, "mounted to read only share\n");
2864 else if ((cifs_sb->mnt_cifs_flags &
2865 CIFS_MOUNT_RW_CACHE) == 0)
2866 cifs_dbg(VFS, "read only mount of RW share\n");
2867
2868 }
2869 }
2870
2871
2872
2873
2874
2875
2876 if ((cifs_sb->ctx->wsize == 0) ||
2877 (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx)))
2878 cifs_sb->ctx->wsize = server->ops->negotiate_wsize(tcon, ctx);
2879 if ((cifs_sb->ctx->rsize == 0) ||
2880 (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx)))
2881 cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx);
2882
2883 return 0;
2884}
2885
2886static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
2887 struct cifs_tcon *tcon)
2888{
2889 struct tcon_link *tlink;
2890
2891
2892 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
2893 if (tlink == NULL)
2894 return -ENOMEM;
2895
2896 tlink->tl_uid = ses->linux_uid;
2897 tlink->tl_tcon = tcon;
2898 tlink->tl_time = jiffies;
2899 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
2900 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
2901
2902 cifs_sb->master_tlink = tlink;
2903 spin_lock(&cifs_sb->tlink_tree_lock);
2904 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
2905 spin_unlock(&cifs_sb->tlink_tree_lock);
2906
2907 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
2908 TLINK_IDLE_EXPIRE);
2909 return 0;
2910}
2911
2912#ifdef CONFIG_CIFS_DFS_UPCALL
2913
2914
2915
2916
2917static char *
2918build_unc_path_to_root(const struct smb3_fs_context *ctx,
2919 const struct cifs_sb_info *cifs_sb, bool useppath)
2920{
2921 char *full_path, *pos;
2922 unsigned int pplen = useppath && ctx->prepath ?
2923 strlen(ctx->prepath) + 1 : 0;
2924 unsigned int unc_len = strnlen(ctx->UNC, MAX_TREE_SIZE + 1);
2925
2926 if (unc_len > MAX_TREE_SIZE)
2927 return ERR_PTR(-EINVAL);
2928
2929 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
2930 if (full_path == NULL)
2931 return ERR_PTR(-ENOMEM);
2932
2933 memcpy(full_path, ctx->UNC, unc_len);
2934 pos = full_path + unc_len;
2935
2936 if (pplen) {
2937 *pos = CIFS_DIR_SEP(cifs_sb);
2938 memcpy(pos + 1, ctx->prepath, pplen);
2939 pos += pplen;
2940 }
2941
2942 *pos = '\0';
2943 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
2944 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
2945 return full_path;
2946}
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958static int
2959expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
2960 struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb,
2961 char *ref_path)
2962{
2963 int rc;
2964 struct dfs_info3_param referral = {0};
2965 char *full_path = NULL, *mdata = NULL;
2966
2967 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
2968 return -EREMOTE;
2969
2970 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
2971 if (IS_ERR(full_path))
2972 return PTR_ERR(full_path);
2973
2974 rc = dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
2975 ref_path, &referral, NULL);
2976 if (!rc) {
2977 char *fake_devname = NULL;
2978
2979 mdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options,
2980 full_path + 1, &referral,
2981 &fake_devname);
2982 free_dfs_info_param(&referral);
2983
2984 if (IS_ERR(mdata)) {
2985 rc = PTR_ERR(mdata);
2986 mdata = NULL;
2987 } else {
2988
2989
2990
2991
2992
2993
2994 kfree(ctx->prepath);
2995 ctx->prepath = NULL;
2996 rc = cifs_setup_volume_info(ctx, mdata, fake_devname);
2997 }
2998 kfree(fake_devname);
2999 kfree(cifs_sb->ctx->mount_options);
3000 cifs_sb->ctx->mount_options = mdata;
3001 }
3002 kfree(full_path);
3003 return rc;
3004}
3005
3006static inline int get_next_dfs_tgt(const char *path,
3007 struct dfs_cache_tgt_list *tgt_list,
3008 struct dfs_cache_tgt_iterator **tgt_it)
3009{
3010 if (!*tgt_it)
3011 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
3012 else
3013 *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
3014 return !*tgt_it ? -EHOSTDOWN : 0;
3015}
3016
3017static int update_vol_info(const struct dfs_cache_tgt_iterator *tgt_it,
3018 struct smb3_fs_context *fake_ctx, struct smb3_fs_context *ctx)
3019{
3020 const char *tgt = dfs_cache_get_tgt_name(tgt_it);
3021 int len = strlen(tgt) + 2;
3022 char *new_unc;
3023
3024 new_unc = kmalloc(len, GFP_KERNEL);
3025 if (!new_unc)
3026 return -ENOMEM;
3027 scnprintf(new_unc, len, "\\%s", tgt);
3028
3029 kfree(ctx->UNC);
3030 ctx->UNC = new_unc;
3031
3032 if (fake_ctx->prepath) {
3033 kfree(ctx->prepath);
3034 ctx->prepath = fake_ctx->prepath;
3035 fake_ctx->prepath = NULL;
3036 }
3037 memcpy(&ctx->dstaddr, &fake_ctx->dstaddr, sizeof(ctx->dstaddr));
3038
3039 return 0;
3040}
3041
3042static int do_dfs_failover(const char *path, const char *full_path, struct cifs_sb_info *cifs_sb,
3043 struct smb3_fs_context *ctx, struct cifs_ses *root_ses,
3044 unsigned int *xid, struct TCP_Server_Info **server,
3045 struct cifs_ses **ses, struct cifs_tcon **tcon)
3046{
3047 int rc;
3048 struct dfs_cache_tgt_list tgt_list = {0};
3049 struct dfs_cache_tgt_iterator *tgt_it = NULL;
3050 struct smb3_fs_context tmp_ctx = {NULL};
3051
3052 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
3053 return -EOPNOTSUPP;
3054
3055 cifs_dbg(FYI, "%s: path=%s full_path=%s\n", __func__, path, full_path);
3056
3057 rc = dfs_cache_noreq_find(path, NULL, &tgt_list);
3058 if (rc)
3059 return rc;
3060
3061
3062
3063
3064 rc = smb3_fs_context_dup(&tmp_ctx, ctx);
3065 if (rc)
3066 goto out;
3067
3068 for (;;) {
3069 struct dfs_info3_param ref = {0};
3070 char *fake_devname = NULL, *mdata = NULL;
3071
3072
3073 rc = get_next_dfs_tgt(path, &tgt_list, &tgt_it);
3074 if (rc)
3075 break;
3076
3077 rc = dfs_cache_get_tgt_referral(path, tgt_it, &ref);
3078 if (rc)
3079 break;
3080
3081 cifs_dbg(FYI, "%s: old ctx: UNC=%s prepath=%s\n", __func__, tmp_ctx.UNC,
3082 tmp_ctx.prepath);
3083
3084 mdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options, full_path + 1, &ref,
3085 &fake_devname);
3086 free_dfs_info_param(&ref);
3087
3088 if (IS_ERR(mdata)) {
3089 rc = PTR_ERR(mdata);
3090 mdata = NULL;
3091 } else
3092 rc = cifs_setup_volume_info(&tmp_ctx, mdata, fake_devname);
3093
3094 kfree(mdata);
3095 kfree(fake_devname);
3096
3097 if (rc)
3098 break;
3099
3100 cifs_dbg(FYI, "%s: new ctx: UNC=%s prepath=%s\n", __func__, tmp_ctx.UNC,
3101 tmp_ctx.prepath);
3102
3103 mount_put_conns(cifs_sb, *xid, *server, *ses, *tcon);
3104 rc = mount_get_conns(&tmp_ctx, cifs_sb, xid, server, ses, tcon);
3105 if (!rc || (*server && *ses)) {
3106
3107
3108
3109
3110 rc = update_vol_info(tgt_it, &tmp_ctx, ctx);
3111 break;
3112 }
3113 }
3114 if (!rc) {
3115 cifs_dbg(FYI, "%s: final ctx: UNC=%s prepath=%s\n", __func__, tmp_ctx.UNC,
3116 tmp_ctx.prepath);
3117
3118
3119
3120
3121 rc = dfs_cache_update_tgthint(*xid, root_ses ? root_ses : *ses, cifs_sb->local_nls,
3122 cifs_remap(cifs_sb), path, tgt_it);
3123 }
3124
3125out:
3126 smb3_cleanup_fs_context_contents(&tmp_ctx);
3127 dfs_cache_free_tgts(&tgt_list);
3128 return rc;
3129}
3130#endif
3131
3132
3133
3134
3135int
3136cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const char *devname)
3137{
3138 int rc;
3139
3140 if (devname) {
3141 cifs_dbg(FYI, "%s: devname=%s\n", __func__, devname);
3142 rc = smb3_parse_devname(devname, ctx);
3143 if (rc) {
3144 cifs_dbg(VFS, "%s: failed to parse %s: %d\n", __func__, devname, rc);
3145 return rc;
3146 }
3147 }
3148
3149 if (mntopts) {
3150 char *ip;
3151
3152 rc = smb3_parse_opt(mntopts, "ip", &ip);
3153 if (rc) {
3154 cifs_dbg(VFS, "%s: failed to parse ip options: %d\n", __func__, rc);
3155 return rc;
3156 }
3157
3158 rc = cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip, strlen(ip));
3159 kfree(ip);
3160 if (!rc) {
3161 cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
3162 return -EINVAL;
3163 }
3164 }
3165
3166 if (ctx->nullauth) {
3167 cifs_dbg(FYI, "Anonymous login\n");
3168 kfree(ctx->username);
3169 ctx->username = NULL;
3170 } else if (ctx->username) {
3171
3172 cifs_dbg(FYI, "Username: %s\n", ctx->username);
3173 } else {
3174 cifs_dbg(VFS, "No username specified\n");
3175
3176
3177 return -EINVAL;
3178 }
3179
3180 return 0;
3181}
3182
3183static int
3184cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3185 unsigned int xid,
3186 struct cifs_tcon *tcon,
3187 struct cifs_sb_info *cifs_sb,
3188 char *full_path,
3189 int added_treename)
3190{
3191 int rc;
3192 char *s;
3193 char sep, tmp;
3194 int skip = added_treename ? 1 : 0;
3195
3196 sep = CIFS_DIR_SEP(cifs_sb);
3197 s = full_path;
3198
3199 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3200 while (rc == 0) {
3201
3202 while (*s == sep)
3203 s++;
3204 if (!*s)
3205 break;
3206
3207 while (*s && *s != sep)
3208 s++;
3209
3210
3211
3212
3213 if (skip) {
3214 skip = 0;
3215 continue;
3216 }
3217
3218
3219
3220
3221 tmp = *s;
3222 *s = 0;
3223 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3224 full_path);
3225 *s = tmp;
3226 }
3227 return rc;
3228}
3229
3230
3231
3232
3233
3234static int is_path_remote(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx,
3235 const unsigned int xid,
3236 struct TCP_Server_Info *server,
3237 struct cifs_tcon *tcon)
3238{
3239 int rc;
3240 char *full_path;
3241
3242 if (!server->ops->is_path_accessible)
3243 return -EOPNOTSUPP;
3244
3245
3246
3247
3248 full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon,
3249 tcon->Flags & SMB_SHARE_IS_IN_DFS);
3250 if (full_path == NULL)
3251 return -ENOMEM;
3252
3253 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
3254
3255 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3256 full_path);
3257 if (rc != 0 && rc != -EREMOTE) {
3258 kfree(full_path);
3259 return rc;
3260 }
3261
3262 if (rc != -EREMOTE) {
3263 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
3264 cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
3265 if (rc != 0) {
3266 cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
3267 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3268 rc = 0;
3269 }
3270 }
3271
3272 kfree(full_path);
3273 return rc;
3274}
3275
3276#ifdef CONFIG_CIFS_DFS_UPCALL
3277static void set_root_ses(struct cifs_sb_info *cifs_sb, const uuid_t *mount_id, struct cifs_ses *ses,
3278 struct cifs_ses **root_ses)
3279{
3280 if (ses) {
3281 spin_lock(&cifs_tcp_ses_lock);
3282 ses->ses_count++;
3283 cifs_dbg(FYI, "%s: new ses_count=%d\n", __func__, ses->ses_count);
3284 if (ses->tcon_ipc) {
3285 cifs_dbg(FYI, "%s: ipc tcon: %s\n", __func__, ses->tcon_ipc->treeName);
3286 ses->tcon_ipc->remap = cifs_remap(cifs_sb);
3287 }
3288 spin_unlock(&cifs_tcp_ses_lock);
3289 dfs_cache_add_refsrv_session(mount_id, ses);
3290 }
3291 *root_ses = ses;
3292}
3293
3294
3295static int next_dfs_prepath(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx,
3296 const unsigned int xid, struct TCP_Server_Info *server,
3297 struct cifs_tcon *tcon, char **dfs_path)
3298{
3299 char *path, *npath;
3300 int added_treename = is_tcon_dfs(tcon);
3301 int rc;
3302
3303 path = cifs_build_path_to_root(ctx, cifs_sb, tcon, added_treename);
3304 if (!path)
3305 return -ENOMEM;
3306
3307 rc = is_path_remote(cifs_sb, ctx, xid, server, tcon);
3308 if (rc == -EREMOTE) {
3309 struct smb3_fs_context v = {NULL};
3310
3311 if (added_treename) {
3312 rc = smb3_parse_devname(path, &v);
3313 if (rc)
3314 goto out;
3315 npath = build_unc_path_to_root(&v, cifs_sb, true);
3316 smb3_cleanup_fs_context_contents(&v);
3317 } else {
3318 v.UNC = ctx->UNC;
3319 v.prepath = path + 1;
3320 npath = build_unc_path_to_root(&v, cifs_sb, true);
3321 }
3322
3323 if (IS_ERR(npath)) {
3324 rc = PTR_ERR(npath);
3325 goto out;
3326 }
3327
3328 kfree(*dfs_path);
3329 *dfs_path = npath;
3330 rc = -EREMOTE;
3331 }
3332
3333out:
3334 kfree(path);
3335 return rc;
3336}
3337
3338
3339static int is_referral_server(const char *ref_path, struct cifs_tcon *tcon, bool *ref_server)
3340{
3341 int rc;
3342 struct dfs_info3_param ref = {0};
3343
3344 if (is_tcon_dfs(tcon)) {
3345 *ref_server = true;
3346 } else {
3347 cifs_dbg(FYI, "%s: ref_path=%s\n", __func__, ref_path);
3348
3349 rc = dfs_cache_noreq_find(ref_path, &ref, NULL);
3350 if (rc) {
3351 cifs_dbg(VFS, "%s: dfs_cache_noreq_find: failed (rc=%d)\n", __func__, rc);
3352 return rc;
3353 }
3354 cifs_dbg(FYI, "%s: ref.flags=0x%x\n", __func__, ref.flags);
3355
3356
3357
3358
3359 *ref_server = !!(ref.flags & DFSREF_REFERRAL_SERVER);
3360 free_dfs_info_param(&ref);
3361 }
3362 return 0;
3363}
3364
3365int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3366{
3367 int rc = 0;
3368 unsigned int xid;
3369 struct TCP_Server_Info *server = NULL;
3370 struct cifs_ses *ses = NULL, *root_ses = NULL;
3371 struct cifs_tcon *tcon = NULL;
3372 int count = 0;
3373 uuid_t mount_id = {0};
3374 char *ref_path = NULL, *full_path = NULL;
3375 char *oldmnt = NULL;
3376 bool ref_server = false;
3377
3378 rc = mount_get_conns(ctx, cifs_sb, &xid, &server, &ses, &tcon);
3379
3380
3381
3382
3383
3384
3385
3386 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) ||
3387 dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb), ctx->UNC + 1, NULL,
3388 NULL)) {
3389 if (rc)
3390 goto error;
3391
3392 rc = is_path_remote(cifs_sb, ctx, xid, server, tcon);
3393 if (!rc)
3394 goto out;
3395 if (rc != -EREMOTE)
3396 goto error;
3397 }
3398
3399
3400 ref_path = build_unc_path_to_root(ctx, cifs_sb, false);
3401 if (IS_ERR(ref_path)) {
3402 rc = PTR_ERR(ref_path);
3403 ref_path = NULL;
3404 goto error;
3405 }
3406
3407 uuid_gen(&mount_id);
3408 set_root_ses(cifs_sb, &mount_id, ses, &root_ses);
3409 do {
3410
3411 kfree(full_path);
3412 full_path = build_unc_path_to_root(ctx, cifs_sb, !!count);
3413 if (IS_ERR(full_path)) {
3414 rc = PTR_ERR(full_path);
3415 full_path = NULL;
3416 break;
3417 }
3418
3419 oldmnt = cifs_sb->ctx->mount_options;
3420 rc = expand_dfs_referral(xid, root_ses, ctx, cifs_sb, ref_path + 1);
3421 if (rc)
3422 break;
3423
3424 if (oldmnt != cifs_sb->ctx->mount_options) {
3425 mount_put_conns(cifs_sb, xid, server, ses, tcon);
3426 rc = mount_get_conns(ctx, cifs_sb, &xid, &server, &ses, &tcon);
3427 }
3428 if (rc && !server && !ses) {
3429
3430 rc = do_dfs_failover(ref_path + 1, full_path, cifs_sb, ctx, root_ses, &xid,
3431 &server, &ses, &tcon);
3432 }
3433 if (rc == -EACCES || rc == -EOPNOTSUPP || !server || !ses)
3434 break;
3435 if (!tcon)
3436 continue;
3437
3438
3439 rc = is_referral_server(ref_path + 1, tcon, &ref_server);
3440 if (rc)
3441 break;
3442 if (ref_server)
3443 set_root_ses(cifs_sb, &mount_id, ses, &root_ses);
3444
3445
3446 rc = next_dfs_prepath(cifs_sb, ctx, xid, server, tcon, &ref_path);
3447
3448 if (rc == -EREMOTE && ++count > MAX_NESTED_LINKS)
3449 rc = -ELOOP;
3450 } while (rc == -EREMOTE);
3451
3452 if (rc)
3453 goto error;
3454
3455 kfree(ref_path);
3456 ref_path = NULL;
3457
3458
3459
3460
3461
3462
3463
3464
3465 cifs_sb->origin_fullpath = kstrdup(full_path, GFP_KERNEL);
3466 if (!cifs_sb->origin_fullpath) {
3467 rc = -ENOMEM;
3468 goto error;
3469 }
3470 spin_lock(&cifs_tcp_ses_lock);
3471 tcon->dfs_path = full_path;
3472 full_path = NULL;
3473 tcon->remap = cifs_remap(cifs_sb);
3474 spin_unlock(&cifs_tcp_ses_lock);
3475
3476
3477
3478
3479
3480
3481 cifs_autodisable_serverino(cifs_sb);
3482
3483
3484
3485
3486 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3487 kfree(cifs_sb->prepath);
3488 cifs_sb->prepath = ctx->prepath;
3489 ctx->prepath = NULL;
3490 uuid_copy(&cifs_sb->dfs_mount_id, &mount_id);
3491
3492out:
3493 free_xid(xid);
3494 cifs_try_adding_channels(cifs_sb, ses);
3495 return mount_setup_tlink(cifs_sb, ses, tcon);
3496
3497error:
3498 kfree(ref_path);
3499 kfree(full_path);
3500 kfree(cifs_sb->origin_fullpath);
3501 dfs_cache_put_refsrv_sessions(&mount_id);
3502 mount_put_conns(cifs_sb, xid, server, ses, tcon);
3503 return rc;
3504}
3505#else
3506int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3507{
3508 int rc = 0;
3509 unsigned int xid;
3510 struct cifs_ses *ses;
3511 struct cifs_tcon *tcon;
3512 struct TCP_Server_Info *server;
3513
3514 rc = mount_get_conns(ctx, cifs_sb, &xid, &server, &ses, &tcon);
3515 if (rc)
3516 goto error;
3517
3518 if (tcon) {
3519 rc = is_path_remote(cifs_sb, ctx, xid, server, tcon);
3520 if (rc == -EREMOTE)
3521 rc = -EOPNOTSUPP;
3522 if (rc)
3523 goto error;
3524 }
3525
3526 free_xid(xid);
3527
3528 return mount_setup_tlink(cifs_sb, ses, tcon);
3529
3530error:
3531 mount_put_conns(cifs_sb, xid, server, ses, tcon);
3532 return rc;
3533}
3534#endif
3535
3536
3537
3538
3539int
3540CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
3541 const char *tree, struct cifs_tcon *tcon,
3542 const struct nls_table *nls_codepage)
3543{
3544 struct smb_hdr *smb_buffer;
3545 struct smb_hdr *smb_buffer_response;
3546 TCONX_REQ *pSMB;
3547 TCONX_RSP *pSMBr;
3548 unsigned char *bcc_ptr;
3549 int rc = 0;
3550 int length;
3551 __u16 bytes_left, count;
3552
3553 if (ses == NULL)
3554 return -EIO;
3555
3556 smb_buffer = cifs_buf_get();
3557 if (smb_buffer == NULL)
3558 return -ENOMEM;
3559
3560 smb_buffer_response = smb_buffer;
3561
3562 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3563 NULL , 4 );
3564
3565 smb_buffer->Mid = get_next_mid(ses->server);
3566 smb_buffer->Uid = ses->Suid;
3567 pSMB = (TCONX_REQ *) smb_buffer;
3568 pSMBr = (TCONX_RSP *) smb_buffer_response;
3569
3570 pSMB->AndXCommand = 0xFF;
3571 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
3572 bcc_ptr = &pSMB->Password[0];
3573 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
3574 pSMB->PasswordLength = cpu_to_le16(1);
3575 *bcc_ptr = 0;
3576 bcc_ptr++;
3577
3578 } else {
3579 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
3580
3581
3582
3583
3584
3585
3586#ifdef CONFIG_CIFS_WEAK_PW_HASH
3587 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
3588 (ses->sectype == LANMAN))
3589 calc_lanman_hash(tcon->password, ses->server->cryptkey,
3590 ses->server->sec_mode &
3591 SECMODE_PW_ENCRYPT ? true : false,
3592 bcc_ptr);
3593 else
3594#endif
3595 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
3596 bcc_ptr, nls_codepage);
3597 if (rc) {
3598 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
3599 __func__, rc);
3600 cifs_buf_release(smb_buffer);
3601 return rc;
3602 }
3603
3604 bcc_ptr += CIFS_AUTH_RESP_SIZE;
3605 if (ses->capabilities & CAP_UNICODE) {
3606
3607 *bcc_ptr = 0;
3608 bcc_ptr++;
3609 }
3610 }
3611
3612 if (ses->server->sign)
3613 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3614
3615 if (ses->capabilities & CAP_STATUS32) {
3616 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3617 }
3618 if (ses->capabilities & CAP_DFS) {
3619 smb_buffer->Flags2 |= SMBFLG2_DFS;
3620 }
3621 if (ses->capabilities & CAP_UNICODE) {
3622 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3623 length =
3624 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
3625 6 *
3626 ( + 256 ), nls_codepage);
3627 bcc_ptr += 2 * length;
3628 bcc_ptr += 2;
3629 } else {
3630 strcpy(bcc_ptr, tree);
3631 bcc_ptr += strlen(tree) + 1;
3632 }
3633 strcpy(bcc_ptr, "?????");
3634 bcc_ptr += strlen("?????");
3635 bcc_ptr += 1;
3636 count = bcc_ptr - &pSMB->Password[0];
3637 be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
3638 pSMB->ByteCount = cpu_to_le16(count);
3639
3640 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
3641 0);
3642
3643
3644 if (rc == 0) {
3645 bool is_unicode;
3646
3647 tcon->tidStatus = CifsGood;
3648 tcon->need_reconnect = false;
3649 tcon->tid = smb_buffer_response->Tid;
3650 bcc_ptr = pByteArea(smb_buffer_response);
3651 bytes_left = get_bcc(smb_buffer_response);
3652 length = strnlen(bcc_ptr, bytes_left - 2);
3653 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
3654 is_unicode = true;
3655 else
3656 is_unicode = false;
3657
3658
3659
3660 if (length == 3) {
3661 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3662 (bcc_ptr[2] == 'C')) {
3663 cifs_dbg(FYI, "IPC connection\n");
3664 tcon->ipc = true;
3665 tcon->pipe = true;
3666 }
3667 } else if (length == 2) {
3668 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3669
3670 cifs_dbg(FYI, "disk share connection\n");
3671 }
3672 }
3673 bcc_ptr += length + 1;
3674 bytes_left -= (length + 1);
3675 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
3676
3677
3678 kfree(tcon->nativeFileSystem);
3679 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
3680 bytes_left, is_unicode,
3681 nls_codepage);
3682
3683 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
3684
3685 if ((smb_buffer_response->WordCount == 3) ||
3686 (smb_buffer_response->WordCount == 7))
3687
3688 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
3689 else
3690 tcon->Flags = 0;
3691 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
3692 }
3693
3694 cifs_buf_release(smb_buffer);
3695 return rc;
3696}
3697
3698static void delayed_free(struct rcu_head *p)
3699{
3700 struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu);
3701
3702 unload_nls(cifs_sb->local_nls);
3703 smb3_cleanup_fs_context(cifs_sb->ctx);
3704 kfree(cifs_sb);
3705}
3706
3707void
3708cifs_umount(struct cifs_sb_info *cifs_sb)
3709{
3710 struct rb_root *root = &cifs_sb->tlink_tree;
3711 struct rb_node *node;
3712 struct tcon_link *tlink;
3713
3714 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
3715
3716 spin_lock(&cifs_sb->tlink_tree_lock);
3717 while ((node = rb_first(root))) {
3718 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3719 cifs_get_tlink(tlink);
3720 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3721 rb_erase(node, root);
3722
3723 spin_unlock(&cifs_sb->tlink_tree_lock);
3724 cifs_put_tlink(tlink);
3725 spin_lock(&cifs_sb->tlink_tree_lock);
3726 }
3727 spin_unlock(&cifs_sb->tlink_tree_lock);
3728
3729 kfree(cifs_sb->prepath);
3730#ifdef CONFIG_CIFS_DFS_UPCALL
3731 dfs_cache_put_refsrv_sessions(&cifs_sb->dfs_mount_id);
3732 kfree(cifs_sb->origin_fullpath);
3733#endif
3734 call_rcu(&cifs_sb->rcu, delayed_free);
3735}
3736
3737int
3738cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
3739{
3740 int rc = 0;
3741 struct TCP_Server_Info *server = cifs_ses_server(ses);
3742
3743 if (!server->ops->need_neg || !server->ops->negotiate)
3744 return -ENOSYS;
3745
3746
3747 if (!server->ops->need_neg(server))
3748 return 0;
3749
3750 rc = server->ops->negotiate(xid, ses);
3751 if (rc == 0) {
3752 spin_lock(&GlobalMid_Lock);
3753 if (server->tcpStatus == CifsNeedNegotiate)
3754 server->tcpStatus = CifsGood;
3755 else
3756 rc = -EHOSTDOWN;
3757 spin_unlock(&GlobalMid_Lock);
3758 }
3759
3760 return rc;
3761}
3762
3763int
3764cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
3765 struct nls_table *nls_info)
3766{
3767 int rc = -ENOSYS;
3768 struct TCP_Server_Info *server = cifs_ses_server(ses);
3769
3770 if (!ses->binding) {
3771 ses->capabilities = server->capabilities;
3772 if (!linuxExtEnabled)
3773 ses->capabilities &= (~server->vals->cap_unix);
3774
3775 if (ses->auth_key.response) {
3776 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
3777 ses->auth_key.response);
3778 kfree(ses->auth_key.response);
3779 ses->auth_key.response = NULL;
3780 ses->auth_key.len = 0;
3781 }
3782 }
3783
3784 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
3785 server->sec_mode, server->capabilities, server->timeAdj);
3786
3787 if (server->ops->sess_setup)
3788 rc = server->ops->sess_setup(xid, ses, nls_info);
3789
3790 if (rc)
3791 cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
3792
3793 return rc;
3794}
3795
3796static int
3797cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses)
3798{
3799 ctx->sectype = ses->sectype;
3800
3801
3802 if (ctx->sectype == Kerberos)
3803 return 0;
3804
3805 return cifs_set_cifscreds(ctx, ses);
3806}
3807
3808static struct cifs_tcon *
3809cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
3810{
3811 int rc;
3812 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
3813 struct cifs_ses *ses;
3814 struct cifs_tcon *tcon = NULL;
3815 struct smb3_fs_context *ctx;
3816
3817 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3818 if (ctx == NULL)
3819 return ERR_PTR(-ENOMEM);
3820
3821 ctx->local_nls = cifs_sb->local_nls;
3822 ctx->linux_uid = fsuid;
3823 ctx->cred_uid = fsuid;
3824 ctx->UNC = master_tcon->treeName;
3825 ctx->retry = master_tcon->retry;
3826 ctx->nocase = master_tcon->nocase;
3827 ctx->nohandlecache = master_tcon->nohandlecache;
3828 ctx->local_lease = master_tcon->local_lease;
3829 ctx->no_lease = master_tcon->no_lease;
3830 ctx->resilient = master_tcon->use_resilient;
3831 ctx->persistent = master_tcon->use_persistent;
3832 ctx->handle_timeout = master_tcon->handle_timeout;
3833 ctx->no_linux_ext = !master_tcon->unix_ext;
3834 ctx->linux_ext = master_tcon->posix_extensions;
3835 ctx->sectype = master_tcon->ses->sectype;
3836 ctx->sign = master_tcon->ses->sign;
3837 ctx->seal = master_tcon->seal;
3838#ifdef CONFIG_CIFS_SWN_UPCALL
3839 ctx->witness = master_tcon->use_witness;
3840#endif
3841
3842 rc = cifs_set_vol_auth(ctx, master_tcon->ses);
3843 if (rc) {
3844 tcon = ERR_PTR(rc);
3845 goto out;
3846 }
3847
3848
3849 spin_lock(&cifs_tcp_ses_lock);
3850 ++master_tcon->ses->server->srv_count;
3851 spin_unlock(&cifs_tcp_ses_lock);
3852
3853 ses = cifs_get_smb_ses(master_tcon->ses->server, ctx);
3854 if (IS_ERR(ses)) {
3855 tcon = (struct cifs_tcon *)ses;
3856 cifs_put_tcp_session(master_tcon->ses->server, 0);
3857 goto out;
3858 }
3859
3860 tcon = cifs_get_tcon(ses, ctx);
3861 if (IS_ERR(tcon)) {
3862 cifs_put_smb_ses(ses);
3863 goto out;
3864 }
3865
3866 if (cap_unix(ses))
3867 reset_cifs_unix_caps(0, tcon, NULL, ctx);
3868
3869out:
3870 kfree(ctx->username);
3871 kfree_sensitive(ctx->password);
3872 kfree(ctx);
3873
3874 return tcon;
3875}
3876
3877struct cifs_tcon *
3878cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
3879{
3880 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
3881}
3882
3883
3884static struct tcon_link *
3885tlink_rb_search(struct rb_root *root, kuid_t uid)
3886{
3887 struct rb_node *node = root->rb_node;
3888 struct tcon_link *tlink;
3889
3890 while (node) {
3891 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3892
3893 if (uid_gt(tlink->tl_uid, uid))
3894 node = node->rb_left;
3895 else if (uid_lt(tlink->tl_uid, uid))
3896 node = node->rb_right;
3897 else
3898 return tlink;
3899 }
3900 return NULL;
3901}
3902
3903
3904static void
3905tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
3906{
3907 struct rb_node **new = &(root->rb_node), *parent = NULL;
3908 struct tcon_link *tlink;
3909
3910 while (*new) {
3911 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
3912 parent = *new;
3913
3914 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
3915 new = &((*new)->rb_left);
3916 else
3917 new = &((*new)->rb_right);
3918 }
3919
3920 rb_link_node(&new_tlink->tl_rbnode, parent, new);
3921 rb_insert_color(&new_tlink->tl_rbnode, root);
3922}
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940struct tcon_link *
3941cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
3942{
3943 int ret;
3944 kuid_t fsuid = current_fsuid();
3945 struct tcon_link *tlink, *newtlink;
3946
3947 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
3948 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3949
3950 spin_lock(&cifs_sb->tlink_tree_lock);
3951 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
3952 if (tlink)
3953 cifs_get_tlink(tlink);
3954 spin_unlock(&cifs_sb->tlink_tree_lock);
3955
3956 if (tlink == NULL) {
3957 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3958 if (newtlink == NULL)
3959 return ERR_PTR(-ENOMEM);
3960 newtlink->tl_uid = fsuid;
3961 newtlink->tl_tcon = ERR_PTR(-EACCES);
3962 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
3963 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
3964 cifs_get_tlink(newtlink);
3965
3966 spin_lock(&cifs_sb->tlink_tree_lock);
3967
3968 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
3969 if (tlink) {
3970 cifs_get_tlink(tlink);
3971 spin_unlock(&cifs_sb->tlink_tree_lock);
3972 kfree(newtlink);
3973 goto wait_for_construction;
3974 }
3975 tlink = newtlink;
3976 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3977 spin_unlock(&cifs_sb->tlink_tree_lock);
3978 } else {
3979wait_for_construction:
3980 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
3981 TASK_INTERRUPTIBLE);
3982 if (ret) {
3983 cifs_put_tlink(tlink);
3984 return ERR_PTR(-ERESTARTSYS);
3985 }
3986
3987
3988 if (!IS_ERR(tlink->tl_tcon))
3989 return tlink;
3990
3991
3992 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
3993 cifs_put_tlink(tlink);
3994 return ERR_PTR(-EACCES);
3995 }
3996
3997 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
3998 goto wait_for_construction;
3999 }
4000
4001 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4002 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4003 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4004
4005 if (IS_ERR(tlink->tl_tcon)) {
4006 cifs_put_tlink(tlink);
4007 return ERR_PTR(-EACCES);
4008 }
4009
4010 return tlink;
4011}
4012
4013
4014
4015
4016
4017static void
4018cifs_prune_tlinks(struct work_struct *work)
4019{
4020 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4021 prune_tlinks.work);
4022 struct rb_root *root = &cifs_sb->tlink_tree;
4023 struct rb_node *node;
4024 struct rb_node *tmp;
4025 struct tcon_link *tlink;
4026
4027
4028
4029
4030
4031
4032
4033
4034 spin_lock(&cifs_sb->tlink_tree_lock);
4035 node = rb_first(root);
4036 while (node != NULL) {
4037 tmp = node;
4038 node = rb_next(tmp);
4039 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4040
4041 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4042 atomic_read(&tlink->tl_count) != 0 ||
4043 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4044 continue;
4045
4046 cifs_get_tlink(tlink);
4047 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4048 rb_erase(tmp, root);
4049
4050 spin_unlock(&cifs_sb->tlink_tree_lock);
4051 cifs_put_tlink(tlink);
4052 spin_lock(&cifs_sb->tlink_tree_lock);
4053 }
4054 spin_unlock(&cifs_sb->tlink_tree_lock);
4055
4056 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
4057 TLINK_IDLE_EXPIRE);
4058}
4059
4060#ifdef CONFIG_CIFS_DFS_UPCALL
4061int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4062{
4063 int rc;
4064 struct TCP_Server_Info *server = tcon->ses->server;
4065 const struct smb_version_operations *ops = server->ops;
4066 struct dfs_cache_tgt_list tl;
4067 struct dfs_cache_tgt_iterator *it = NULL;
4068 char *tree;
4069 const char *tcp_host;
4070 size_t tcp_host_len;
4071 const char *dfs_host;
4072 size_t dfs_host_len;
4073 char *share = NULL, *prefix = NULL;
4074 struct dfs_info3_param ref = {0};
4075 bool isroot;
4076
4077 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
4078 if (!tree)
4079 return -ENOMEM;
4080
4081 if (!tcon->dfs_path) {
4082 if (tcon->ipc) {
4083 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
4084 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
4085 } else {
4086 rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
4087 }
4088 goto out;
4089 }
4090
4091 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, &ref, &tl);
4092 if (rc)
4093 goto out;
4094 isroot = ref.server_type == DFS_TYPE_ROOT;
4095 free_dfs_info_param(&ref);
4096
4097 extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len);
4098
4099 for (it = dfs_cache_get_tgt_iterator(&tl); it; it = dfs_cache_get_next_tgt(&tl, it)) {
4100 bool target_match;
4101
4102 kfree(share);
4103 kfree(prefix);
4104 share = NULL;
4105 prefix = NULL;
4106
4107 rc = dfs_cache_get_tgt_share(tcon->dfs_path + 1, it, &share, &prefix);
4108 if (rc) {
4109 cifs_dbg(VFS, "%s: failed to parse target share %d\n",
4110 __func__, rc);
4111 continue;
4112 }
4113
4114 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
4115
4116 if (dfs_host_len != tcp_host_len
4117 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
4118 cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n", __func__, (int)dfs_host_len,
4119 dfs_host, (int)tcp_host_len, tcp_host);
4120
4121 rc = match_target_ip(server, dfs_host, dfs_host_len, &target_match);
4122 if (rc) {
4123 cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
4124 break;
4125 }
4126
4127 if (!target_match) {
4128 cifs_dbg(FYI, "%s: skipping target\n", __func__);
4129 continue;
4130 }
4131 }
4132
4133 if (tcon->ipc) {
4134 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", share);
4135 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
4136 } else {
4137 scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
4138 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
4139
4140 if (!rc && !isroot) {
4141 rc = update_super_prepath(tcon, prefix);
4142 break;
4143 }
4144 }
4145 if (rc == -EREMOTE)
4146 break;
4147 }
4148
4149 kfree(share);
4150 kfree(prefix);
4151
4152 if (!rc) {
4153 if (it)
4154 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1, it);
4155 else
4156 rc = -ENOENT;
4157 }
4158 dfs_cache_free_tgts(&tl);
4159out:
4160 kfree(tree);
4161 return rc;
4162}
4163#else
4164int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4165{
4166 const struct smb_version_operations *ops = tcon->ses->server->ops;
4167
4168 return ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
4169}
4170#endif
4171