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