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