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