blob: 7e85070d010f4ee006a4c64178e9f17bd274a76c [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * fs/cifs/connect.c
3 *
4 * Copyright (C) International Business Machines Corp., 2002,2011
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21#include <linux/fs.h>
22#include <linux/net.h>
23#include <linux/string.h>
24#include <linux/sched/signal.h>
25#include <linux/list.h>
26#include <linux/wait.h>
27#include <linux/slab.h>
28#include <linux/pagemap.h>
29#include <linux/ctype.h>
30#include <linux/utsname.h>
31#include <linux/mempool.h>
32#include <linux/delay.h>
33#include <linux/completion.h>
34#include <linux/kthread.h>
35#include <linux/pagevec.h>
36#include <linux/freezer.h>
37#include <linux/namei.h>
38#include <linux/uuid.h>
39#include <linux/uaccess.h>
40#include <asm/processor.h>
41#include <linux/inet.h>
42#include <linux/module.h>
43#include <keys/user-type.h>
44#include <net/ipv6.h>
45#include <linux/parser.h>
46#include <linux/bvec.h>
47#include "cifspdu.h"
48#include "cifsglob.h"
49#include "cifsproto.h"
50#include "cifs_unicode.h"
51#include "cifs_debug.h"
52#include "cifs_fs_sb.h"
53#include "dns_resolve.h"
54#include "ntlmssp.h"
55#include "nterr.h"
56#include "rfc1002pdu.h"
57#include "fscache.h"
58#include "smb2proto.h"
59#include "smbdirect.h"
60
61extern mempool_t *cifs_req_poolp;
62extern bool disable_legacy_dialects;
63
64/* FIXME: should these be tunable? */
65#define TLINK_ERROR_EXPIRE (1 * HZ)
66#define TLINK_IDLE_EXPIRE (600 * HZ)
67
68enum {
69 /* Mount options that take no arguments */
70 Opt_user_xattr, Opt_nouser_xattr,
71 Opt_forceuid, Opt_noforceuid,
72 Opt_forcegid, Opt_noforcegid,
73 Opt_noblocksend, Opt_noautotune, Opt_nolease,
74 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
75 Opt_mapposix, Opt_nomapposix,
76 Opt_mapchars, Opt_nomapchars, Opt_sfu,
77 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
78 Opt_noposixpaths, Opt_nounix, Opt_unix,
79 Opt_nocase,
80 Opt_brl, Opt_nobrl,
81 Opt_handlecache, Opt_nohandlecache,
82 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
83 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
84 Opt_nohard, Opt_nosoft,
85 Opt_nointr, Opt_intr,
86 Opt_nostrictsync, Opt_strictsync,
87 Opt_serverino, Opt_noserverino,
88 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
89 Opt_acl, Opt_noacl, Opt_locallease,
90 Opt_sign, Opt_seal, Opt_noac,
91 Opt_fsc, Opt_mfsymlinks,
92 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
93 Opt_persistent, Opt_nopersistent,
94 Opt_resilient, Opt_noresilient,
95 Opt_domainauto, Opt_rdma,
96
97 /* Mount options which take numeric value */
98 Opt_backupuid, Opt_backupgid, Opt_uid,
99 Opt_cruid, Opt_gid, Opt_file_mode,
100 Opt_dirmode, Opt_port,
101 Opt_rsize, Opt_wsize, Opt_actimeo,
102 Opt_echo_interval, Opt_max_credits,
103 Opt_snapshot,
104
105 /* Mount options which take string value */
106 Opt_user, Opt_pass, Opt_ip,
107 Opt_domain, Opt_srcaddr, Opt_iocharset,
108 Opt_netbiosname, Opt_servern,
109 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
110
111 /* Mount options to be ignored */
112 Opt_ignore,
113
114 /* Options which could be blank */
115 Opt_blank_pass,
116 Opt_blank_user,
117 Opt_blank_ip,
118
119 Opt_err
120};
121
122static const match_table_t cifs_mount_option_tokens = {
123
124 { Opt_user_xattr, "user_xattr" },
125 { Opt_nouser_xattr, "nouser_xattr" },
126 { Opt_forceuid, "forceuid" },
127 { Opt_noforceuid, "noforceuid" },
128 { Opt_forcegid, "forcegid" },
129 { Opt_noforcegid, "noforcegid" },
130 { Opt_noblocksend, "noblocksend" },
131 { Opt_noautotune, "noautotune" },
132 { Opt_nolease, "nolease" },
133 { Opt_hard, "hard" },
134 { Opt_soft, "soft" },
135 { Opt_perm, "perm" },
136 { Opt_noperm, "noperm" },
137 { Opt_mapchars, "mapchars" }, /* SFU style */
138 { Opt_nomapchars, "nomapchars" },
139 { Opt_mapposix, "mapposix" }, /* SFM style */
140 { Opt_nomapposix, "nomapposix" },
141 { Opt_sfu, "sfu" },
142 { Opt_nosfu, "nosfu" },
143 { Opt_nodfs, "nodfs" },
144 { Opt_posixpaths, "posixpaths" },
145 { Opt_noposixpaths, "noposixpaths" },
146 { Opt_nounix, "nounix" },
147 { Opt_nounix, "nolinux" },
148 { Opt_nounix, "noposix" },
149 { Opt_unix, "unix" },
150 { Opt_unix, "linux" },
151 { Opt_unix, "posix" },
152 { Opt_nocase, "nocase" },
153 { Opt_nocase, "ignorecase" },
154 { Opt_brl, "brl" },
155 { Opt_nobrl, "nobrl" },
156 { Opt_handlecache, "handlecache" },
157 { Opt_nohandlecache, "nohandlecache" },
158 { Opt_nobrl, "nolock" },
159 { Opt_forcemandatorylock, "forcemandatorylock" },
160 { Opt_forcemandatorylock, "forcemand" },
161 { Opt_setuids, "setuids" },
162 { Opt_nosetuids, "nosetuids" },
163 { Opt_setuidfromacl, "idsfromsid" },
164 { Opt_dynperm, "dynperm" },
165 { Opt_nodynperm, "nodynperm" },
166 { Opt_nohard, "nohard" },
167 { Opt_nosoft, "nosoft" },
168 { Opt_nointr, "nointr" },
169 { Opt_intr, "intr" },
170 { Opt_nostrictsync, "nostrictsync" },
171 { Opt_strictsync, "strictsync" },
172 { Opt_serverino, "serverino" },
173 { Opt_noserverino, "noserverino" },
174 { Opt_rwpidforward, "rwpidforward" },
175 { Opt_cifsacl, "cifsacl" },
176 { Opt_nocifsacl, "nocifsacl" },
177 { Opt_acl, "acl" },
178 { Opt_noacl, "noacl" },
179 { Opt_locallease, "locallease" },
180 { Opt_sign, "sign" },
181 { Opt_seal, "seal" },
182 { Opt_noac, "noac" },
183 { Opt_fsc, "fsc" },
184 { Opt_mfsymlinks, "mfsymlinks" },
185 { Opt_multiuser, "multiuser" },
186 { Opt_sloppy, "sloppy" },
187 { Opt_nosharesock, "nosharesock" },
188 { Opt_persistent, "persistenthandles"},
189 { Opt_nopersistent, "nopersistenthandles"},
190 { Opt_resilient, "resilienthandles"},
191 { Opt_noresilient, "noresilienthandles"},
192 { Opt_domainauto, "domainauto"},
193 { Opt_rdma, "rdma"},
194
195 { Opt_backupuid, "backupuid=%s" },
196 { Opt_backupgid, "backupgid=%s" },
197 { Opt_uid, "uid=%s" },
198 { Opt_cruid, "cruid=%s" },
199 { Opt_gid, "gid=%s" },
200 { Opt_file_mode, "file_mode=%s" },
201 { Opt_dirmode, "dirmode=%s" },
202 { Opt_dirmode, "dir_mode=%s" },
203 { Opt_port, "port=%s" },
204 { Opt_rsize, "rsize=%s" },
205 { Opt_wsize, "wsize=%s" },
206 { Opt_actimeo, "actimeo=%s" },
207 { Opt_echo_interval, "echo_interval=%s" },
208 { Opt_max_credits, "max_credits=%s" },
209 { Opt_snapshot, "snapshot=%s" },
210
211 { Opt_blank_user, "user=" },
212 { Opt_blank_user, "username=" },
213 { Opt_user, "user=%s" },
214 { Opt_user, "username=%s" },
215 { Opt_blank_pass, "pass=" },
216 { Opt_blank_pass, "password=" },
217 { Opt_pass, "pass=%s" },
218 { Opt_pass, "password=%s" },
219 { Opt_blank_ip, "ip=" },
220 { Opt_blank_ip, "addr=" },
221 { Opt_ip, "ip=%s" },
222 { Opt_ip, "addr=%s" },
223 { Opt_ignore, "unc=%s" },
224 { Opt_ignore, "target=%s" },
225 { Opt_ignore, "path=%s" },
226 { Opt_domain, "dom=%s" },
227 { Opt_domain, "domain=%s" },
228 { Opt_domain, "workgroup=%s" },
229 { Opt_srcaddr, "srcaddr=%s" },
230 { Opt_ignore, "prefixpath=%s" },
231 { Opt_iocharset, "iocharset=%s" },
232 { Opt_netbiosname, "netbiosname=%s" },
233 { Opt_servern, "servern=%s" },
234 { Opt_ver, "ver=%s" },
235 { Opt_vers, "vers=%s" },
236 { Opt_sec, "sec=%s" },
237 { Opt_cache, "cache=%s" },
238
239 { Opt_ignore, "cred" },
240 { Opt_ignore, "credentials" },
241 { Opt_ignore, "cred=%s" },
242 { Opt_ignore, "credentials=%s" },
243 { Opt_ignore, "guest" },
244 { Opt_ignore, "rw" },
245 { Opt_ignore, "ro" },
246 { Opt_ignore, "suid" },
247 { Opt_ignore, "nosuid" },
248 { Opt_ignore, "exec" },
249 { Opt_ignore, "noexec" },
250 { Opt_ignore, "nodev" },
251 { Opt_ignore, "noauto" },
252 { Opt_ignore, "dev" },
253 { Opt_ignore, "mand" },
254 { Opt_ignore, "nomand" },
255 { Opt_ignore, "_netdev" },
256
257 { Opt_err, NULL }
258};
259
260enum {
261 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
262 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
263 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
264 Opt_sec_ntlmv2i, Opt_sec_lanman,
265 Opt_sec_none,
266
267 Opt_sec_err
268};
269
270static const match_table_t cifs_secflavor_tokens = {
271 { Opt_sec_krb5, "krb5" },
272 { Opt_sec_krb5i, "krb5i" },
273 { Opt_sec_krb5p, "krb5p" },
274 { Opt_sec_ntlmsspi, "ntlmsspi" },
275 { Opt_sec_ntlmssp, "ntlmssp" },
276 { Opt_ntlm, "ntlm" },
277 { Opt_sec_ntlmi, "ntlmi" },
278 { Opt_sec_ntlmv2, "nontlm" },
279 { Opt_sec_ntlmv2, "ntlmv2" },
280 { Opt_sec_ntlmv2i, "ntlmv2i" },
281 { Opt_sec_lanman, "lanman" },
282 { Opt_sec_none, "none" },
283
284 { Opt_sec_err, NULL }
285};
286
287/* cache flavors */
288enum {
289 Opt_cache_loose,
290 Opt_cache_strict,
291 Opt_cache_none,
292 Opt_cache_err
293};
294
295static const match_table_t cifs_cacheflavor_tokens = {
296 { Opt_cache_loose, "loose" },
297 { Opt_cache_strict, "strict" },
298 { Opt_cache_none, "none" },
299 { Opt_cache_err, NULL }
300};
301
302static const match_table_t cifs_smb_version_tokens = {
303 { Smb_1, SMB1_VERSION_STRING },
304 { Smb_20, SMB20_VERSION_STRING},
305 { Smb_21, SMB21_VERSION_STRING },
306 { Smb_30, SMB30_VERSION_STRING },
307 { Smb_302, SMB302_VERSION_STRING },
308 { Smb_311, SMB311_VERSION_STRING },
309 { Smb_311, ALT_SMB311_VERSION_STRING },
310 { Smb_3any, SMB3ANY_VERSION_STRING },
311 { Smb_default, SMBDEFAULT_VERSION_STRING },
312 { Smb_version_err, NULL }
313};
314
315static int ip_connect(struct TCP_Server_Info *server);
316static int generic_ip_connect(struct TCP_Server_Info *server);
317static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
318static void cifs_prune_tlinks(struct work_struct *work);
319static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
320 const char *devname, bool is_smb3);
321
322/*
323 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
324 * get their ip addresses changed at some point.
325 *
326 * This should be called with server->srv_mutex held.
327 */
328#ifdef CONFIG_CIFS_DFS_UPCALL
329static int reconn_set_ipaddr(struct TCP_Server_Info *server)
330{
331 int rc;
332 int len;
333 char *unc, *ipaddr = NULL;
334
335 if (!server->hostname)
336 return -EINVAL;
337
338 len = strlen(server->hostname) + 3;
339
340 unc = kmalloc(len, GFP_KERNEL);
341 if (!unc) {
342 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
343 return -ENOMEM;
344 }
345 snprintf(unc, len, "\\\\%s", server->hostname);
346
347 rc = dns_resolve_server_name_to_ip(unc, &ipaddr);
348 kfree(unc);
349
350 if (rc < 0) {
351 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
352 __func__, server->hostname, rc);
353 return rc;
354 }
355
356 rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
357 strlen(ipaddr));
358 kfree(ipaddr);
359
360 return !rc ? -1 : 0;
361}
362#else
363static inline int reconn_set_ipaddr(struct TCP_Server_Info *server)
364{
365 return 0;
366}
367#endif
368
369/*
370 * cifs tcp session reconnection
371 *
372 * mark tcp session as reconnecting so temporarily locked
373 * mark all smb sessions as reconnecting for tcp session
374 * reconnect tcp session
375 * wake up waiters on reconnection? - (not needed currently)
376 */
377int
378cifs_reconnect(struct TCP_Server_Info *server)
379{
380 int rc = 0;
381 struct list_head *tmp, *tmp2;
382 struct cifs_ses *ses;
383 struct cifs_tcon *tcon;
384 struct mid_q_entry *mid_entry;
385 struct list_head retry_list;
386
387 spin_lock(&GlobalMid_Lock);
388 if (server->tcpStatus == CifsExiting) {
389 /* the demux thread will exit normally
390 next time through the loop */
391 spin_unlock(&GlobalMid_Lock);
392 return rc;
393 } else
394 server->tcpStatus = CifsNeedReconnect;
395 spin_unlock(&GlobalMid_Lock);
396 server->maxBuf = 0;
397 server->max_read = 0;
398
399 cifs_dbg(FYI, "Reconnecting tcp session\n");
400 trace_smb3_reconnect(server->CurrentMid, server->hostname);
401
402 /* before reconnecting the tcp session, mark the smb session (uid)
403 and the tid bad so they are not used until reconnected */
404 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
405 __func__);
406 spin_lock(&cifs_tcp_ses_lock);
407 list_for_each(tmp, &server->smb_ses_list) {
408 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
409 ses->need_reconnect = true;
410 list_for_each(tmp2, &ses->tcon_list) {
411 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
412 tcon->need_reconnect = true;
413 }
414 if (ses->tcon_ipc)
415 ses->tcon_ipc->need_reconnect = true;
416 }
417 spin_unlock(&cifs_tcp_ses_lock);
418
419 /* do not want to be sending data on a socket we are freeing */
420 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
421 mutex_lock(&server->srv_mutex);
422 if (server->ssocket) {
423 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
424 server->ssocket->state, server->ssocket->flags);
425 kernel_sock_shutdown(server->ssocket, SHUT_WR);
426 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
427 server->ssocket->state, server->ssocket->flags);
428 sock_release(server->ssocket);
429 server->ssocket = NULL;
430 }
431 server->sequence_number = 0;
432 server->session_estab = false;
433 kfree(server->session_key.response);
434 server->session_key.response = NULL;
435 server->session_key.len = 0;
436 server->lstrp = jiffies;
437
438 /* mark submitted MIDs for retry and issue callback */
439 INIT_LIST_HEAD(&retry_list);
440 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
441 spin_lock(&GlobalMid_Lock);
442 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
443 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
444 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
445 mid_entry->mid_state = MID_RETRY_NEEDED;
446 list_move(&mid_entry->qhead, &retry_list);
447 }
448 spin_unlock(&GlobalMid_Lock);
449 mutex_unlock(&server->srv_mutex);
450
451 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
452 list_for_each_safe(tmp, tmp2, &retry_list) {
453 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
454 list_del_init(&mid_entry->qhead);
455 mid_entry->callback(mid_entry);
456 }
457
458 do {
459 try_to_freeze();
460
461 /* we should try only the port we connected to before */
462 mutex_lock(&server->srv_mutex);
463 if (cifs_rdma_enabled(server))
464 rc = smbd_reconnect(server);
465 else
466 rc = generic_ip_connect(server);
467 if (rc) {
468 cifs_dbg(FYI, "reconnect error %d\n", rc);
469 rc = reconn_set_ipaddr(server);
470 if (rc) {
471 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
472 __func__, rc);
473 }
474 mutex_unlock(&server->srv_mutex);
475 msleep(3000);
476 } else {
477 atomic_inc(&tcpSesReconnectCount);
478 spin_lock(&GlobalMid_Lock);
479 if (server->tcpStatus != CifsExiting)
480 server->tcpStatus = CifsNeedNegotiate;
481 spin_unlock(&GlobalMid_Lock);
482 mutex_unlock(&server->srv_mutex);
483 }
484 } while (server->tcpStatus == CifsNeedReconnect);
485
486 if (server->tcpStatus == CifsNeedNegotiate)
487 mod_delayed_work(cifsiod_wq, &server->echo, 0);
488
489 return rc;
490}
491
492static void
493cifs_echo_request(struct work_struct *work)
494{
495 int rc;
496 struct TCP_Server_Info *server = container_of(work,
497 struct TCP_Server_Info, echo.work);
498 unsigned long echo_interval;
499
500 /*
501 * If we need to renegotiate, set echo interval to zero to
502 * immediately call echo service where we can renegotiate.
503 */
504 if (server->tcpStatus == CifsNeedNegotiate)
505 echo_interval = 0;
506 else
507 echo_interval = server->echo_interval;
508
509 /*
510 * We cannot send an echo if it is disabled.
511 * Also, no need to ping if we got a response recently.
512 */
513
514 if (server->tcpStatus == CifsNeedReconnect ||
515 server->tcpStatus == CifsExiting ||
516 server->tcpStatus == CifsNew ||
517 (server->ops->can_echo && !server->ops->can_echo(server)) ||
518 time_before(jiffies, server->lstrp + echo_interval - HZ))
519 goto requeue_echo;
520
521 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
522 if (rc)
523 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
524 server->hostname);
525
526requeue_echo:
527 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
528}
529
530static bool
531allocate_buffers(struct TCP_Server_Info *server)
532{
533 if (!server->bigbuf) {
534 server->bigbuf = (char *)cifs_buf_get();
535 if (!server->bigbuf) {
536 cifs_dbg(VFS, "No memory for large SMB response\n");
537 msleep(3000);
538 /* retry will check if exiting */
539 return false;
540 }
541 } else if (server->large_buf) {
542 /* we are reusing a dirty large buf, clear its start */
543 memset(server->bigbuf, 0, HEADER_SIZE(server));
544 }
545
546 if (!server->smallbuf) {
547 server->smallbuf = (char *)cifs_small_buf_get();
548 if (!server->smallbuf) {
549 cifs_dbg(VFS, "No memory for SMB response\n");
550 msleep(1000);
551 /* retry will check if exiting */
552 return false;
553 }
554 /* beginning of smb buffer is cleared in our buf_get */
555 } else {
556 /* if existing small buf clear beginning */
557 memset(server->smallbuf, 0, HEADER_SIZE(server));
558 }
559
560 return true;
561}
562
563static bool
564server_unresponsive(struct TCP_Server_Info *server)
565{
566 /*
567 * We need to wait 3 echo intervals to make sure we handle such
568 * situations right:
569 * 1s client sends a normal SMB request
570 * 3s client gets a response
571 * 30s echo workqueue job pops, and decides we got a response recently
572 * and don't need to send another
573 * ...
574 * 65s kernel_recvmsg times out, and we see that we haven't gotten
575 * a response in >60s.
576 */
577 if ((server->tcpStatus == CifsGood ||
578 server->tcpStatus == CifsNeedNegotiate) &&
579 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
580 cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
581 server->hostname, (3 * server->echo_interval) / HZ);
582 cifs_reconnect(server);
583 wake_up(&server->response_q);
584 return true;
585 }
586
587 return false;
588}
589
590static inline bool
591zero_credits(struct TCP_Server_Info *server)
592{
593 int val;
594
595 spin_lock(&server->req_lock);
596 val = server->credits + server->echo_credits + server->oplock_credits;
597 if (server->in_flight == 0 && val == 0) {
598 spin_unlock(&server->req_lock);
599 return true;
600 }
601 spin_unlock(&server->req_lock);
602 return false;
603}
604
605static int
606cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
607{
608 int length = 0;
609 int total_read;
610
611 smb_msg->msg_control = NULL;
612 smb_msg->msg_controllen = 0;
613
614 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
615 try_to_freeze();
616
617 /* reconnect if no credits and no requests in flight */
618 if (zero_credits(server)) {
619 cifs_reconnect(server);
620 return -ECONNABORTED;
621 }
622
623 if (server_unresponsive(server))
624 return -ECONNABORTED;
625 if (cifs_rdma_enabled(server) && server->smbd_conn)
626 length = smbd_recv(server->smbd_conn, smb_msg);
627 else
628 length = sock_recvmsg(server->ssocket, smb_msg, 0);
629
630 if (server->tcpStatus == CifsExiting)
631 return -ESHUTDOWN;
632
633 if (server->tcpStatus == CifsNeedReconnect) {
634 cifs_reconnect(server);
635 return -ECONNABORTED;
636 }
637
638 if (length == -ERESTARTSYS ||
639 length == -EAGAIN ||
640 length == -EINTR) {
641 /*
642 * Minimum sleep to prevent looping, allowing socket
643 * to clear and app threads to set tcpStatus
644 * CifsNeedReconnect if server hung.
645 */
646 usleep_range(1000, 2000);
647 length = 0;
648 continue;
649 }
650
651 if (length <= 0) {
652 cifs_dbg(FYI, "Received no data or error: %d\n", length);
653 cifs_reconnect(server);
654 return -ECONNABORTED;
655 }
656 }
657 return total_read;
658}
659
660int
661cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
662 unsigned int to_read)
663{
664 struct msghdr smb_msg;
665 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
666 iov_iter_kvec(&smb_msg.msg_iter, READ | ITER_KVEC, &iov, 1, to_read);
667
668 return cifs_readv_from_socket(server, &smb_msg);
669}
670
671int
672cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
673 unsigned int page_offset, unsigned int to_read)
674{
675 struct msghdr smb_msg;
676 struct bio_vec bv = {
677 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
678 iov_iter_bvec(&smb_msg.msg_iter, READ | ITER_BVEC, &bv, 1, to_read);
679 return cifs_readv_from_socket(server, &smb_msg);
680}
681
682static bool
683is_smb_response(struct TCP_Server_Info *server, unsigned char type)
684{
685 /*
686 * The first byte big endian of the length field,
687 * is actually not part of the length but the type
688 * with the most common, zero, as regular data.
689 */
690 switch (type) {
691 case RFC1002_SESSION_MESSAGE:
692 /* Regular SMB response */
693 return true;
694 case RFC1002_SESSION_KEEP_ALIVE:
695 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
696 break;
697 case RFC1002_POSITIVE_SESSION_RESPONSE:
698 cifs_dbg(FYI, "RFC 1002 positive session response\n");
699 break;
700 case RFC1002_NEGATIVE_SESSION_RESPONSE:
701 /*
702 * We get this from Windows 98 instead of an error on
703 * SMB negprot response.
704 */
705 cifs_dbg(FYI, "RFC 1002 negative session response\n");
706 /* give server a second to clean up */
707 msleep(1000);
708 /*
709 * Always try 445 first on reconnect since we get NACK
710 * on some if we ever connected to port 139 (the NACK
711 * is since we do not begin with RFC1001 session
712 * initialize frame).
713 */
714 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
715 cifs_reconnect(server);
716 wake_up(&server->response_q);
717 break;
718 default:
719 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
720 cifs_reconnect(server);
721 }
722
723 return false;
724}
725
726void
727dequeue_mid(struct mid_q_entry *mid, bool malformed)
728{
729#ifdef CONFIG_CIFS_STATS2
730 mid->when_received = jiffies;
731#endif
732 spin_lock(&GlobalMid_Lock);
733 if (!malformed)
734 mid->mid_state = MID_RESPONSE_RECEIVED;
735 else
736 mid->mid_state = MID_RESPONSE_MALFORMED;
737 /*
738 * Trying to handle/dequeue a mid after the send_recv()
739 * function has finished processing it is a bug.
740 */
741 if (mid->mid_flags & MID_DELETED)
742 printk_once(KERN_WARNING
743 "trying to dequeue a deleted mid\n");
744 else
745 list_del_init(&mid->qhead);
746 spin_unlock(&GlobalMid_Lock);
747}
748
749static void
750handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
751 char *buf, int malformed)
752{
753 if (server->ops->check_trans2 &&
754 server->ops->check_trans2(mid, server, buf, malformed))
755 return;
756 mid->resp_buf = buf;
757 mid->large_buf = server->large_buf;
758 /* Was previous buf put in mpx struct for multi-rsp? */
759 if (!mid->multiRsp) {
760 /* smb buffer will be freed by user thread */
761 if (server->large_buf)
762 server->bigbuf = NULL;
763 else
764 server->smallbuf = NULL;
765 }
766 dequeue_mid(mid, malformed);
767}
768
769static void clean_demultiplex_info(struct TCP_Server_Info *server)
770{
771 int length;
772
773 /* take it off the list, if it's not already */
774 spin_lock(&cifs_tcp_ses_lock);
775 list_del_init(&server->tcp_ses_list);
776 spin_unlock(&cifs_tcp_ses_lock);
777
778 spin_lock(&GlobalMid_Lock);
779 server->tcpStatus = CifsExiting;
780 spin_unlock(&GlobalMid_Lock);
781 wake_up_all(&server->response_q);
782
783 /* check if we have blocked requests that need to free */
784 spin_lock(&server->req_lock);
785 if (server->credits <= 0)
786 server->credits = 1;
787 spin_unlock(&server->req_lock);
788 /*
789 * Although there should not be any requests blocked on this queue it
790 * can not hurt to be paranoid and try to wake up requests that may
791 * haven been blocked when more than 50 at time were on the wire to the
792 * same server - they now will see the session is in exit state and get
793 * out of SendReceive.
794 */
795 wake_up_all(&server->request_q);
796 /* give those requests time to exit */
797 msleep(125);
798 if (cifs_rdma_enabled(server) && server->smbd_conn) {
799 smbd_destroy(server->smbd_conn);
800 server->smbd_conn = NULL;
801 }
802 if (server->ssocket) {
803 sock_release(server->ssocket);
804 server->ssocket = NULL;
805 }
806
807 if (!list_empty(&server->pending_mid_q)) {
808 struct list_head dispose_list;
809 struct mid_q_entry *mid_entry;
810 struct list_head *tmp, *tmp2;
811
812 INIT_LIST_HEAD(&dispose_list);
813 spin_lock(&GlobalMid_Lock);
814 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
815 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
816 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
817 mid_entry->mid_state = MID_SHUTDOWN;
818 list_move(&mid_entry->qhead, &dispose_list);
819 }
820 spin_unlock(&GlobalMid_Lock);
821
822 /* now walk dispose list and issue callbacks */
823 list_for_each_safe(tmp, tmp2, &dispose_list) {
824 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
825 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
826 list_del_init(&mid_entry->qhead);
827 mid_entry->callback(mid_entry);
828 }
829 /* 1/8th of sec is more than enough time for them to exit */
830 msleep(125);
831 }
832
833 if (!list_empty(&server->pending_mid_q)) {
834 /*
835 * mpx threads have not exited yet give them at least the smb
836 * send timeout time for long ops.
837 *
838 * Due to delays on oplock break requests, we need to wait at
839 * least 45 seconds before giving up on a request getting a
840 * response and going ahead and killing cifsd.
841 */
842 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
843 msleep(46000);
844 /*
845 * If threads still have not exited they are probably never
846 * coming home not much else we can do but free the memory.
847 */
848 }
849
850 kfree(server->hostname);
851 kfree(server);
852
853 length = atomic_dec_return(&tcpSesAllocCount);
854 if (length > 0)
855 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
856}
857
858static int
859standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
860{
861 int length;
862 char *buf = server->smallbuf;
863 unsigned int pdu_length = server->pdu_size;
864
865 /* make sure this will fit in a large buffer */
866 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
867 server->vals->header_preamble_size) {
868 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
869 cifs_reconnect(server);
870 wake_up(&server->response_q);
871 return -ECONNABORTED;
872 }
873
874 /* switch to large buffer if too big for a small one */
875 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
876 server->large_buf = true;
877 memcpy(server->bigbuf, buf, server->total_read);
878 buf = server->bigbuf;
879 }
880
881 /* now read the rest */
882 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
883 pdu_length - HEADER_SIZE(server) + 1
884 + server->vals->header_preamble_size);
885
886 if (length < 0)
887 return length;
888 server->total_read += length;
889
890 dump_smb(buf, server->total_read);
891
892 return cifs_handle_standard(server, mid);
893}
894
895int
896cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
897{
898 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
899 int length;
900
901 /*
902 * We know that we received enough to get to the MID as we
903 * checked the pdu_length earlier. Now check to see
904 * if the rest of the header is OK. We borrow the length
905 * var for the rest of the loop to avoid a new stack var.
906 *
907 * 48 bytes is enough to display the header and a little bit
908 * into the payload for debugging purposes.
909 */
910 length = server->ops->check_message(buf, server->total_read, server);
911 if (length != 0)
912 cifs_dump_mem("Bad SMB: ", buf,
913 min_t(unsigned int, server->total_read, 48));
914
915 if (server->ops->is_session_expired &&
916 server->ops->is_session_expired(buf)) {
917 cifs_reconnect(server);
918 wake_up(&server->response_q);
919 return -1;
920 }
921
922 if (server->ops->is_status_pending &&
923 server->ops->is_status_pending(buf, server, length))
924 return -1;
925
926 if (!mid)
927 return length;
928
929 handle_mid(mid, server, buf, length);
930 return 0;
931}
932
933static void
934smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
935{
936 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buffer;
937
938 /*
939 * SMB1 does not use credits.
940 */
941 if (server->vals->header_preamble_size)
942 return;
943
944 if (shdr->CreditRequest) {
945 spin_lock(&server->req_lock);
946 server->credits += le16_to_cpu(shdr->CreditRequest);
947 spin_unlock(&server->req_lock);
948 wake_up(&server->request_q);
949 }
950}
951
952
953static int
954cifs_demultiplex_thread(void *p)
955{
956 int i, num_mids, length;
957 struct TCP_Server_Info *server = p;
958 unsigned int pdu_length;
959 unsigned int next_offset;
960 char *buf = NULL;
961 struct task_struct *task_to_wake = NULL;
962 struct mid_q_entry *mids[MAX_COMPOUND];
963 char *bufs[MAX_COMPOUND];
964
965 current->flags |= PF_MEMALLOC;
966 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
967
968 length = atomic_inc_return(&tcpSesAllocCount);
969 if (length > 1)
970 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
971
972 set_freezable();
973 while (server->tcpStatus != CifsExiting) {
974 if (try_to_freeze())
975 continue;
976
977 if (!allocate_buffers(server))
978 continue;
979
980 server->large_buf = false;
981 buf = server->smallbuf;
982 pdu_length = 4; /* enough to get RFC1001 header */
983
984 length = cifs_read_from_socket(server, buf, pdu_length);
985 if (length < 0)
986 continue;
987
988 if (server->vals->header_preamble_size == 0)
989 server->total_read = 0;
990 else
991 server->total_read = length;
992
993 /*
994 * The right amount was read from socket - 4 bytes,
995 * so we can now interpret the length field.
996 */
997 pdu_length = get_rfc1002_length(buf);
998
999 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
1000 if (!is_smb_response(server, buf[0]))
1001 continue;
1002next_pdu:
1003 server->pdu_size = pdu_length;
1004
1005 /* make sure we have enough to get to the MID */
1006 if (server->pdu_size < HEADER_SIZE(server) - 1 -
1007 server->vals->header_preamble_size) {
1008 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
1009 server->pdu_size);
1010 cifs_reconnect(server);
1011 wake_up(&server->response_q);
1012 continue;
1013 }
1014
1015 /* read down to the MID */
1016 length = cifs_read_from_socket(server,
1017 buf + server->vals->header_preamble_size,
1018 HEADER_SIZE(server) - 1
1019 - server->vals->header_preamble_size);
1020 if (length < 0)
1021 continue;
1022 server->total_read += length;
1023
1024 if (server->ops->next_header) {
1025 next_offset = server->ops->next_header(buf);
1026 if (next_offset)
1027 server->pdu_size = next_offset;
1028 }
1029
1030 memset(mids, 0, sizeof(mids));
1031 memset(bufs, 0, sizeof(bufs));
1032 num_mids = 0;
1033
1034 if (server->ops->is_transform_hdr &&
1035 server->ops->receive_transform &&
1036 server->ops->is_transform_hdr(buf)) {
1037 length = server->ops->receive_transform(server,
1038 mids,
1039 bufs,
1040 &num_mids);
1041 } else {
1042 mids[0] = server->ops->find_mid(server, buf);
1043 bufs[0] = buf;
1044 num_mids = 1;
1045
1046 if (!mids[0] || !mids[0]->receive)
1047 length = standard_receive3(server, mids[0]);
1048 else
1049 length = mids[0]->receive(server, mids[0]);
1050 }
1051
1052 if (length < 0) {
1053 for (i = 0; i < num_mids; i++)
1054 if (mids[i])
1055 cifs_mid_q_entry_release(mids[i]);
1056 continue;
1057 }
1058
1059 if (server->large_buf)
1060 buf = server->bigbuf;
1061
1062
1063 server->lstrp = jiffies;
1064
1065 for (i = 0; i < num_mids; i++) {
1066 if (mids[i] != NULL) {
1067 mids[i]->resp_buf_size = server->pdu_size;
1068 if ((mids[i]->mid_flags & MID_WAIT_CANCELLED) &&
1069 mids[i]->mid_state == MID_RESPONSE_RECEIVED &&
1070 server->ops->handle_cancelled_mid)
1071 server->ops->handle_cancelled_mid(
1072 mids[i]->resp_buf,
1073 server);
1074
1075 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1076 mids[i]->callback(mids[i]);
1077
1078 cifs_mid_q_entry_release(mids[i]);
1079 } else if (server->ops->is_oplock_break &&
1080 server->ops->is_oplock_break(bufs[i],
1081 server)) {
1082 smb2_add_credits_from_hdr(bufs[i], server);
1083 cifs_dbg(FYI, "Received oplock break\n");
1084 } else {
1085 cifs_dbg(VFS, "No task to wake, unknown frame "
1086 "received! NumMids %d\n",
1087 atomic_read(&midCount));
1088 cifs_dump_mem("Received Data is: ", bufs[i],
1089 HEADER_SIZE(server));
1090#ifdef CONFIG_CIFS_DEBUG2
1091 if (server->ops->dump_detail)
1092 server->ops->dump_detail(bufs[i],
1093 server);
1094 smb2_add_credits_from_hdr(bufs[i], server);
1095 cifs_dump_mids(server);
1096#endif /* CIFS_DEBUG2 */
1097 }
1098 }
1099
1100 if (pdu_length > server->pdu_size) {
1101 if (!allocate_buffers(server))
1102 continue;
1103 pdu_length -= server->pdu_size;
1104 server->total_read = 0;
1105 server->large_buf = false;
1106 buf = server->smallbuf;
1107 goto next_pdu;
1108 }
1109 } /* end while !EXITING */
1110
1111 /* buffer usually freed in free_mid - need to free it here on exit */
1112 cifs_buf_release(server->bigbuf);
1113 if (server->smallbuf) /* no sense logging a debug message if NULL */
1114 cifs_small_buf_release(server->smallbuf);
1115
1116 task_to_wake = xchg(&server->tsk, NULL);
1117 clean_demultiplex_info(server);
1118
1119 /* if server->tsk was NULL then wait for a signal before exiting */
1120 if (!task_to_wake) {
1121 set_current_state(TASK_INTERRUPTIBLE);
1122 while (!signal_pending(current)) {
1123 schedule();
1124 set_current_state(TASK_INTERRUPTIBLE);
1125 }
1126 set_current_state(TASK_RUNNING);
1127 }
1128
1129 module_put_and_exit(0);
1130}
1131
1132/* extract the host portion of the UNC string */
1133static char *
1134extract_hostname(const char *unc)
1135{
1136 const char *src;
1137 char *dst, *delim;
1138 unsigned int len;
1139
1140 /* skip double chars at beginning of string */
1141 /* BB: check validity of these bytes? */
1142 src = unc + 2;
1143
1144 /* delimiter between hostname and sharename is always '\\' now */
1145 delim = strchr(src, '\\');
1146 if (!delim)
1147 return ERR_PTR(-EINVAL);
1148
1149 len = delim - src;
1150 dst = kmalloc((len + 1), GFP_KERNEL);
1151 if (dst == NULL)
1152 return ERR_PTR(-ENOMEM);
1153
1154 memcpy(dst, src, len);
1155 dst[len] = '\0';
1156
1157 return dst;
1158}
1159
1160static int get_option_ul(substring_t args[], unsigned long *option)
1161{
1162 int rc;
1163 char *string;
1164
1165 string = match_strdup(args);
1166 if (string == NULL)
1167 return -ENOMEM;
1168 rc = kstrtoul(string, 0, option);
1169 kfree(string);
1170
1171 return rc;
1172}
1173
1174static int get_option_uid(substring_t args[], kuid_t *result)
1175{
1176 unsigned long value;
1177 kuid_t uid;
1178 int rc;
1179
1180 rc = get_option_ul(args, &value);
1181 if (rc)
1182 return rc;
1183
1184 uid = make_kuid(current_user_ns(), value);
1185 if (!uid_valid(uid))
1186 return -EINVAL;
1187
1188 *result = uid;
1189 return 0;
1190}
1191
1192static int get_option_gid(substring_t args[], kgid_t *result)
1193{
1194 unsigned long value;
1195 kgid_t gid;
1196 int rc;
1197
1198 rc = get_option_ul(args, &value);
1199 if (rc)
1200 return rc;
1201
1202 gid = make_kgid(current_user_ns(), value);
1203 if (!gid_valid(gid))
1204 return -EINVAL;
1205
1206 *result = gid;
1207 return 0;
1208}
1209
1210static int cifs_parse_security_flavors(char *value,
1211 struct smb_vol *vol)
1212{
1213
1214 substring_t args[MAX_OPT_ARGS];
1215
1216 /*
1217 * With mount options, the last one should win. Reset any existing
1218 * settings back to default.
1219 */
1220 vol->sectype = Unspecified;
1221 vol->sign = false;
1222
1223 switch (match_token(value, cifs_secflavor_tokens, args)) {
1224 case Opt_sec_krb5p:
1225 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1226 return 1;
1227 case Opt_sec_krb5i:
1228 vol->sign = true;
1229 /* Fallthrough */
1230 case Opt_sec_krb5:
1231 vol->sectype = Kerberos;
1232 break;
1233 case Opt_sec_ntlmsspi:
1234 vol->sign = true;
1235 /* Fallthrough */
1236 case Opt_sec_ntlmssp:
1237 vol->sectype = RawNTLMSSP;
1238 break;
1239 case Opt_sec_ntlmi:
1240 vol->sign = true;
1241 /* Fallthrough */
1242 case Opt_ntlm:
1243 vol->sectype = NTLM;
1244 break;
1245 case Opt_sec_ntlmv2i:
1246 vol->sign = true;
1247 /* Fallthrough */
1248 case Opt_sec_ntlmv2:
1249 vol->sectype = NTLMv2;
1250 break;
1251#ifdef CONFIG_CIFS_WEAK_PW_HASH
1252 case Opt_sec_lanman:
1253 vol->sectype = LANMAN;
1254 break;
1255#endif
1256 case Opt_sec_none:
1257 vol->nullauth = 1;
1258 break;
1259 default:
1260 cifs_dbg(VFS, "bad security option: %s\n", value);
1261 return 1;
1262 }
1263
1264 return 0;
1265}
1266
1267static int
1268cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1269{
1270 substring_t args[MAX_OPT_ARGS];
1271
1272 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1273 case Opt_cache_loose:
1274 vol->direct_io = false;
1275 vol->strict_io = false;
1276 break;
1277 case Opt_cache_strict:
1278 vol->direct_io = false;
1279 vol->strict_io = true;
1280 break;
1281 case Opt_cache_none:
1282 vol->direct_io = true;
1283 vol->strict_io = false;
1284 break;
1285 default:
1286 cifs_dbg(VFS, "bad cache= option: %s\n", value);
1287 return 1;
1288 }
1289 return 0;
1290}
1291
1292static int
1293cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
1294{
1295 substring_t args[MAX_OPT_ARGS];
1296
1297 switch (match_token(value, cifs_smb_version_tokens, args)) {
1298#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1299 case Smb_1:
1300 if (disable_legacy_dialects) {
1301 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1302 return 1;
1303 }
1304 if (is_smb3) {
1305 cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
1306 return 1;
1307 }
1308 vol->ops = &smb1_operations;
1309 vol->vals = &smb1_values;
1310 break;
1311 case Smb_20:
1312 if (disable_legacy_dialects) {
1313 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1314 return 1;
1315 }
1316 if (is_smb3) {
1317 cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
1318 return 1;
1319 }
1320 vol->ops = &smb20_operations;
1321 vol->vals = &smb20_values;
1322 break;
1323#else
1324 case Smb_1:
1325 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1326 return 1;
1327 case Smb_20:
1328 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1329 return 1;
1330#endif /* CIFS_ALLOW_INSECURE_LEGACY */
1331 case Smb_21:
1332 vol->ops = &smb21_operations;
1333 vol->vals = &smb21_values;
1334 break;
1335 case Smb_30:
1336 vol->ops = &smb30_operations;
1337 vol->vals = &smb30_values;
1338 break;
1339 case Smb_302:
1340 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1341 vol->vals = &smb302_values;
1342 break;
1343 case Smb_311:
1344 vol->ops = &smb311_operations;
1345 vol->vals = &smb311_values;
1346 break;
1347 case Smb_3any:
1348 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1349 vol->vals = &smb3any_values;
1350 break;
1351 case Smb_default:
1352 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1353 vol->vals = &smbdefault_values;
1354 break;
1355 default:
1356 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
1357 return 1;
1358 }
1359 return 0;
1360}
1361
1362/*
1363 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1364 * fields with the result. Returns 0 on success and an error otherwise.
1365 */
1366static int
1367cifs_parse_devname(const char *devname, struct smb_vol *vol)
1368{
1369 char *pos;
1370 const char *delims = "/\\";
1371 size_t len;
1372
1373 if (unlikely(!devname || !*devname)) {
1374 cifs_dbg(VFS, "Device name not specified.\n");
1375 return -EINVAL;
1376 }
1377
1378 /* make sure we have a valid UNC double delimiter prefix */
1379 len = strspn(devname, delims);
1380 if (len != 2)
1381 return -EINVAL;
1382
1383 /* find delimiter between host and sharename */
1384 pos = strpbrk(devname + 2, delims);
1385 if (!pos)
1386 return -EINVAL;
1387
1388 /* skip past delimiter */
1389 ++pos;
1390
1391 /* now go until next delimiter or end of string */
1392 len = strcspn(pos, delims);
1393
1394 /* move "pos" up to delimiter or NULL */
1395 pos += len;
1396 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1397 if (!vol->UNC)
1398 return -ENOMEM;
1399
1400 convert_delimiter(vol->UNC, '\\');
1401
1402 /* skip any delimiter */
1403 if (*pos == '/' || *pos == '\\')
1404 pos++;
1405
1406 /* If pos is NULL then no prepath */
1407 if (!*pos)
1408 return 0;
1409
1410 vol->prepath = kstrdup(pos, GFP_KERNEL);
1411 if (!vol->prepath)
1412 return -ENOMEM;
1413
1414 return 0;
1415}
1416
1417static int
1418cifs_parse_mount_options(const char *mountdata, const char *devname,
1419 struct smb_vol *vol, bool is_smb3)
1420{
1421 char *data, *end;
1422 char *mountdata_copy = NULL, *options;
1423 unsigned int temp_len, i, j;
1424 char separator[2];
1425 short int override_uid = -1;
1426 short int override_gid = -1;
1427 bool uid_specified = false;
1428 bool gid_specified = false;
1429 bool sloppy = false;
1430 char *invalid = NULL;
1431 char *nodename = utsname()->nodename;
1432 char *string = NULL;
1433 char *tmp_end, *value;
1434 char delim;
1435 bool got_ip = false;
1436 bool got_version = false;
1437 unsigned short port = 0;
1438 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
1439
1440 separator[0] = ',';
1441 separator[1] = 0;
1442 delim = separator[0];
1443
1444 /* ensure we always start with zeroed-out smb_vol */
1445 memset(vol, 0, sizeof(*vol));
1446
1447 /*
1448 * does not have to be perfect mapping since field is
1449 * informational, only used for servers that do not support
1450 * port 445 and it can be overridden at mount time
1451 */
1452 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1453 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
1454 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1455
1456 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
1457 /* null target name indicates to use *SMBSERVR default called name
1458 if we end up sending RFC1001 session initialize */
1459 vol->target_rfc1001_name[0] = 0;
1460 vol->cred_uid = current_uid();
1461 vol->linux_uid = current_uid();
1462 vol->linux_gid = current_gid();
1463
1464 /*
1465 * default to SFM style remapping of seven reserved characters
1466 * unless user overrides it or we negotiate CIFS POSIX where
1467 * it is unnecessary. Can not simultaneously use more than one mapping
1468 * since then readdir could list files that open could not open
1469 */
1470 vol->remap = true;
1471
1472 /* default to only allowing write access to owner of the mount */
1473 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
1474
1475 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
1476 /* default is always to request posix paths. */
1477 vol->posix_paths = 1;
1478 /* default to using server inode numbers where available */
1479 vol->server_ino = 1;
1480
1481 /* default is to use strict cifs caching semantics */
1482 vol->strict_io = true;
1483
1484 vol->actimeo = CIFS_DEF_ACTIMEO;
1485
1486 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1487 vol->ops = &smb30_operations;
1488 vol->vals = &smbdefault_values;
1489
1490 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1491
1492 if (!mountdata)
1493 goto cifs_parse_mount_err;
1494
1495 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1496 if (!mountdata_copy)
1497 goto cifs_parse_mount_err;
1498
1499 options = mountdata_copy;
1500 end = options + strlen(options);
1501
1502 if (strncmp(options, "sep=", 4) == 0) {
1503 if (options[4] != 0) {
1504 separator[0] = options[4];
1505 options += 5;
1506 } else {
1507 cifs_dbg(FYI, "Null separator not allowed\n");
1508 }
1509 }
1510 vol->backupuid_specified = false; /* no backup intent for a user */
1511 vol->backupgid_specified = false; /* no backup intent for a group */
1512
1513 switch (cifs_parse_devname(devname, vol)) {
1514 case 0:
1515 break;
1516 case -ENOMEM:
1517 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1518 goto cifs_parse_mount_err;
1519 case -EINVAL:
1520 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1521 goto cifs_parse_mount_err;
1522 default:
1523 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1524 goto cifs_parse_mount_err;
1525 }
1526
1527 while ((data = strsep(&options, separator)) != NULL) {
1528 substring_t args[MAX_OPT_ARGS];
1529 unsigned long option;
1530 int token;
1531
1532 if (!*data)
1533 continue;
1534
1535 token = match_token(data, cifs_mount_option_tokens, args);
1536
1537 switch (token) {
1538
1539 /* Ingnore the following */
1540 case Opt_ignore:
1541 break;
1542
1543 /* Boolean values */
1544 case Opt_user_xattr:
1545 vol->no_xattr = 0;
1546 break;
1547 case Opt_nouser_xattr:
1548 vol->no_xattr = 1;
1549 break;
1550 case Opt_forceuid:
1551 override_uid = 1;
1552 break;
1553 case Opt_noforceuid:
1554 override_uid = 0;
1555 break;
1556 case Opt_forcegid:
1557 override_gid = 1;
1558 break;
1559 case Opt_noforcegid:
1560 override_gid = 0;
1561 break;
1562 case Opt_noblocksend:
1563 vol->noblocksnd = 1;
1564 break;
1565 case Opt_noautotune:
1566 vol->noautotune = 1;
1567 break;
1568 case Opt_nolease:
1569 vol->no_lease = 1;
1570 break;
1571 case Opt_hard:
1572 vol->retry = 1;
1573 break;
1574 case Opt_soft:
1575 vol->retry = 0;
1576 break;
1577 case Opt_perm:
1578 vol->noperm = 0;
1579 break;
1580 case Opt_noperm:
1581 vol->noperm = 1;
1582 break;
1583 case Opt_mapchars:
1584 vol->sfu_remap = true;
1585 vol->remap = false; /* disable SFM mapping */
1586 break;
1587 case Opt_nomapchars:
1588 vol->sfu_remap = false;
1589 break;
1590 case Opt_mapposix:
1591 vol->remap = true;
1592 vol->sfu_remap = false; /* disable SFU mapping */
1593 break;
1594 case Opt_nomapposix:
1595 vol->remap = false;
1596 break;
1597 case Opt_sfu:
1598 vol->sfu_emul = 1;
1599 break;
1600 case Opt_nosfu:
1601 vol->sfu_emul = 0;
1602 break;
1603 case Opt_nodfs:
1604 vol->nodfs = 1;
1605 break;
1606 case Opt_posixpaths:
1607 vol->posix_paths = 1;
1608 break;
1609 case Opt_noposixpaths:
1610 vol->posix_paths = 0;
1611 break;
1612 case Opt_nounix:
1613 if (vol->linux_ext)
1614 cifs_dbg(VFS,
1615 "conflicting unix mount options\n");
1616 vol->no_linux_ext = 1;
1617 break;
1618 case Opt_unix:
1619 if (vol->no_linux_ext)
1620 cifs_dbg(VFS,
1621 "conflicting unix mount options\n");
1622 vol->linux_ext = 1;
1623 break;
1624 case Opt_nocase:
1625 vol->nocase = 1;
1626 break;
1627 case Opt_brl:
1628 vol->nobrl = 0;
1629 break;
1630 case Opt_nobrl:
1631 vol->nobrl = 1;
1632 /*
1633 * turn off mandatory locking in mode
1634 * if remote locking is turned off since the
1635 * local vfs will do advisory
1636 */
1637 if (vol->file_mode ==
1638 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
1639 vol->file_mode = S_IALLUGO;
1640 break;
1641 case Opt_nohandlecache:
1642 vol->nohandlecache = 1;
1643 break;
1644 case Opt_handlecache:
1645 vol->nohandlecache = 0;
1646 break;
1647 case Opt_forcemandatorylock:
1648 vol->mand_lock = 1;
1649 break;
1650 case Opt_setuids:
1651 vol->setuids = 1;
1652 break;
1653 case Opt_nosetuids:
1654 vol->setuids = 0;
1655 break;
1656 case Opt_setuidfromacl:
1657 vol->setuidfromacl = 1;
1658 break;
1659 case Opt_dynperm:
1660 vol->dynperm = true;
1661 break;
1662 case Opt_nodynperm:
1663 vol->dynperm = false;
1664 break;
1665 case Opt_nohard:
1666 vol->retry = 0;
1667 break;
1668 case Opt_nosoft:
1669 vol->retry = 1;
1670 break;
1671 case Opt_nointr:
1672 vol->intr = 0;
1673 break;
1674 case Opt_intr:
1675 vol->intr = 1;
1676 break;
1677 case Opt_nostrictsync:
1678 vol->nostrictsync = 1;
1679 break;
1680 case Opt_strictsync:
1681 vol->nostrictsync = 0;
1682 break;
1683 case Opt_serverino:
1684 vol->server_ino = 1;
1685 break;
1686 case Opt_noserverino:
1687 vol->server_ino = 0;
1688 break;
1689 case Opt_rwpidforward:
1690 vol->rwpidforward = 1;
1691 break;
1692 case Opt_cifsacl:
1693 vol->cifs_acl = 1;
1694 break;
1695 case Opt_nocifsacl:
1696 vol->cifs_acl = 0;
1697 break;
1698 case Opt_acl:
1699 vol->no_psx_acl = 0;
1700 break;
1701 case Opt_noacl:
1702 vol->no_psx_acl = 1;
1703 break;
1704 case Opt_locallease:
1705 vol->local_lease = 1;
1706 break;
1707 case Opt_sign:
1708 vol->sign = true;
1709 break;
1710 case Opt_seal:
1711 /* we do not do the following in secFlags because seal
1712 * is a per tree connection (mount) not a per socket
1713 * or per-smb connection option in the protocol
1714 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1715 */
1716 vol->seal = 1;
1717 break;
1718 case Opt_noac:
1719 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
1720 break;
1721 case Opt_fsc:
1722#ifndef CONFIG_CIFS_FSCACHE
1723 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
1724 goto cifs_parse_mount_err;
1725#endif
1726 vol->fsc = true;
1727 break;
1728 case Opt_mfsymlinks:
1729 vol->mfsymlinks = true;
1730 break;
1731 case Opt_multiuser:
1732 vol->multiuser = true;
1733 break;
1734 case Opt_sloppy:
1735 sloppy = true;
1736 break;
1737 case Opt_nosharesock:
1738 vol->nosharesock = true;
1739 break;
1740 case Opt_nopersistent:
1741 vol->nopersistent = true;
1742 if (vol->persistent) {
1743 cifs_dbg(VFS,
1744 "persistenthandles mount options conflict\n");
1745 goto cifs_parse_mount_err;
1746 }
1747 break;
1748 case Opt_persistent:
1749 vol->persistent = true;
1750 if ((vol->nopersistent) || (vol->resilient)) {
1751 cifs_dbg(VFS,
1752 "persistenthandles mount options conflict\n");
1753 goto cifs_parse_mount_err;
1754 }
1755 break;
1756 case Opt_resilient:
1757 vol->resilient = true;
1758 if (vol->persistent) {
1759 cifs_dbg(VFS,
1760 "persistenthandles mount options conflict\n");
1761 goto cifs_parse_mount_err;
1762 }
1763 break;
1764 case Opt_noresilient:
1765 vol->resilient = false; /* already the default */
1766 break;
1767 case Opt_domainauto:
1768 vol->domainauto = true;
1769 break;
1770 case Opt_rdma:
1771 vol->rdma = true;
1772 break;
1773
1774 /* Numeric Values */
1775 case Opt_backupuid:
1776 if (get_option_uid(args, &vol->backupuid)) {
1777 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1778 __func__);
1779 goto cifs_parse_mount_err;
1780 }
1781 vol->backupuid_specified = true;
1782 break;
1783 case Opt_backupgid:
1784 if (get_option_gid(args, &vol->backupgid)) {
1785 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1786 __func__);
1787 goto cifs_parse_mount_err;
1788 }
1789 vol->backupgid_specified = true;
1790 break;
1791 case Opt_uid:
1792 if (get_option_uid(args, &vol->linux_uid)) {
1793 cifs_dbg(VFS, "%s: Invalid uid value\n",
1794 __func__);
1795 goto cifs_parse_mount_err;
1796 }
1797 uid_specified = true;
1798 break;
1799 case Opt_cruid:
1800 if (get_option_uid(args, &vol->cred_uid)) {
1801 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1802 __func__);
1803 goto cifs_parse_mount_err;
1804 }
1805 break;
1806 case Opt_gid:
1807 if (get_option_gid(args, &vol->linux_gid)) {
1808 cifs_dbg(VFS, "%s: Invalid gid value\n",
1809 __func__);
1810 goto cifs_parse_mount_err;
1811 }
1812 gid_specified = true;
1813 break;
1814 case Opt_file_mode:
1815 if (get_option_ul(args, &option)) {
1816 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1817 __func__);
1818 goto cifs_parse_mount_err;
1819 }
1820 vol->file_mode = option;
1821 break;
1822 case Opt_dirmode:
1823 if (get_option_ul(args, &option)) {
1824 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1825 __func__);
1826 goto cifs_parse_mount_err;
1827 }
1828 vol->dir_mode = option;
1829 break;
1830 case Opt_port:
1831 if (get_option_ul(args, &option) ||
1832 option > USHRT_MAX) {
1833 cifs_dbg(VFS, "%s: Invalid port value\n",
1834 __func__);
1835 goto cifs_parse_mount_err;
1836 }
1837 port = (unsigned short)option;
1838 break;
1839 case Opt_rsize:
1840 if (get_option_ul(args, &option)) {
1841 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1842 __func__);
1843 goto cifs_parse_mount_err;
1844 }
1845 vol->rsize = option;
1846 break;
1847 case Opt_wsize:
1848 if (get_option_ul(args, &option)) {
1849 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1850 __func__);
1851 goto cifs_parse_mount_err;
1852 }
1853 vol->wsize = option;
1854 break;
1855 case Opt_actimeo:
1856 if (get_option_ul(args, &option)) {
1857 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1858 __func__);
1859 goto cifs_parse_mount_err;
1860 }
1861 vol->actimeo = HZ * option;
1862 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
1863 cifs_dbg(VFS, "attribute cache timeout too large\n");
1864 goto cifs_parse_mount_err;
1865 }
1866 break;
1867 case Opt_echo_interval:
1868 if (get_option_ul(args, &option)) {
1869 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1870 __func__);
1871 goto cifs_parse_mount_err;
1872 }
1873 vol->echo_interval = option;
1874 break;
1875 case Opt_snapshot:
1876 if (get_option_ul(args, &option)) {
1877 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1878 __func__);
1879 goto cifs_parse_mount_err;
1880 }
1881 vol->snapshot_time = option;
1882 break;
1883 case Opt_max_credits:
1884 if (get_option_ul(args, &option) || (option < 20) ||
1885 (option > 60000)) {
1886 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1887 __func__);
1888 goto cifs_parse_mount_err;
1889 }
1890 vol->max_credits = option;
1891 break;
1892
1893 /* String Arguments */
1894
1895 case Opt_blank_user:
1896 /* null user, ie. anonymous authentication */
1897 vol->nullauth = 1;
1898 vol->username = NULL;
1899 break;
1900 case Opt_user:
1901 string = match_strdup(args);
1902 if (string == NULL)
1903 goto out_nomem;
1904
1905 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1906 CIFS_MAX_USERNAME_LEN) {
1907 pr_warn("CIFS: username too long\n");
1908 goto cifs_parse_mount_err;
1909 }
1910
1911 kfree(vol->username);
1912 vol->username = kstrdup(string, GFP_KERNEL);
1913 if (!vol->username)
1914 goto cifs_parse_mount_err;
1915 break;
1916 case Opt_blank_pass:
1917 /* passwords have to be handled differently
1918 * to allow the character used for deliminator
1919 * to be passed within them
1920 */
1921
1922 /*
1923 * Check if this is a case where the password
1924 * starts with a delimiter
1925 */
1926 tmp_end = strchr(data, '=');
1927 tmp_end++;
1928 if (!(tmp_end < end && tmp_end[1] == delim)) {
1929 /* No it is not. Set the password to NULL */
1930 kzfree(vol->password);
1931 vol->password = NULL;
1932 break;
1933 }
1934 /* Yes it is. Drop down to Opt_pass below.*/
1935 case Opt_pass:
1936 /* Obtain the value string */
1937 value = strchr(data, '=');
1938 value++;
1939
1940 /* Set tmp_end to end of the string */
1941 tmp_end = (char *) value + strlen(value);
1942
1943 /* Check if following character is the deliminator
1944 * If yes, we have encountered a double deliminator
1945 * reset the NULL character to the deliminator
1946 */
1947 if (tmp_end < end && tmp_end[1] == delim) {
1948 tmp_end[0] = delim;
1949
1950 /* Keep iterating until we get to a single
1951 * deliminator OR the end
1952 */
1953 while ((tmp_end = strchr(tmp_end, delim))
1954 != NULL && (tmp_end[1] == delim)) {
1955 tmp_end = (char *) &tmp_end[2];
1956 }
1957
1958 /* Reset var options to point to next element */
1959 if (tmp_end) {
1960 tmp_end[0] = '\0';
1961 options = (char *) &tmp_end[1];
1962 } else
1963 /* Reached the end of the mount option
1964 * string */
1965 options = end;
1966 }
1967
1968 kzfree(vol->password);
1969 /* Now build new password string */
1970 temp_len = strlen(value);
1971 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1972 if (vol->password == NULL) {
1973 pr_warn("CIFS: no memory for password\n");
1974 goto cifs_parse_mount_err;
1975 }
1976
1977 for (i = 0, j = 0; i < temp_len; i++, j++) {
1978 vol->password[j] = value[i];
1979 if ((value[i] == delim) &&
1980 value[i+1] == delim)
1981 /* skip the second deliminator */
1982 i++;
1983 }
1984 vol->password[j] = '\0';
1985 break;
1986 case Opt_blank_ip:
1987 /* FIXME: should this be an error instead? */
1988 got_ip = false;
1989 break;
1990 case Opt_ip:
1991 string = match_strdup(args);
1992 if (string == NULL)
1993 goto out_nomem;
1994
1995 if (!cifs_convert_address(dstaddr, string,
1996 strlen(string))) {
1997 pr_err("CIFS: bad ip= option (%s).\n", string);
1998 goto cifs_parse_mount_err;
1999 }
2000 got_ip = true;
2001 break;
2002 case Opt_domain:
2003 string = match_strdup(args);
2004 if (string == NULL)
2005 goto out_nomem;
2006
2007 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
2008 == CIFS_MAX_DOMAINNAME_LEN) {
2009 pr_warn("CIFS: domain name too long\n");
2010 goto cifs_parse_mount_err;
2011 }
2012
2013 kfree(vol->domainname);
2014 vol->domainname = kstrdup(string, GFP_KERNEL);
2015 if (!vol->domainname) {
2016 pr_warn("CIFS: no memory for domainname\n");
2017 goto cifs_parse_mount_err;
2018 }
2019 cifs_dbg(FYI, "Domain name set\n");
2020 break;
2021 case Opt_srcaddr:
2022 string = match_strdup(args);
2023 if (string == NULL)
2024 goto out_nomem;
2025
2026 if (!cifs_convert_address(
2027 (struct sockaddr *)&vol->srcaddr,
2028 string, strlen(string))) {
2029 pr_warn("CIFS: Could not parse srcaddr: %s\n",
2030 string);
2031 goto cifs_parse_mount_err;
2032 }
2033 break;
2034 case Opt_iocharset:
2035 string = match_strdup(args);
2036 if (string == NULL)
2037 goto out_nomem;
2038
2039 if (strnlen(string, 1024) >= 65) {
2040 pr_warn("CIFS: iocharset name too long.\n");
2041 goto cifs_parse_mount_err;
2042 }
2043
2044 if (strncasecmp(string, "default", 7) != 0) {
2045 kfree(vol->iocharset);
2046 vol->iocharset = kstrdup(string,
2047 GFP_KERNEL);
2048 if (!vol->iocharset) {
2049 pr_warn("CIFS: no memory for charset\n");
2050 goto cifs_parse_mount_err;
2051 }
2052 }
2053 /* if iocharset not set then load_nls_default
2054 * is used by caller
2055 */
2056 cifs_dbg(FYI, "iocharset set to %s\n", string);
2057 break;
2058 case Opt_netbiosname:
2059 string = match_strdup(args);
2060 if (string == NULL)
2061 goto out_nomem;
2062
2063 memset(vol->source_rfc1001_name, 0x20,
2064 RFC1001_NAME_LEN);
2065 /*
2066 * FIXME: are there cases in which a comma can
2067 * be valid in workstation netbios name (and
2068 * need special handling)?
2069 */
2070 for (i = 0; i < RFC1001_NAME_LEN; i++) {
2071 /* don't ucase netbiosname for user */
2072 if (string[i] == 0)
2073 break;
2074 vol->source_rfc1001_name[i] = string[i];
2075 }
2076 /* The string has 16th byte zero still from
2077 * set at top of the function
2078 */
2079 if (i == RFC1001_NAME_LEN && string[i] != 0)
2080 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
2081 break;
2082 case Opt_servern:
2083 /* servernetbiosname specified override *SMBSERVER */
2084 string = match_strdup(args);
2085 if (string == NULL)
2086 goto out_nomem;
2087
2088 /* last byte, type, is 0x20 for servr type */
2089 memset(vol->target_rfc1001_name, 0x20,
2090 RFC1001_NAME_LEN_WITH_NULL);
2091
2092 /* BB are there cases in which a comma can be
2093 valid in this workstation netbios name
2094 (and need special handling)? */
2095
2096 /* user or mount helper must uppercase the
2097 netbios name */
2098 for (i = 0; i < 15; i++) {
2099 if (string[i] == 0)
2100 break;
2101 vol->target_rfc1001_name[i] = string[i];
2102 }
2103 /* The string has 16th byte zero still from
2104 set at top of the function */
2105 if (i == RFC1001_NAME_LEN && string[i] != 0)
2106 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
2107 break;
2108 case Opt_ver:
2109 /* version of mount userspace tools, not dialect */
2110 string = match_strdup(args);
2111 if (string == NULL)
2112 goto out_nomem;
2113
2114 /* If interface changes in mount.cifs bump to new ver */
2115 if (strncasecmp(string, "1", 1) == 0) {
2116 if (strlen(string) > 1) {
2117 pr_warn("Bad mount helper ver=%s. Did "
2118 "you want SMB1 (CIFS) dialect "
2119 "and mean to type vers=1.0 "
2120 "instead?\n", string);
2121 goto cifs_parse_mount_err;
2122 }
2123 /* This is the default */
2124 break;
2125 }
2126 /* For all other value, error */
2127 pr_warn("CIFS: Invalid mount helper version specified\n");
2128 goto cifs_parse_mount_err;
2129 case Opt_vers:
2130 /* protocol version (dialect) */
2131 string = match_strdup(args);
2132 if (string == NULL)
2133 goto out_nomem;
2134
2135 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
2136 goto cifs_parse_mount_err;
2137 got_version = true;
2138 break;
2139 case Opt_sec:
2140 string = match_strdup(args);
2141 if (string == NULL)
2142 goto out_nomem;
2143
2144 if (cifs_parse_security_flavors(string, vol) != 0)
2145 goto cifs_parse_mount_err;
2146 break;
2147 case Opt_cache:
2148 string = match_strdup(args);
2149 if (string == NULL)
2150 goto out_nomem;
2151
2152 if (cifs_parse_cache_flavor(string, vol) != 0)
2153 goto cifs_parse_mount_err;
2154 break;
2155 default:
2156 /*
2157 * An option we don't recognize. Save it off for later
2158 * if we haven't already found one
2159 */
2160 if (!invalid)
2161 invalid = data;
2162 break;
2163 }
2164 /* Free up any allocated string */
2165 kfree(string);
2166 string = NULL;
2167 }
2168
2169 if (!sloppy && invalid) {
2170 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
2171 goto cifs_parse_mount_err;
2172 }
2173
2174 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2175 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2176 goto cifs_parse_mount_err;
2177 }
2178
2179#ifndef CONFIG_KEYS
2180 /* Muliuser mounts require CONFIG_KEYS support */
2181 if (vol->multiuser) {
2182 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
2183 goto cifs_parse_mount_err;
2184 }
2185#endif
2186 if (!vol->UNC) {
2187 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
2188 goto cifs_parse_mount_err;
2189 }
2190
2191 /* make sure UNC has a share name */
2192 if (!strchr(vol->UNC + 3, '\\')) {
2193 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
2194 goto cifs_parse_mount_err;
2195 }
2196
2197 if (!got_ip) {
2198 int len;
2199 const char *slash;
2200
2201 /* No ip= option specified? Try to get it from UNC */
2202 /* Use the address part of the UNC. */
2203 slash = strchr(&vol->UNC[2], '\\');
2204 len = slash - &vol->UNC[2];
2205 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
2206 pr_err("Unable to determine destination address.\n");
2207 goto cifs_parse_mount_err;
2208 }
2209 }
2210
2211 /* set the port that we got earlier */
2212 cifs_set_port(dstaddr, port);
2213
2214 if (uid_specified)
2215 vol->override_uid = override_uid;
2216 else if (override_uid == 1)
2217 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
2218
2219 if (gid_specified)
2220 vol->override_gid = override_gid;
2221 else if (override_gid == 1)
2222 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
2223
2224 if (got_version == false)
2225 pr_warn("No dialect specified on mount. Default has changed to "
2226 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
2227 "(SMB1). To use the less secure SMB1 dialect to access "
2228 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2229 " on mount.\n");
2230
2231 kfree(mountdata_copy);
2232 return 0;
2233
2234out_nomem:
2235 pr_warn("Could not allocate temporary buffer\n");
2236cifs_parse_mount_err:
2237 kfree(string);
2238 kfree(mountdata_copy);
2239 return 1;
2240}
2241
2242/** Returns true if srcaddr isn't specified and rhs isn't
2243 * specified, or if srcaddr is specified and
2244 * matches the IP address of the rhs argument.
2245 */
2246static bool
2247srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2248{
2249 switch (srcaddr->sa_family) {
2250 case AF_UNSPEC:
2251 return (rhs->sa_family == AF_UNSPEC);
2252 case AF_INET: {
2253 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2254 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2255 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2256 }
2257 case AF_INET6: {
2258 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
2259 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
2260 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2261 }
2262 default:
2263 WARN_ON(1);
2264 return false; /* don't expect to be here */
2265 }
2266}
2267
2268/*
2269 * If no port is specified in addr structure, we try to match with 445 port
2270 * and if it fails - with 139 ports. It should be called only if address
2271 * families of server and addr are equal.
2272 */
2273static bool
2274match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2275{
2276 __be16 port, *sport;
2277
2278 switch (addr->sa_family) {
2279 case AF_INET:
2280 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2281 port = ((struct sockaddr_in *) addr)->sin_port;
2282 break;
2283 case AF_INET6:
2284 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2285 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2286 break;
2287 default:
2288 WARN_ON(1);
2289 return false;
2290 }
2291
2292 if (!port) {
2293 port = htons(CIFS_PORT);
2294 if (port == *sport)
2295 return true;
2296
2297 port = htons(RFC1001_PORT);
2298 }
2299
2300 return port == *sport;
2301}
2302
2303static bool
2304match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2305 struct sockaddr *srcaddr)
2306{
2307 switch (addr->sa_family) {
2308 case AF_INET: {
2309 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2310 struct sockaddr_in *srv_addr4 =
2311 (struct sockaddr_in *)&server->dstaddr;
2312
2313 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
2314 return false;
2315 break;
2316 }
2317 case AF_INET6: {
2318 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2319 struct sockaddr_in6 *srv_addr6 =
2320 (struct sockaddr_in6 *)&server->dstaddr;
2321
2322 if (!ipv6_addr_equal(&addr6->sin6_addr,
2323 &srv_addr6->sin6_addr))
2324 return false;
2325 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
2326 return false;
2327 break;
2328 }
2329 default:
2330 WARN_ON(1);
2331 return false; /* don't expect to be here */
2332 }
2333
2334 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2335 return false;
2336
2337 return true;
2338}
2339
2340static bool
2341match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2342{
2343 /*
2344 * The select_sectype function should either return the vol->sectype
2345 * that was specified, or "Unspecified" if that sectype was not
2346 * compatible with the given NEGOTIATE request.
2347 */
2348 if (server->ops->select_sectype(server, vol->sectype)
2349 == Unspecified)
2350 return false;
2351
2352 /*
2353 * Now check if signing mode is acceptable. No need to check
2354 * global_secflags at this point since if MUST_SIGN is set then
2355 * the server->sign had better be too.
2356 */
2357 if (vol->sign && !server->sign)
2358 return false;
2359
2360 return true;
2361}
2362
2363static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
2364{
2365 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2366
2367 if (vol->nosharesock)
2368 return 0;
2369
2370 /* BB update this for smb3any and default case */
2371 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2372 return 0;
2373
2374 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2375 return 0;
2376
2377 if (!match_address(server, addr,
2378 (struct sockaddr *)&vol->srcaddr))
2379 return 0;
2380
2381 if (!match_port(server, addr))
2382 return 0;
2383
2384 if (!match_security(server, vol))
2385 return 0;
2386
2387 if (server->echo_interval != vol->echo_interval * HZ)
2388 return 0;
2389
2390 if (server->rdma != vol->rdma)
2391 return 0;
2392
2393 return 1;
2394}
2395
2396static struct TCP_Server_Info *
2397cifs_find_tcp_session(struct smb_vol *vol)
2398{
2399 struct TCP_Server_Info *server;
2400
2401 spin_lock(&cifs_tcp_ses_lock);
2402 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
2403 if (!match_server(server, vol))
2404 continue;
2405
2406 ++server->srv_count;
2407 spin_unlock(&cifs_tcp_ses_lock);
2408 cifs_dbg(FYI, "Existing tcp session with server found\n");
2409 return server;
2410 }
2411 spin_unlock(&cifs_tcp_ses_lock);
2412 return NULL;
2413}
2414
2415void
2416cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
2417{
2418 struct task_struct *task;
2419
2420 spin_lock(&cifs_tcp_ses_lock);
2421 if (--server->srv_count > 0) {
2422 spin_unlock(&cifs_tcp_ses_lock);
2423 return;
2424 }
2425
2426 put_net(cifs_net_ns(server));
2427
2428 list_del_init(&server->tcp_ses_list);
2429 spin_unlock(&cifs_tcp_ses_lock);
2430
2431 cancel_delayed_work_sync(&server->echo);
2432
2433 if (from_reconnect)
2434 /*
2435 * Avoid deadlock here: reconnect work calls
2436 * cifs_put_tcp_session() at its end. Need to be sure
2437 * that reconnect work does nothing with server pointer after
2438 * that step.
2439 */
2440 cancel_delayed_work(&server->reconnect);
2441 else
2442 cancel_delayed_work_sync(&server->reconnect);
2443
2444 spin_lock(&GlobalMid_Lock);
2445 server->tcpStatus = CifsExiting;
2446 spin_unlock(&GlobalMid_Lock);
2447
2448 cifs_crypto_secmech_release(server);
2449 cifs_fscache_release_client_cookie(server);
2450
2451 kfree(server->session_key.response);
2452 server->session_key.response = NULL;
2453 server->session_key.len = 0;
2454
2455 task = xchg(&server->tsk, NULL);
2456 if (task)
2457 force_sig(SIGKILL, task);
2458}
2459
2460static struct TCP_Server_Info *
2461cifs_get_tcp_session(struct smb_vol *volume_info)
2462{
2463 struct TCP_Server_Info *tcp_ses = NULL;
2464 int rc;
2465
2466 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
2467
2468 /* see if we already have a matching tcp_ses */
2469 tcp_ses = cifs_find_tcp_session(volume_info);
2470 if (tcp_ses)
2471 return tcp_ses;
2472
2473 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2474 if (!tcp_ses) {
2475 rc = -ENOMEM;
2476 goto out_err;
2477 }
2478
2479 tcp_ses->ops = volume_info->ops;
2480 tcp_ses->vals = volume_info->vals;
2481 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
2482 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2483 if (IS_ERR(tcp_ses->hostname)) {
2484 rc = PTR_ERR(tcp_ses->hostname);
2485 goto out_err_crypto_release;
2486 }
2487
2488 tcp_ses->noblocksnd = volume_info->noblocksnd;
2489 tcp_ses->noautotune = volume_info->noautotune;
2490 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
2491 tcp_ses->rdma = volume_info->rdma;
2492 tcp_ses->in_flight = 0;
2493 tcp_ses->credits = 1;
2494 init_waitqueue_head(&tcp_ses->response_q);
2495 init_waitqueue_head(&tcp_ses->request_q);
2496 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2497 mutex_init(&tcp_ses->srv_mutex);
2498 memcpy(tcp_ses->workstation_RFC1001_name,
2499 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2500 memcpy(tcp_ses->server_RFC1001_name,
2501 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2502 tcp_ses->session_estab = false;
2503 tcp_ses->sequence_number = 0;
2504 tcp_ses->lstrp = jiffies;
2505 spin_lock_init(&tcp_ses->req_lock);
2506 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2507 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
2508 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
2509 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2510 mutex_init(&tcp_ses->reconnect_mutex);
2511 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2512 sizeof(tcp_ses->srcaddr));
2513 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2514 sizeof(tcp_ses->dstaddr));
2515 generate_random_uuid(tcp_ses->client_guid);
2516 /*
2517 * at this point we are the only ones with the pointer
2518 * to the struct since the kernel thread not created yet
2519 * no need to spinlock this init of tcpStatus or srv_count
2520 */
2521 tcp_ses->tcpStatus = CifsNew;
2522 ++tcp_ses->srv_count;
2523
2524 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2525 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2526 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2527 else
2528 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
2529 if (tcp_ses->rdma) {
2530#ifndef CONFIG_CIFS_SMB_DIRECT
2531 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2532 rc = -ENOENT;
2533 goto out_err_crypto_release;
2534#endif
2535 tcp_ses->smbd_conn = smbd_get_connection(
2536 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2537 if (tcp_ses->smbd_conn) {
2538 cifs_dbg(VFS, "RDMA transport established\n");
2539 rc = 0;
2540 goto smbd_connected;
2541 } else {
2542 rc = -ENOENT;
2543 goto out_err_crypto_release;
2544 }
2545 }
2546 rc = ip_connect(tcp_ses);
2547 if (rc < 0) {
2548 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
2549 goto out_err_crypto_release;
2550 }
2551smbd_connected:
2552 /*
2553 * since we're in a cifs function already, we know that
2554 * this will succeed. No need for try_module_get().
2555 */
2556 __module_get(THIS_MODULE);
2557 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
2558 tcp_ses, "cifsd");
2559 if (IS_ERR(tcp_ses->tsk)) {
2560 rc = PTR_ERR(tcp_ses->tsk);
2561 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
2562 module_put(THIS_MODULE);
2563 goto out_err_crypto_release;
2564 }
2565 tcp_ses->tcpStatus = CifsNeedNegotiate;
2566
2567 /* thread spawned, put it on the list */
2568 spin_lock(&cifs_tcp_ses_lock);
2569 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
2570 spin_unlock(&cifs_tcp_ses_lock);
2571
2572 cifs_fscache_get_client_cookie(tcp_ses);
2573
2574 /* queue echo request delayed work */
2575 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
2576
2577 return tcp_ses;
2578
2579out_err_crypto_release:
2580 cifs_crypto_secmech_release(tcp_ses);
2581
2582 put_net(cifs_net_ns(tcp_ses));
2583
2584out_err:
2585 if (tcp_ses) {
2586 if (!IS_ERR(tcp_ses->hostname))
2587 kfree(tcp_ses->hostname);
2588 if (tcp_ses->ssocket)
2589 sock_release(tcp_ses->ssocket);
2590 kfree(tcp_ses);
2591 }
2592 return ERR_PTR(rc);
2593}
2594
2595static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
2596{
2597 if (vol->sectype != Unspecified &&
2598 vol->sectype != ses->sectype)
2599 return 0;
2600
2601 switch (ses->sectype) {
2602 case Kerberos:
2603 if (!uid_eq(vol->cred_uid, ses->cred_uid))
2604 return 0;
2605 break;
2606 default:
2607 /* NULL username means anonymous session */
2608 if (ses->user_name == NULL) {
2609 if (!vol->nullauth)
2610 return 0;
2611 break;
2612 }
2613
2614 /* anything else takes username/password */
2615 if (strncmp(ses->user_name,
2616 vol->username ? vol->username : "",
2617 CIFS_MAX_USERNAME_LEN))
2618 return 0;
2619 if ((vol->username && strlen(vol->username) != 0) &&
2620 ses->password != NULL &&
2621 strncmp(ses->password,
2622 vol->password ? vol->password : "",
2623 CIFS_MAX_PASSWORD_LEN))
2624 return 0;
2625 }
2626 return 1;
2627}
2628
2629/**
2630 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2631 *
2632 * A new IPC connection is made and stored in the session
2633 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2634 */
2635static int
2636cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2637{
2638 int rc = 0, xid;
2639 struct cifs_tcon *tcon;
2640 struct nls_table *nls_codepage;
2641 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2642 bool seal = false;
2643
2644 /*
2645 * If the mount request that resulted in the creation of the
2646 * session requires encryption, force IPC to be encrypted too.
2647 */
2648 if (volume_info->seal) {
2649 if (ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
2650 seal = true;
2651 else {
2652 cifs_dbg(VFS,
2653 "IPC: server doesn't support encryption\n");
2654 return -EOPNOTSUPP;
2655 }
2656 }
2657
2658 tcon = tconInfoAlloc();
2659 if (tcon == NULL)
2660 return -ENOMEM;
2661
2662 snprintf(unc, sizeof(unc), "\\\\%s\\IPC$", ses->server->hostname);
2663
2664 /* cannot fail */
2665 nls_codepage = load_nls_default();
2666
2667 xid = get_xid();
2668 tcon->ses = ses;
2669 tcon->ipc = true;
2670 tcon->seal = seal;
2671 rc = ses->server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
2672 free_xid(xid);
2673
2674 if (rc) {
2675 cifs_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
2676 tconInfoFree(tcon);
2677 goto out;
2678 }
2679
2680 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2681
2682 ses->tcon_ipc = tcon;
2683out:
2684 unload_nls(nls_codepage);
2685 return rc;
2686}
2687
2688/**
2689 * cifs_free_ipc - helper to release the session IPC tcon
2690 *
2691 * Needs to be called everytime a session is destroyed
2692 */
2693static int
2694cifs_free_ipc(struct cifs_ses *ses)
2695{
2696 int rc = 0, xid;
2697 struct cifs_tcon *tcon = ses->tcon_ipc;
2698
2699 if (tcon == NULL)
2700 return 0;
2701
2702 if (ses->server->ops->tree_disconnect) {
2703 xid = get_xid();
2704 rc = ses->server->ops->tree_disconnect(xid, tcon);
2705 free_xid(xid);
2706 }
2707
2708 if (rc)
2709 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2710
2711 tconInfoFree(tcon);
2712 ses->tcon_ipc = NULL;
2713 return rc;
2714}
2715
2716static struct cifs_ses *
2717cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
2718{
2719 struct cifs_ses *ses;
2720
2721 spin_lock(&cifs_tcp_ses_lock);
2722 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2723 if (ses->status == CifsExiting)
2724 continue;
2725 if (!match_session(ses, vol))
2726 continue;
2727 ++ses->ses_count;
2728 spin_unlock(&cifs_tcp_ses_lock);
2729 return ses;
2730 }
2731 spin_unlock(&cifs_tcp_ses_lock);
2732 return NULL;
2733}
2734
2735static void
2736cifs_put_smb_ses(struct cifs_ses *ses)
2737{
2738 unsigned int rc, xid;
2739 struct TCP_Server_Info *server = ses->server;
2740
2741 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
2742
2743 spin_lock(&cifs_tcp_ses_lock);
2744 if (ses->status == CifsExiting) {
2745 spin_unlock(&cifs_tcp_ses_lock);
2746 return;
2747 }
2748 if (--ses->ses_count > 0) {
2749 spin_unlock(&cifs_tcp_ses_lock);
2750 return;
2751 }
2752 if (ses->status == CifsGood)
2753 ses->status = CifsExiting;
2754 spin_unlock(&cifs_tcp_ses_lock);
2755
2756 cifs_free_ipc(ses);
2757
2758 if (ses->status == CifsExiting && server->ops->logoff) {
2759 xid = get_xid();
2760 rc = server->ops->logoff(xid, ses);
2761 if (rc)
2762 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2763 __func__, rc);
2764 _free_xid(xid);
2765 }
2766
2767 spin_lock(&cifs_tcp_ses_lock);
2768 list_del_init(&ses->smb_ses_list);
2769 spin_unlock(&cifs_tcp_ses_lock);
2770
2771 sesInfoFree(ses);
2772 cifs_put_tcp_session(server, 0);
2773}
2774
2775#ifdef CONFIG_KEYS
2776
2777/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2778#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
2779
2780/* Populate username and pw fields from keyring if possible */
2781static int
2782cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2783{
2784 int rc = 0;
2785 int is_domain = 0;
2786 const char *delim, *payload;
2787 char *desc;
2788 ssize_t len;
2789 struct key *key;
2790 struct TCP_Server_Info *server = ses->server;
2791 struct sockaddr_in *sa;
2792 struct sockaddr_in6 *sa6;
2793 const struct user_key_payload *upayload;
2794
2795 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2796 if (!desc)
2797 return -ENOMEM;
2798
2799 /* try to find an address key first */
2800 switch (server->dstaddr.ss_family) {
2801 case AF_INET:
2802 sa = (struct sockaddr_in *)&server->dstaddr;
2803 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2804 break;
2805 case AF_INET6:
2806 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2807 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2808 break;
2809 default:
2810 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2811 server->dstaddr.ss_family);
2812 rc = -EINVAL;
2813 goto out_err;
2814 }
2815
2816 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2817 key = request_key(&key_type_logon, desc, "");
2818 if (IS_ERR(key)) {
2819 if (!ses->domainName) {
2820 cifs_dbg(FYI, "domainName is NULL\n");
2821 rc = PTR_ERR(key);
2822 goto out_err;
2823 }
2824
2825 /* didn't work, try to find a domain key */
2826 sprintf(desc, "cifs:d:%s", ses->domainName);
2827 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2828 key = request_key(&key_type_logon, desc, "");
2829 if (IS_ERR(key)) {
2830 rc = PTR_ERR(key);
2831 goto out_err;
2832 }
2833 is_domain = 1;
2834 }
2835
2836 down_read(&key->sem);
2837 upayload = user_key_payload_locked(key);
2838 if (IS_ERR_OR_NULL(upayload)) {
2839 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
2840 goto out_key_put;
2841 }
2842
2843 /* find first : in payload */
2844 payload = upayload->data;
2845 delim = strnchr(payload, upayload->datalen, ':');
2846 cifs_dbg(FYI, "payload=%s\n", payload);
2847 if (!delim) {
2848 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2849 upayload->datalen);
2850 rc = -EINVAL;
2851 goto out_key_put;
2852 }
2853
2854 len = delim - payload;
2855 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
2856 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2857 len);
2858 rc = -EINVAL;
2859 goto out_key_put;
2860 }
2861
2862 vol->username = kstrndup(payload, len, GFP_KERNEL);
2863 if (!vol->username) {
2864 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2865 len);
2866 rc = -ENOMEM;
2867 goto out_key_put;
2868 }
2869 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
2870
2871 len = key->datalen - (len + 1);
2872 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
2873 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
2874 rc = -EINVAL;
2875 kfree(vol->username);
2876 vol->username = NULL;
2877 goto out_key_put;
2878 }
2879
2880 ++delim;
2881 vol->password = kstrndup(delim, len, GFP_KERNEL);
2882 if (!vol->password) {
2883 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2884 len);
2885 rc = -ENOMEM;
2886 kfree(vol->username);
2887 vol->username = NULL;
2888 goto out_key_put;
2889 }
2890
2891 /*
2892 * If we have a domain key then we must set the domainName in the
2893 * for the request.
2894 */
2895 if (is_domain && ses->domainName) {
2896 vol->domainname = kstrndup(ses->domainName,
2897 strlen(ses->domainName),
2898 GFP_KERNEL);
2899 if (!vol->domainname) {
2900 cifs_dbg(FYI, "Unable to allocate %zd bytes for "
2901 "domain\n", len);
2902 rc = -ENOMEM;
2903 kfree(vol->username);
2904 vol->username = NULL;
2905 kzfree(vol->password);
2906 vol->password = NULL;
2907 goto out_key_put;
2908 }
2909 }
2910
2911out_key_put:
2912 up_read(&key->sem);
2913 key_put(key);
2914out_err:
2915 kfree(desc);
2916 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
2917 return rc;
2918}
2919#else /* ! CONFIG_KEYS */
2920static inline int
2921cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2922 struct cifs_ses *ses __attribute__((unused)))
2923{
2924 return -ENOSYS;
2925}
2926#endif /* CONFIG_KEYS */
2927
2928/**
2929 * cifs_get_smb_ses - get a session matching @volume_info data from @server
2930 *
2931 * This function assumes it is being called from cifs_mount() where we
2932 * already got a server reference (server refcount +1). See
2933 * cifs_get_tcon() for refcount explanations.
2934 */
2935static struct cifs_ses *
2936cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2937{
2938 int rc = -ENOMEM;
2939 unsigned int xid;
2940 struct cifs_ses *ses;
2941 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2942 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2943
2944 xid = get_xid();
2945
2946 ses = cifs_find_smb_ses(server, volume_info);
2947 if (ses) {
2948 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2949 ses->status);
2950
2951 mutex_lock(&ses->session_mutex);
2952 rc = cifs_negotiate_protocol(xid, ses);
2953 if (rc) {
2954 mutex_unlock(&ses->session_mutex);
2955 /* problem -- put our ses reference */
2956 cifs_put_smb_ses(ses);
2957 free_xid(xid);
2958 return ERR_PTR(rc);
2959 }
2960 if (ses->need_reconnect) {
2961 cifs_dbg(FYI, "Session needs reconnect\n");
2962 rc = cifs_setup_session(xid, ses,
2963 volume_info->local_nls);
2964 if (rc) {
2965 mutex_unlock(&ses->session_mutex);
2966 /* problem -- put our reference */
2967 cifs_put_smb_ses(ses);
2968 free_xid(xid);
2969 return ERR_PTR(rc);
2970 }
2971 }
2972 mutex_unlock(&ses->session_mutex);
2973
2974 /* existing SMB ses has a server reference already */
2975 cifs_put_tcp_session(server, 0);
2976 free_xid(xid);
2977 return ses;
2978 }
2979
2980 cifs_dbg(FYI, "Existing smb sess not found\n");
2981 ses = sesInfoAlloc();
2982 if (ses == NULL)
2983 goto get_ses_fail;
2984
2985 /* new SMB session uses our server ref */
2986 ses->server = server;
2987 if (server->dstaddr.ss_family == AF_INET6)
2988 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
2989 else
2990 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
2991
2992 if (volume_info->username) {
2993 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2994 if (!ses->user_name)
2995 goto get_ses_fail;
2996 }
2997
2998 /* volume_info->password freed at unmount */
2999 if (volume_info->password) {
3000 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
3001 if (!ses->password)
3002 goto get_ses_fail;
3003 }
3004 if (volume_info->domainname) {
3005 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
3006 if (!ses->domainName)
3007 goto get_ses_fail;
3008 }
3009 if (volume_info->domainauto)
3010 ses->domainAuto = volume_info->domainauto;
3011 ses->cred_uid = volume_info->cred_uid;
3012 ses->linux_uid = volume_info->linux_uid;
3013
3014 ses->sectype = volume_info->sectype;
3015 ses->sign = volume_info->sign;
3016
3017 mutex_lock(&ses->session_mutex);
3018 rc = cifs_negotiate_protocol(xid, ses);
3019 if (!rc)
3020 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
3021 mutex_unlock(&ses->session_mutex);
3022 if (rc)
3023 goto get_ses_fail;
3024
3025 /* success, put it on the list */
3026 spin_lock(&cifs_tcp_ses_lock);
3027 list_add(&ses->smb_ses_list, &server->smb_ses_list);
3028 spin_unlock(&cifs_tcp_ses_lock);
3029
3030 free_xid(xid);
3031
3032 cifs_setup_ipc(ses, volume_info);
3033
3034 return ses;
3035
3036get_ses_fail:
3037 sesInfoFree(ses);
3038 free_xid(xid);
3039 return ERR_PTR(rc);
3040}
3041
3042static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
3043{
3044 if (tcon->tidStatus == CifsExiting)
3045 return 0;
3046 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
3047 return 0;
3048 if (tcon->seal != volume_info->seal)
3049 return 0;
3050 if (tcon->snapshot_time != volume_info->snapshot_time)
3051 return 0;
3052 if (tcon->no_lease != volume_info->no_lease)
3053 return 0;
3054 return 1;
3055}
3056
3057static struct cifs_tcon *
3058cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
3059{
3060 struct list_head *tmp;
3061 struct cifs_tcon *tcon;
3062
3063 spin_lock(&cifs_tcp_ses_lock);
3064 list_for_each(tmp, &ses->tcon_list) {
3065 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
3066 if (!match_tcon(tcon, volume_info))
3067 continue;
3068 ++tcon->tc_count;
3069 spin_unlock(&cifs_tcp_ses_lock);
3070 return tcon;
3071 }
3072 spin_unlock(&cifs_tcp_ses_lock);
3073 return NULL;
3074}
3075
3076void
3077cifs_put_tcon(struct cifs_tcon *tcon)
3078{
3079 unsigned int xid;
3080 struct cifs_ses *ses;
3081
3082 /*
3083 * IPC tcon share the lifetime of their session and are
3084 * destroyed in the session put function
3085 */
3086 if (tcon == NULL || tcon->ipc)
3087 return;
3088
3089 ses = tcon->ses;
3090 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
3091 spin_lock(&cifs_tcp_ses_lock);
3092 if (--tcon->tc_count > 0) {
3093 spin_unlock(&cifs_tcp_ses_lock);
3094 return;
3095 }
3096
3097 list_del_init(&tcon->tcon_list);
3098 spin_unlock(&cifs_tcp_ses_lock);
3099
3100 xid = get_xid();
3101 if (ses->server->ops->tree_disconnect)
3102 ses->server->ops->tree_disconnect(xid, tcon);
3103 _free_xid(xid);
3104
3105 cifs_fscache_release_super_cookie(tcon);
3106 tconInfoFree(tcon);
3107 cifs_put_smb_ses(ses);
3108}
3109
3110/**
3111 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
3112 *
3113 * - tcon refcount is the number of mount points using the tcon.
3114 * - ses refcount is the number of tcon using the session.
3115 *
3116 * 1. This function assumes it is being called from cifs_mount() where
3117 * we already got a session reference (ses refcount +1).
3118 *
3119 * 2. Since we're in the context of adding a mount point, the end
3120 * result should be either:
3121 *
3122 * a) a new tcon already allocated with refcount=1 (1 mount point) and
3123 * its session refcount incremented (1 new tcon). This +1 was
3124 * already done in (1).
3125 *
3126 * b) an existing tcon with refcount+1 (add a mount point to it) and
3127 * identical ses refcount (no new tcon). Because of (1) we need to
3128 * decrement the ses refcount.
3129 */
3130static struct cifs_tcon *
3131cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
3132{
3133 int rc, xid;
3134 struct cifs_tcon *tcon;
3135
3136 tcon = cifs_find_tcon(ses, volume_info);
3137 if (tcon) {
3138 /*
3139 * tcon has refcount already incremented but we need to
3140 * decrement extra ses reference gotten by caller (case b)
3141 */
3142 cifs_dbg(FYI, "Found match on UNC path\n");
3143 cifs_put_smb_ses(ses);
3144 return tcon;
3145 }
3146
3147 if (!ses->server->ops->tree_connect) {
3148 rc = -ENOSYS;
3149 goto out_fail;
3150 }
3151
3152 tcon = tconInfoAlloc();
3153 if (tcon == NULL) {
3154 rc = -ENOMEM;
3155 goto out_fail;
3156 }
3157
3158 if (volume_info->snapshot_time) {
3159 if (ses->server->vals->protocol_id == 0) {
3160 cifs_dbg(VFS,
3161 "Use SMB2 or later for snapshot mount option\n");
3162 rc = -EOPNOTSUPP;
3163 goto out_fail;
3164 } else
3165 tcon->snapshot_time = volume_info->snapshot_time;
3166 }
3167
3168 tcon->ses = ses;
3169 if (volume_info->password) {
3170 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3171 if (!tcon->password) {
3172 rc = -ENOMEM;
3173 goto out_fail;
3174 }
3175 }
3176
3177 if (volume_info->seal) {
3178 if (ses->server->vals->protocol_id == 0) {
3179 cifs_dbg(VFS,
3180 "SMB3 or later required for encryption\n");
3181 rc = -EOPNOTSUPP;
3182 goto out_fail;
3183 } else if (tcon->ses->server->capabilities &
3184 SMB2_GLOBAL_CAP_ENCRYPTION)
3185 tcon->seal = true;
3186 else {
3187 cifs_dbg(VFS, "Encryption is not supported on share\n");
3188 rc = -EOPNOTSUPP;
3189 goto out_fail;
3190 }
3191 }
3192
3193 if (volume_info->linux_ext) {
3194 if (ses->server->posix_ext_supported) {
3195 tcon->posix_extensions = true;
3196 printk_once(KERN_WARNING
3197 "SMB3.11 POSIX Extensions are experimental\n");
3198 } else {
3199 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
3200 rc = -EOPNOTSUPP;
3201 goto out_fail;
3202 }
3203 }
3204
3205 /*
3206 * BB Do we need to wrap session_mutex around this TCon call and Unix
3207 * SetFS as we do on SessSetup and reconnect?
3208 */
3209 xid = get_xid();
3210 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3211 volume_info->local_nls);
3212 free_xid(xid);
3213 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
3214 if (rc)
3215 goto out_fail;
3216
3217 if (volume_info->nodfs) {
3218 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
3219 cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
3220 }
3221 tcon->use_persistent = false;
3222 /* check if SMB2 or later, CIFS does not support persistent handles */
3223 if (volume_info->persistent) {
3224 if (ses->server->vals->protocol_id == 0) {
3225 cifs_dbg(VFS,
3226 "SMB3 or later required for persistent handles\n");
3227 rc = -EOPNOTSUPP;
3228 goto out_fail;
3229 } else if (ses->server->capabilities &
3230 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3231 tcon->use_persistent = true;
3232 else /* persistent handles requested but not supported */ {
3233 cifs_dbg(VFS,
3234 "Persistent handles not supported on share\n");
3235 rc = -EOPNOTSUPP;
3236 goto out_fail;
3237 }
3238 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3239 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3240 && (volume_info->nopersistent == false)) {
3241 cifs_dbg(FYI, "enabling persistent handles\n");
3242 tcon->use_persistent = true;
3243 } else if (volume_info->resilient) {
3244 if (ses->server->vals->protocol_id == 0) {
3245 cifs_dbg(VFS,
3246 "SMB2.1 or later required for resilient handles\n");
3247 rc = -EOPNOTSUPP;
3248 goto out_fail;
3249 }
3250 tcon->use_resilient = true;
3251 }
3252
3253 /*
3254 * We can have only one retry value for a connection to a share so for
3255 * resources mounted more than once to the same server share the last
3256 * value passed in for the retry flag is used.
3257 */
3258 tcon->retry = volume_info->retry;
3259 tcon->nocase = volume_info->nocase;
3260 tcon->nohandlecache = volume_info->nohandlecache;
3261 tcon->local_lease = volume_info->local_lease;
3262 tcon->no_lease = volume_info->no_lease;
3263 INIT_LIST_HEAD(&tcon->pending_opens);
3264
3265 spin_lock(&cifs_tcp_ses_lock);
3266 list_add(&tcon->tcon_list, &ses->tcon_list);
3267 spin_unlock(&cifs_tcp_ses_lock);
3268
3269 cifs_fscache_get_super_cookie(tcon);
3270
3271 return tcon;
3272
3273out_fail:
3274 tconInfoFree(tcon);
3275 return ERR_PTR(rc);
3276}
3277
3278void
3279cifs_put_tlink(struct tcon_link *tlink)
3280{
3281 if (!tlink || IS_ERR(tlink))
3282 return;
3283
3284 if (!atomic_dec_and_test(&tlink->tl_count) ||
3285 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3286 tlink->tl_time = jiffies;
3287 return;
3288 }
3289
3290 if (!IS_ERR(tlink_tcon(tlink)))
3291 cifs_put_tcon(tlink_tcon(tlink));
3292 kfree(tlink);
3293 return;
3294}
3295
3296static int
3297compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3298{
3299 struct cifs_sb_info *old = CIFS_SB(sb);
3300 struct cifs_sb_info *new = mnt_data->cifs_sb;
3301 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
3302 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
3303
3304 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3305 return 0;
3306
3307 if (old->mnt_cifs_serverino_autodisabled)
3308 newflags &= ~CIFS_MOUNT_SERVER_INUM;
3309
3310 if (oldflags != newflags)
3311 return 0;
3312
3313 /*
3314 * We want to share sb only if we don't specify an r/wsize or
3315 * specified r/wsize is greater than or equal to existing one.
3316 */
3317 if (new->wsize && new->wsize < old->wsize)
3318 return 0;
3319
3320 if (new->rsize && new->rsize < old->rsize)
3321 return 0;
3322
3323 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
3324 return 0;
3325
3326 if (old->mnt_file_mode != new->mnt_file_mode ||
3327 old->mnt_dir_mode != new->mnt_dir_mode)
3328 return 0;
3329
3330 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3331 return 0;
3332
3333 if (old->actimeo != new->actimeo)
3334 return 0;
3335
3336 return 1;
3337}
3338
3339static int
3340match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3341{
3342 struct cifs_sb_info *old = CIFS_SB(sb);
3343 struct cifs_sb_info *new = mnt_data->cifs_sb;
3344 bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
3345 bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
3346
3347 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
3348 return 1;
3349 else if (!old_set && !new_set)
3350 return 1;
3351
3352 return 0;
3353}
3354
3355int
3356cifs_match_super(struct super_block *sb, void *data)
3357{
3358 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3359 struct smb_vol *volume_info;
3360 struct cifs_sb_info *cifs_sb;
3361 struct TCP_Server_Info *tcp_srv;
3362 struct cifs_ses *ses;
3363 struct cifs_tcon *tcon;
3364 struct tcon_link *tlink;
3365 int rc = 0;
3366
3367 spin_lock(&cifs_tcp_ses_lock);
3368 cifs_sb = CIFS_SB(sb);
3369 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3370 if (IS_ERR(tlink)) {
3371 spin_unlock(&cifs_tcp_ses_lock);
3372 return rc;
3373 }
3374 tcon = tlink_tcon(tlink);
3375 ses = tcon->ses;
3376 tcp_srv = ses->server;
3377
3378 volume_info = mnt_data->vol;
3379
3380 if (!match_server(tcp_srv, volume_info) ||
3381 !match_session(ses, volume_info) ||
3382 !match_tcon(tcon, volume_info) ||
3383 !match_prepath(sb, mnt_data)) {
3384 rc = 0;
3385 goto out;
3386 }
3387
3388 rc = compare_mount_options(sb, mnt_data);
3389out:
3390 spin_unlock(&cifs_tcp_ses_lock);
3391 cifs_put_tlink(tlink);
3392 return rc;
3393}
3394
3395int
3396get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
3397 const struct nls_table *nls_codepage, unsigned int *num_referrals,
3398 struct dfs_info3_param **referrals, int remap)
3399{
3400 int rc = 0;
3401
3402 if (!ses->server->ops->get_dfs_refer)
3403 return -ENOSYS;
3404
3405 *num_referrals = 0;
3406 *referrals = NULL;
3407
3408 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
3409 referrals, num_referrals,
3410 nls_codepage, remap);
3411 return rc;
3412}
3413
3414#ifdef CONFIG_DEBUG_LOCK_ALLOC
3415static struct lock_class_key cifs_key[2];
3416static struct lock_class_key cifs_slock_key[2];
3417
3418static inline void
3419cifs_reclassify_socket4(struct socket *sock)
3420{
3421 struct sock *sk = sock->sk;
3422 BUG_ON(!sock_allow_reclassification(sk));
3423 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3424 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3425}
3426
3427static inline void
3428cifs_reclassify_socket6(struct socket *sock)
3429{
3430 struct sock *sk = sock->sk;
3431 BUG_ON(!sock_allow_reclassification(sk));
3432 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3433 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3434}
3435#else
3436static inline void
3437cifs_reclassify_socket4(struct socket *sock)
3438{
3439}
3440
3441static inline void
3442cifs_reclassify_socket6(struct socket *sock)
3443{
3444}
3445#endif
3446
3447/* See RFC1001 section 14 on representation of Netbios names */
3448static void rfc1002mangle(char *target, char *source, unsigned int length)
3449{
3450 unsigned int i, j;
3451
3452 for (i = 0, j = 0; i < (length); i++) {
3453 /* mask a nibble at a time and encode */
3454 target[j] = 'A' + (0x0F & (source[i] >> 4));
3455 target[j+1] = 'A' + (0x0F & source[i]);
3456 j += 2;
3457 }
3458
3459}
3460
3461static int
3462bind_socket(struct TCP_Server_Info *server)
3463{
3464 int rc = 0;
3465 if (server->srcaddr.ss_family != AF_UNSPEC) {
3466 /* Bind to the specified local IP address */
3467 struct socket *socket = server->ssocket;
3468 rc = socket->ops->bind(socket,
3469 (struct sockaddr *) &server->srcaddr,
3470 sizeof(server->srcaddr));
3471 if (rc < 0) {
3472 struct sockaddr_in *saddr4;
3473 struct sockaddr_in6 *saddr6;
3474 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3475 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3476 if (saddr6->sin6_family == AF_INET6)
3477 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3478 &saddr6->sin6_addr, rc);
3479 else
3480 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3481 &saddr4->sin_addr.s_addr, rc);
3482 }
3483 }
3484 return rc;
3485}
3486
3487static int
3488ip_rfc1001_connect(struct TCP_Server_Info *server)
3489{
3490 int rc = 0;
3491 /*
3492 * some servers require RFC1001 sessinit before sending
3493 * negprot - BB check reconnection in case where second
3494 * sessinit is sent but no second negprot
3495 */
3496 struct rfc1002_session_packet *ses_init_buf;
3497 struct smb_hdr *smb_buf;
3498 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3499 GFP_KERNEL);
3500 if (ses_init_buf) {
3501 ses_init_buf->trailer.session_req.called_len = 32;
3502
3503 if (server->server_RFC1001_name[0] != 0)
3504 rfc1002mangle(ses_init_buf->trailer.
3505 session_req.called_name,
3506 server->server_RFC1001_name,
3507 RFC1001_NAME_LEN_WITH_NULL);
3508 else
3509 rfc1002mangle(ses_init_buf->trailer.
3510 session_req.called_name,
3511 DEFAULT_CIFS_CALLED_NAME,
3512 RFC1001_NAME_LEN_WITH_NULL);
3513
3514 ses_init_buf->trailer.session_req.calling_len = 32;
3515
3516 /*
3517 * calling name ends in null (byte 16) from old smb
3518 * convention.
3519 */
3520 if (server->workstation_RFC1001_name[0] != 0)
3521 rfc1002mangle(ses_init_buf->trailer.
3522 session_req.calling_name,
3523 server->workstation_RFC1001_name,
3524 RFC1001_NAME_LEN_WITH_NULL);
3525 else
3526 rfc1002mangle(ses_init_buf->trailer.
3527 session_req.calling_name,
3528 "LINUX_CIFS_CLNT",
3529 RFC1001_NAME_LEN_WITH_NULL);
3530
3531 ses_init_buf->trailer.session_req.scope1 = 0;
3532 ses_init_buf->trailer.session_req.scope2 = 0;
3533 smb_buf = (struct smb_hdr *)ses_init_buf;
3534
3535 /* sizeof RFC1002_SESSION_REQUEST with no scope */
3536 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
3537 rc = smb_send(server, smb_buf, 0x44);
3538 kfree(ses_init_buf);
3539 /*
3540 * RFC1001 layer in at least one server
3541 * requires very short break before negprot
3542 * presumably because not expecting negprot
3543 * to follow so fast. This is a simple
3544 * solution that works without
3545 * complicating the code and causes no
3546 * significant slowing down on mount
3547 * for everyone else
3548 */
3549 usleep_range(1000, 2000);
3550 }
3551 /*
3552 * else the negprot may still work without this
3553 * even though malloc failed
3554 */
3555
3556 return rc;
3557}
3558
3559static int
3560generic_ip_connect(struct TCP_Server_Info *server)
3561{
3562 int rc = 0;
3563 __be16 sport;
3564 int slen, sfamily;
3565 struct socket *socket = server->ssocket;
3566 struct sockaddr *saddr;
3567
3568 saddr = (struct sockaddr *) &server->dstaddr;
3569
3570 if (server->dstaddr.ss_family == AF_INET6) {
3571 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3572 slen = sizeof(struct sockaddr_in6);
3573 sfamily = AF_INET6;
3574 } else {
3575 sport = ((struct sockaddr_in *) saddr)->sin_port;
3576 slen = sizeof(struct sockaddr_in);
3577 sfamily = AF_INET;
3578 }
3579
3580 if (socket == NULL) {
3581 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3582 IPPROTO_TCP, &socket, 1);
3583 if (rc < 0) {
3584 cifs_dbg(VFS, "Error %d creating socket\n", rc);
3585 server->ssocket = NULL;
3586 return rc;
3587 }
3588
3589 /* BB other socket options to set KEEPALIVE, NODELAY? */
3590 cifs_dbg(FYI, "Socket created\n");
3591 server->ssocket = socket;
3592 socket->sk->sk_allocation = GFP_NOFS;
3593 if (sfamily == AF_INET6)
3594 cifs_reclassify_socket6(socket);
3595 else
3596 cifs_reclassify_socket4(socket);
3597 }
3598
3599 rc = bind_socket(server);
3600 if (rc < 0)
3601 return rc;
3602
3603 /*
3604 * Eventually check for other socket options to change from
3605 * the default. sock_setsockopt not used because it expects
3606 * user space buffer
3607 */
3608 socket->sk->sk_rcvtimeo = 7 * HZ;
3609 socket->sk->sk_sndtimeo = 5 * HZ;
3610
3611 /* make the bufsizes depend on wsize/rsize and max requests */
3612 if (server->noautotune) {
3613 if (socket->sk->sk_sndbuf < (200 * 1024))
3614 socket->sk->sk_sndbuf = 200 * 1024;
3615 if (socket->sk->sk_rcvbuf < (140 * 1024))
3616 socket->sk->sk_rcvbuf = 140 * 1024;
3617 }
3618
3619 if (server->tcp_nodelay) {
3620 int val = 1;
3621 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3622 (char *)&val, sizeof(val));
3623 if (rc)
3624 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3625 rc);
3626 }
3627
3628 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
3629 socket->sk->sk_sndbuf,
3630 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3631
3632 rc = socket->ops->connect(socket, saddr, slen, 0);
3633 if (rc < 0) {
3634 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
3635 sock_release(socket);
3636 server->ssocket = NULL;
3637 return rc;
3638 }
3639
3640 if (sport == htons(RFC1001_PORT))
3641 rc = ip_rfc1001_connect(server);
3642
3643 return rc;
3644}
3645
3646static int
3647ip_connect(struct TCP_Server_Info *server)
3648{
3649 __be16 *sport;
3650 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3651 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3652
3653 if (server->dstaddr.ss_family == AF_INET6)
3654 sport = &addr6->sin6_port;
3655 else
3656 sport = &addr->sin_port;
3657
3658 if (*sport == 0) {
3659 int rc;
3660
3661 /* try with 445 port at first */
3662 *sport = htons(CIFS_PORT);
3663
3664 rc = generic_ip_connect(server);
3665 if (rc >= 0)
3666 return rc;
3667
3668 /* if it failed, try with 139 port */
3669 *sport = htons(RFC1001_PORT);
3670 }
3671
3672 return generic_ip_connect(server);
3673}
3674
3675void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
3676 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
3677{
3678 /* if we are reconnecting then should we check to see if
3679 * any requested capabilities changed locally e.g. via
3680 * remount but we can not do much about it here
3681 * if they have (even if we could detect it by the following)
3682 * Perhaps we could add a backpointer to array of sb from tcon
3683 * or if we change to make all sb to same share the same
3684 * sb as NFS - then we only have one backpointer to sb.
3685 * What if we wanted to mount the server share twice once with
3686 * and once without posixacls or posix paths? */
3687 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3688
3689 if (vol_info && vol_info->no_linux_ext) {
3690 tcon->fsUnixInfo.Capability = 0;
3691 tcon->unix_ext = 0; /* Unix Extensions disabled */
3692 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
3693 return;
3694 } else if (vol_info)
3695 tcon->unix_ext = 1; /* Unix Extensions supported */
3696
3697 if (tcon->unix_ext == 0) {
3698 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
3699 return;
3700 }
3701
3702 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
3703 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3704 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
3705 /* check for reconnect case in which we do not
3706 want to change the mount behavior if we can avoid it */
3707 if (vol_info == NULL) {
3708 /* turn off POSIX ACL and PATHNAMES if not set
3709 originally at mount time */
3710 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3711 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3712 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3713 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3714 cifs_dbg(VFS, "POSIXPATH support change\n");
3715 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3716 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3717 cifs_dbg(VFS, "possible reconnect error\n");
3718 cifs_dbg(VFS, "server disabled POSIX path support\n");
3719 }
3720 }
3721
3722 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3723 cifs_dbg(VFS, "per-share encryption not supported yet\n");
3724
3725 cap &= CIFS_UNIX_CAP_MASK;
3726 if (vol_info && vol_info->no_psx_acl)
3727 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3728 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
3729 cifs_dbg(FYI, "negotiated posix acl support\n");
3730 if (cifs_sb)
3731 cifs_sb->mnt_cifs_flags |=
3732 CIFS_MOUNT_POSIXACL;
3733 }
3734
3735 if (vol_info && vol_info->posix_paths == 0)
3736 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3737 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3738 cifs_dbg(FYI, "negotiate posix pathnames\n");
3739 if (cifs_sb)
3740 cifs_sb->mnt_cifs_flags |=
3741 CIFS_MOUNT_POSIX_PATHS;
3742 }
3743
3744 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
3745#ifdef CONFIG_CIFS_DEBUG2
3746 if (cap & CIFS_UNIX_FCNTL_CAP)
3747 cifs_dbg(FYI, "FCNTL cap\n");
3748 if (cap & CIFS_UNIX_EXTATTR_CAP)
3749 cifs_dbg(FYI, "EXTATTR cap\n");
3750 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3751 cifs_dbg(FYI, "POSIX path cap\n");
3752 if (cap & CIFS_UNIX_XATTR_CAP)
3753 cifs_dbg(FYI, "XATTR cap\n");
3754 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
3755 cifs_dbg(FYI, "POSIX ACL cap\n");
3756 if (cap & CIFS_UNIX_LARGE_READ_CAP)
3757 cifs_dbg(FYI, "very large read cap\n");
3758 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
3759 cifs_dbg(FYI, "very large write cap\n");
3760 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
3761 cifs_dbg(FYI, "transport encryption cap\n");
3762 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3763 cifs_dbg(FYI, "mandatory transport encryption cap\n");
3764#endif /* CIFS_DEBUG2 */
3765 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
3766 if (vol_info == NULL) {
3767 cifs_dbg(FYI, "resetting capabilities failed\n");
3768 } else
3769 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");
3770
3771 }
3772 }
3773}
3774
3775int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
3776 struct cifs_sb_info *cifs_sb)
3777{
3778 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3779
3780 spin_lock_init(&cifs_sb->tlink_tree_lock);
3781 cifs_sb->tlink_tree = RB_ROOT;
3782
3783 /*
3784 * Temporarily set r/wsize for matching superblock. If we end up using
3785 * new sb then client will later negotiate it downward if needed.
3786 */
3787 cifs_sb->rsize = pvolume_info->rsize;
3788 cifs_sb->wsize = pvolume_info->wsize;
3789
3790 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3791 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3792 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3793 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
3794 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3795 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
3796
3797 cifs_sb->actimeo = pvolume_info->actimeo;
3798 cifs_sb->local_nls = pvolume_info->local_nls;
3799
3800 if (pvolume_info->noperm)
3801 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3802 if (pvolume_info->setuids)
3803 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
3804 if (pvolume_info->setuidfromacl)
3805 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
3806 if (pvolume_info->server_ino)
3807 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3808 if (pvolume_info->remap)
3809 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3810 if (pvolume_info->sfu_remap)
3811 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3812 if (pvolume_info->no_xattr)
3813 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3814 if (pvolume_info->sfu_emul)
3815 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3816 if (pvolume_info->nobrl)
3817 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
3818 if (pvolume_info->nohandlecache)
3819 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
3820 if (pvolume_info->nostrictsync)
3821 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
3822 if (pvolume_info->mand_lock)
3823 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
3824 if (pvolume_info->rwpidforward)
3825 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
3826 if (pvolume_info->cifs_acl)
3827 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
3828 if (pvolume_info->backupuid_specified) {
3829 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
3830 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3831 }
3832 if (pvolume_info->backupgid_specified) {
3833 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
3834 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3835 }
3836 if (pvolume_info->override_uid)
3837 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3838 if (pvolume_info->override_gid)
3839 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3840 if (pvolume_info->dynperm)
3841 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
3842 if (pvolume_info->fsc)
3843 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
3844 if (pvolume_info->multiuser)
3845 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3846 CIFS_MOUNT_NO_PERM);
3847 if (pvolume_info->strict_io)
3848 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
3849 if (pvolume_info->direct_io) {
3850 cifs_dbg(FYI, "mounting share using direct i/o\n");
3851 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3852 }
3853 if (pvolume_info->mfsymlinks) {
3854 if (pvolume_info->sfu_emul) {
3855 /*
3856 * Our SFU ("Services for Unix" emulation does not allow
3857 * creating symlinks but does allow reading existing SFU
3858 * symlinks (it does allow both creating and reading SFU
3859 * style mknod and FIFOs though). When "mfsymlinks" and
3860 * "sfu" are both enabled at the same time, it allows
3861 * reading both types of symlinks, but will only create
3862 * them with mfsymlinks format. This allows better
3863 * Apple compatibility (probably better for Samba too)
3864 * while still recognizing old Windows style symlinks.
3865 */
3866 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
3867 }
3868 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
3869 }
3870
3871 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
3872 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
3873
3874 if (pvolume_info->prepath) {
3875 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3876 if (cifs_sb->prepath == NULL)
3877 return -ENOMEM;
3878 }
3879
3880 return 0;
3881}
3882
3883static void
3884cleanup_volume_info_contents(struct smb_vol *volume_info)
3885{
3886 kfree(volume_info->username);
3887 kzfree(volume_info->password);
3888 kfree(volume_info->UNC);
3889 kfree(volume_info->domainname);
3890 kfree(volume_info->iocharset);
3891 kfree(volume_info->prepath);
3892}
3893
3894void
3895cifs_cleanup_volume_info(struct smb_vol *volume_info)
3896{
3897 if (!volume_info)
3898 return;
3899 cleanup_volume_info_contents(volume_info);
3900 kfree(volume_info);
3901}
3902
3903
3904#ifdef CONFIG_CIFS_DFS_UPCALL
3905/*
3906 * cifs_build_path_to_root returns full path to root when we do not have an
3907 * exiting connection (tcon)
3908 */
3909static char *
3910build_unc_path_to_root(const struct smb_vol *vol,
3911 const struct cifs_sb_info *cifs_sb)
3912{
3913 char *full_path, *pos;
3914 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
3915 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
3916
3917 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
3918 if (full_path == NULL)
3919 return ERR_PTR(-ENOMEM);
3920
3921 strncpy(full_path, vol->UNC, unc_len);
3922 pos = full_path + unc_len;
3923
3924 if (pplen) {
3925 *pos = CIFS_DIR_SEP(cifs_sb);
3926 strncpy(pos + 1, vol->prepath, pplen);
3927 pos += pplen;
3928 }
3929
3930 *pos = '\0'; /* add trailing null */
3931 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
3932 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
3933 return full_path;
3934}
3935
3936/*
3937 * Perform a dfs referral query for a share and (optionally) prefix
3938 *
3939 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3940 * to a string containing updated options for the submount. Otherwise it
3941 * will be left untouched.
3942 *
3943 * Returns the rc from get_dfs_path to the caller, which can be used to
3944 * determine whether there were referrals.
3945 */
3946static int
3947expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
3948 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
3949 int check_prefix)
3950{
3951 int rc;
3952 unsigned int num_referrals = 0;
3953 struct dfs_info3_param *referrals = NULL;
3954 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3955
3956 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3957 if (IS_ERR(full_path))
3958 return PTR_ERR(full_path);
3959
3960 /* For DFS paths, skip the first '\' of the UNC */
3961 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3962
3963 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
3964 &num_referrals, &referrals, cifs_remap(cifs_sb));
3965
3966 if (!rc && num_referrals > 0) {
3967 char *fake_devname = NULL;
3968
3969 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3970 full_path + 1, referrals,
3971 &fake_devname);
3972
3973 free_dfs_info_array(referrals, num_referrals);
3974
3975 if (IS_ERR(mdata)) {
3976 rc = PTR_ERR(mdata);
3977 mdata = NULL;
3978 } else {
3979 cleanup_volume_info_contents(volume_info);
3980 rc = cifs_setup_volume_info(volume_info, mdata,
3981 fake_devname, false);
3982 }
3983 kfree(fake_devname);
3984 kfree(cifs_sb->mountdata);
3985 cifs_sb->mountdata = mdata;
3986 }
3987 kfree(full_path);
3988 return rc;
3989}
3990#endif
3991
3992static int
3993cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
3994 const char *devname, bool is_smb3)
3995{
3996 int rc = 0;
3997
3998 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
3999 return -EINVAL;
4000
4001 if (volume_info->nullauth) {
4002 cifs_dbg(FYI, "Anonymous login\n");
4003 kfree(volume_info->username);
4004 volume_info->username = NULL;
4005 } else if (volume_info->username) {
4006 /* BB fixme parse for domain name here */
4007 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
4008 } else {
4009 cifs_dbg(VFS, "No username specified\n");
4010 /* In userspace mount helper we can get user name from alternate
4011 locations such as env variables and files on disk */
4012 return -EINVAL;
4013 }
4014
4015 /* this is needed for ASCII cp to Unicode converts */
4016 if (volume_info->iocharset == NULL) {
4017 /* load_nls_default cannot return null */
4018 volume_info->local_nls = load_nls_default();
4019 } else {
4020 volume_info->local_nls = load_nls(volume_info->iocharset);
4021 if (volume_info->local_nls == NULL) {
4022 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
4023 volume_info->iocharset);
4024 return -ELIBACC;
4025 }
4026 }
4027
4028 return rc;
4029}
4030
4031struct smb_vol *
4032cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
4033{
4034 int rc;
4035 struct smb_vol *volume_info;
4036
4037 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
4038 if (!volume_info)
4039 return ERR_PTR(-ENOMEM);
4040
4041 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
4042 if (rc) {
4043 cifs_cleanup_volume_info(volume_info);
4044 volume_info = ERR_PTR(rc);
4045 }
4046
4047 return volume_info;
4048}
4049
4050static int
4051cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
4052 unsigned int xid,
4053 struct cifs_tcon *tcon,
4054 struct cifs_sb_info *cifs_sb,
4055 char *full_path)
4056{
4057 int rc;
4058 char *s;
4059 char sep, tmp;
4060
4061 sep = CIFS_DIR_SEP(cifs_sb);
4062 s = full_path;
4063
4064 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
4065 while (rc == 0) {
4066 /* skip separators */
4067 while (*s == sep)
4068 s++;
4069 if (!*s)
4070 break;
4071 /* next separator */
4072 while (*s && *s != sep)
4073 s++;
4074
4075 /*
4076 * temporarily null-terminate the path at the end of
4077 * the current component
4078 */
4079 tmp = *s;
4080 *s = 0;
4081 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4082 full_path);
4083 *s = tmp;
4084 }
4085 return rc;
4086}
4087
4088int
4089cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
4090{
4091 int rc;
4092 unsigned int xid;
4093 struct cifs_ses *ses;
4094 struct cifs_tcon *tcon;
4095 struct TCP_Server_Info *server;
4096 char *full_path;
4097 struct tcon_link *tlink;
4098#ifdef CONFIG_CIFS_DFS_UPCALL
4099 int referral_walks_count = 0;
4100#endif
4101
4102#ifdef CONFIG_CIFS_DFS_UPCALL
4103try_mount_again:
4104 /* cleanup activities if we're chasing a referral */
4105 if (referral_walks_count) {
4106 if (tcon)
4107 cifs_put_tcon(tcon);
4108 else if (ses)
4109 cifs_put_smb_ses(ses);
4110
4111 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
4112
4113 free_xid(xid);
4114 }
4115#endif
4116 rc = 0;
4117 tcon = NULL;
4118 ses = NULL;
4119 server = NULL;
4120 full_path = NULL;
4121 tlink = NULL;
4122
4123 xid = get_xid();
4124
4125 /* get a reference to a tcp session */
4126 server = cifs_get_tcp_session(volume_info);
4127 if (IS_ERR(server)) {
4128 rc = PTR_ERR(server);
4129 goto out;
4130 }
4131 if ((volume_info->max_credits < 20) ||
4132 (volume_info->max_credits > 60000))
4133 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
4134 else
4135 server->max_credits = volume_info->max_credits;
4136 /* get a reference to a SMB session */
4137 ses = cifs_get_smb_ses(server, volume_info);
4138 if (IS_ERR(ses)) {
4139 rc = PTR_ERR(ses);
4140 ses = NULL;
4141 goto mount_fail_check;
4142 }
4143
4144 if ((volume_info->persistent == true) && ((ses->server->capabilities &
4145 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) == 0)) {
4146 cifs_dbg(VFS, "persistent handles not supported by server\n");
4147 rc = -EOPNOTSUPP;
4148 goto mount_fail_check;
4149 }
4150
4151 /* search for existing tcon to this server share */
4152 tcon = cifs_get_tcon(ses, volume_info);
4153 if (IS_ERR(tcon)) {
4154 rc = PTR_ERR(tcon);
4155 tcon = NULL;
4156 if (rc == -EACCES)
4157 goto mount_fail_check;
4158
4159 goto remote_path_check;
4160 }
4161
4162 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4163 if (tcon->posix_extensions)
4164 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
4165
4166 /* tell server which Unix caps we support */
4167 if (cap_unix(tcon->ses)) {
4168 /* reset of caps checks mount to see if unix extensions
4169 disabled for just this mount */
4170 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
4171 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
4172 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
4173 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
4174 rc = -EACCES;
4175 goto mount_fail_check;
4176 }
4177 } else
4178 tcon->unix_ext = 0; /* server does not support them */
4179
4180 /* do not care if a following call succeed - informational */
4181 if (!tcon->pipe && server->ops->qfs_tcon)
4182 server->ops->qfs_tcon(xid, tcon);
4183
4184 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
4185 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
4186
4187remote_path_check:
4188#ifdef CONFIG_CIFS_DFS_UPCALL
4189 /*
4190 * Perform an unconditional check for whether there are DFS
4191 * referrals for this path without prefix, to provide support
4192 * for DFS referrals from w2k8 servers which don't seem to respond
4193 * with PATH_NOT_COVERED to requests that include the prefix.
4194 * Chase the referral if found, otherwise continue normally.
4195 */
4196 if (referral_walks_count == 0) {
4197 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
4198 false);
4199 if (!refrc) {
4200 referral_walks_count++;
4201 goto try_mount_again;
4202 }
4203 }
4204#endif
4205
4206 /* check if a whole path is not remote */
4207 if (!rc && tcon) {
4208 if (!server->ops->is_path_accessible) {
4209 rc = -ENOSYS;
4210 goto mount_fail_check;
4211 }
4212 /*
4213 * cifs_build_path_to_root works only when we have a valid tcon
4214 */
4215 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon,
4216 tcon->Flags & SMB_SHARE_IS_IN_DFS);
4217 if (full_path == NULL) {
4218 rc = -ENOMEM;
4219 goto mount_fail_check;
4220 }
4221 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4222 full_path);
4223 if (rc != 0 && rc != -EREMOTE) {
4224 kfree(full_path);
4225 goto mount_fail_check;
4226 }
4227
4228 if (rc != -EREMOTE) {
4229 rc = cifs_are_all_path_components_accessible(server,
4230 xid, tcon, cifs_sb,
4231 full_path);
4232 if (rc != 0) {
4233 cifs_dbg(VFS, "cannot query dirs between root and final path, "
4234 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4235 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4236 rc = 0;
4237 }
4238 }
4239 kfree(full_path);
4240 }
4241
4242 /* get referral if needed */
4243 if (rc == -EREMOTE) {
4244#ifdef CONFIG_CIFS_DFS_UPCALL
4245 if (referral_walks_count > MAX_NESTED_LINKS) {
4246 /*
4247 * BB: when we implement proper loop detection,
4248 * we will remove this check. But now we need it
4249 * to prevent an indefinite loop if 'DFS tree' is
4250 * misconfigured (i.e. has loops).
4251 */
4252 rc = -ELOOP;
4253 goto mount_fail_check;
4254 }
4255
4256 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
4257
4258 if (!rc) {
4259 referral_walks_count++;
4260 goto try_mount_again;
4261 }
4262 goto mount_fail_check;
4263#else /* No DFS support, return error on mount */
4264 rc = -EOPNOTSUPP;
4265#endif
4266 }
4267
4268 if (rc)
4269 goto mount_fail_check;
4270
4271 /* now, hang the tcon off of the superblock */
4272 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
4273 if (tlink == NULL) {
4274 rc = -ENOMEM;
4275 goto mount_fail_check;
4276 }
4277
4278 tlink->tl_uid = ses->linux_uid;
4279 tlink->tl_tcon = tcon;
4280 tlink->tl_time = jiffies;
4281 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
4282 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4283
4284 cifs_sb->master_tlink = tlink;
4285 spin_lock(&cifs_sb->tlink_tree_lock);
4286 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4287 spin_unlock(&cifs_sb->tlink_tree_lock);
4288
4289 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
4290 TLINK_IDLE_EXPIRE);
4291
4292mount_fail_check:
4293 /* on error free sesinfo and tcon struct if needed */
4294 if (rc) {
4295 /* If find_unc succeeded then rc == 0 so we can not end */
4296 /* up accidentally freeing someone elses tcon struct */
4297 if (tcon)
4298 cifs_put_tcon(tcon);
4299 else if (ses)
4300 cifs_put_smb_ses(ses);
4301 else
4302 cifs_put_tcp_session(server, 0);
4303 }
4304
4305out:
4306 free_xid(xid);
4307 return rc;
4308}
4309
4310/*
4311 * Issue a TREE_CONNECT request.
4312 */
4313int
4314CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
4315 const char *tree, struct cifs_tcon *tcon,
4316 const struct nls_table *nls_codepage)
4317{
4318 struct smb_hdr *smb_buffer;
4319 struct smb_hdr *smb_buffer_response;
4320 TCONX_REQ *pSMB;
4321 TCONX_RSP *pSMBr;
4322 unsigned char *bcc_ptr;
4323 int rc = 0;
4324 int length;
4325 __u16 bytes_left, count;
4326
4327 if (ses == NULL)
4328 return -EIO;
4329
4330 smb_buffer = cifs_buf_get();
4331 if (smb_buffer == NULL)
4332 return -ENOMEM;
4333
4334 smb_buffer_response = smb_buffer;
4335
4336 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4337 NULL /*no tid */ , 4 /*wct */ );
4338
4339 smb_buffer->Mid = get_next_mid(ses->server);
4340 smb_buffer->Uid = ses->Suid;
4341 pSMB = (TCONX_REQ *) smb_buffer;
4342 pSMBr = (TCONX_RSP *) smb_buffer_response;
4343
4344 pSMB->AndXCommand = 0xFF;
4345 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
4346 bcc_ptr = &pSMB->Password[0];
4347 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
4348 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
4349 *bcc_ptr = 0; /* password is null byte */
4350 bcc_ptr++; /* skip password */
4351 /* already aligned so no need to do it below */
4352 } else {
4353 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
4354 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4355 specified as required (when that support is added to
4356 the vfs in the future) as only NTLM or the much
4357 weaker LANMAN (which we do not send by default) is accepted
4358 by Samba (not sure whether other servers allow
4359 NTLMv2 password here) */
4360#ifdef CONFIG_CIFS_WEAK_PW_HASH
4361 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
4362 (ses->sectype == LANMAN))
4363 calc_lanman_hash(tcon->password, ses->server->cryptkey,
4364 ses->server->sec_mode &
4365 SECMODE_PW_ENCRYPT ? true : false,
4366 bcc_ptr);
4367 else
4368#endif /* CIFS_WEAK_PW_HASH */
4369 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
4370 bcc_ptr, nls_codepage);
4371 if (rc) {
4372 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4373 __func__, rc);
4374 cifs_buf_release(smb_buffer);
4375 return rc;
4376 }
4377
4378 bcc_ptr += CIFS_AUTH_RESP_SIZE;
4379 if (ses->capabilities & CAP_UNICODE) {
4380 /* must align unicode strings */
4381 *bcc_ptr = 0; /* null byte password */
4382 bcc_ptr++;
4383 }
4384 }
4385
4386 if (ses->server->sign)
4387 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4388
4389 if (ses->capabilities & CAP_STATUS32) {
4390 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4391 }
4392 if (ses->capabilities & CAP_DFS) {
4393 smb_buffer->Flags2 |= SMBFLG2_DFS;
4394 }
4395 if (ses->capabilities & CAP_UNICODE) {
4396 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4397 length =
4398 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
4399 6 /* max utf8 char length in bytes */ *
4400 (/* server len*/ + 256 /* share len */), nls_codepage);
4401 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
4402 bcc_ptr += 2; /* skip trailing null */
4403 } else { /* ASCII */
4404 strcpy(bcc_ptr, tree);
4405 bcc_ptr += strlen(tree) + 1;
4406 }
4407 strcpy(bcc_ptr, "?????");
4408 bcc_ptr += strlen("?????");
4409 bcc_ptr += 1;
4410 count = bcc_ptr - &pSMB->Password[0];
4411 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4412 pSMB->hdr.smb_buf_length) + count);
4413 pSMB->ByteCount = cpu_to_le16(count);
4414
4415 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
4416 0);
4417
4418 /* above now done in SendReceive */
4419 if (rc == 0) {
4420 bool is_unicode;
4421
4422 tcon->tidStatus = CifsGood;
4423 tcon->need_reconnect = false;
4424 tcon->tid = smb_buffer_response->Tid;
4425 bcc_ptr = pByteArea(smb_buffer_response);
4426 bytes_left = get_bcc(smb_buffer_response);
4427 length = strnlen(bcc_ptr, bytes_left - 2);
4428 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4429 is_unicode = true;
4430 else
4431 is_unicode = false;
4432
4433
4434 /* skip service field (NB: this field is always ASCII) */
4435 if (length == 3) {
4436 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4437 (bcc_ptr[2] == 'C')) {
4438 cifs_dbg(FYI, "IPC connection\n");
4439 tcon->ipc = true;
4440 tcon->pipe = true;
4441 }
4442 } else if (length == 2) {
4443 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4444 /* the most common case */
4445 cifs_dbg(FYI, "disk share connection\n");
4446 }
4447 }
4448 bcc_ptr += length + 1;
4449 bytes_left -= (length + 1);
4450 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
4451
4452 /* mostly informational -- no need to fail on error here */
4453 kfree(tcon->nativeFileSystem);
4454 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
4455 bytes_left, is_unicode,
4456 nls_codepage);
4457
4458 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
4459
4460 if ((smb_buffer_response->WordCount == 3) ||
4461 (smb_buffer_response->WordCount == 7))
4462 /* field is in same location */
4463 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4464 else
4465 tcon->Flags = 0;
4466 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
4467 }
4468
4469 cifs_buf_release(smb_buffer);
4470 return rc;
4471}
4472
4473static void delayed_free(struct rcu_head *p)
4474{
4475 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4476 unload_nls(sbi->local_nls);
4477 kfree(sbi);
4478}
4479
4480void
4481cifs_umount(struct cifs_sb_info *cifs_sb)
4482{
4483 struct rb_root *root = &cifs_sb->tlink_tree;
4484 struct rb_node *node;
4485 struct tcon_link *tlink;
4486
4487 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4488
4489 spin_lock(&cifs_sb->tlink_tree_lock);
4490 while ((node = rb_first(root))) {
4491 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4492 cifs_get_tlink(tlink);
4493 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4494 rb_erase(node, root);
4495
4496 spin_unlock(&cifs_sb->tlink_tree_lock);
4497 cifs_put_tlink(tlink);
4498 spin_lock(&cifs_sb->tlink_tree_lock);
4499 }
4500 spin_unlock(&cifs_sb->tlink_tree_lock);
4501
4502 kfree(cifs_sb->mountdata);
4503 kfree(cifs_sb->prepath);
4504 call_rcu(&cifs_sb->rcu, delayed_free);
4505}
4506
4507int
4508cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
4509{
4510 int rc = 0;
4511 struct TCP_Server_Info *server = ses->server;
4512
4513 if (!server->ops->need_neg || !server->ops->negotiate)
4514 return -ENOSYS;
4515
4516 /* only send once per connect */
4517 if (!server->ops->need_neg(server))
4518 return 0;
4519
4520 set_credits(server, 1);
4521
4522 rc = server->ops->negotiate(xid, ses);
4523 if (rc == 0) {
4524 spin_lock(&GlobalMid_Lock);
4525 if (server->tcpStatus == CifsNeedNegotiate)
4526 server->tcpStatus = CifsGood;
4527 else
4528 rc = -EHOSTDOWN;
4529 spin_unlock(&GlobalMid_Lock);
4530 }
4531
4532 return rc;
4533}
4534
4535int
4536cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4537 struct nls_table *nls_info)
4538{
4539 int rc = -ENOSYS;
4540 struct TCP_Server_Info *server = ses->server;
4541
4542 ses->capabilities = server->capabilities;
4543 if (linuxExtEnabled == 0)
4544 ses->capabilities &= (~server->vals->cap_unix);
4545
4546 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
4547 server->sec_mode, server->capabilities, server->timeAdj);
4548
4549 if (ses->auth_key.response) {
4550 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
4551 ses->auth_key.response);
4552 kfree(ses->auth_key.response);
4553 ses->auth_key.response = NULL;
4554 ses->auth_key.len = 0;
4555 }
4556
4557 if (server->ops->sess_setup)
4558 rc = server->ops->sess_setup(xid, ses, nls_info);
4559
4560 if (rc)
4561 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
4562
4563 return rc;
4564}
4565
4566static int
4567cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4568{
4569 vol->sectype = ses->sectype;
4570
4571 /* krb5 is special, since we don't need username or pw */
4572 if (vol->sectype == Kerberos)
4573 return 0;
4574
4575 return cifs_set_cifscreds(vol, ses);
4576}
4577
4578static struct cifs_tcon *
4579cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
4580{
4581 int rc;
4582 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4583 struct cifs_ses *ses;
4584 struct cifs_tcon *tcon = NULL;
4585 struct smb_vol *vol_info;
4586
4587 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
4588 if (vol_info == NULL)
4589 return ERR_PTR(-ENOMEM);
4590
4591 vol_info->local_nls = cifs_sb->local_nls;
4592 vol_info->linux_uid = fsuid;
4593 vol_info->cred_uid = fsuid;
4594 vol_info->UNC = master_tcon->treeName;
4595 vol_info->retry = master_tcon->retry;
4596 vol_info->nocase = master_tcon->nocase;
4597 vol_info->nohandlecache = master_tcon->nohandlecache;
4598 vol_info->local_lease = master_tcon->local_lease;
4599 vol_info->no_linux_ext = !master_tcon->unix_ext;
4600 vol_info->sectype = master_tcon->ses->sectype;
4601 vol_info->sign = master_tcon->ses->sign;
4602
4603 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4604 if (rc) {
4605 tcon = ERR_PTR(rc);
4606 goto out;
4607 }
4608
4609 /* get a reference for the same TCP session */
4610 spin_lock(&cifs_tcp_ses_lock);
4611 ++master_tcon->ses->server->srv_count;
4612 spin_unlock(&cifs_tcp_ses_lock);
4613
4614 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4615 if (IS_ERR(ses)) {
4616 tcon = (struct cifs_tcon *)ses;
4617 cifs_put_tcp_session(master_tcon->ses->server, 0);
4618 goto out;
4619 }
4620
4621 tcon = cifs_get_tcon(ses, vol_info);
4622 if (IS_ERR(tcon)) {
4623 cifs_put_smb_ses(ses);
4624 goto out;
4625 }
4626
4627 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4628 if (tcon->posix_extensions)
4629 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
4630
4631 if (cap_unix(ses))
4632 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
4633
4634out:
4635 kfree(vol_info->username);
4636 kzfree(vol_info->password);
4637 kfree(vol_info);
4638
4639 return tcon;
4640}
4641
4642struct cifs_tcon *
4643cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4644{
4645 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4646}
4647
4648/* find and return a tlink with given uid */
4649static struct tcon_link *
4650tlink_rb_search(struct rb_root *root, kuid_t uid)
4651{
4652 struct rb_node *node = root->rb_node;
4653 struct tcon_link *tlink;
4654
4655 while (node) {
4656 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4657
4658 if (uid_gt(tlink->tl_uid, uid))
4659 node = node->rb_left;
4660 else if (uid_lt(tlink->tl_uid, uid))
4661 node = node->rb_right;
4662 else
4663 return tlink;
4664 }
4665 return NULL;
4666}
4667
4668/* insert a tcon_link into the tree */
4669static void
4670tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4671{
4672 struct rb_node **new = &(root->rb_node), *parent = NULL;
4673 struct tcon_link *tlink;
4674
4675 while (*new) {
4676 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4677 parent = *new;
4678
4679 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
4680 new = &((*new)->rb_left);
4681 else
4682 new = &((*new)->rb_right);
4683 }
4684
4685 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4686 rb_insert_color(&new_tlink->tl_rbnode, root);
4687}
4688
4689/*
4690 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4691 * current task.
4692 *
4693 * If the superblock doesn't refer to a multiuser mount, then just return
4694 * the master tcon for the mount.
4695 *
4696 * First, search the rbtree for an existing tcon for this fsuid. If one
4697 * exists, then check to see if it's pending construction. If it is then wait
4698 * for construction to complete. Once it's no longer pending, check to see if
4699 * it failed and either return an error or retry construction, depending on
4700 * the timeout.
4701 *
4702 * If one doesn't exist then insert a new tcon_link struct into the tree and
4703 * try to construct a new one.
4704 */
4705struct tcon_link *
4706cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4707{
4708 int ret;
4709 kuid_t fsuid = current_fsuid();
4710 struct tcon_link *tlink, *newtlink;
4711
4712 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4713 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4714
4715 spin_lock(&cifs_sb->tlink_tree_lock);
4716 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
4717 if (tlink)
4718 cifs_get_tlink(tlink);
4719 spin_unlock(&cifs_sb->tlink_tree_lock);
4720
4721 if (tlink == NULL) {
4722 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4723 if (newtlink == NULL)
4724 return ERR_PTR(-ENOMEM);
4725 newtlink->tl_uid = fsuid;
4726 newtlink->tl_tcon = ERR_PTR(-EACCES);
4727 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4728 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4729 cifs_get_tlink(newtlink);
4730
4731 spin_lock(&cifs_sb->tlink_tree_lock);
4732 /* was one inserted after previous search? */
4733 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
4734 if (tlink) {
4735 cifs_get_tlink(tlink);
4736 spin_unlock(&cifs_sb->tlink_tree_lock);
4737 kfree(newtlink);
4738 goto wait_for_construction;
4739 }
4740 tlink = newtlink;
4741 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4742 spin_unlock(&cifs_sb->tlink_tree_lock);
4743 } else {
4744wait_for_construction:
4745 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
4746 TASK_INTERRUPTIBLE);
4747 if (ret) {
4748 cifs_put_tlink(tlink);
4749 return ERR_PTR(-ERESTARTSYS);
4750 }
4751
4752 /* if it's good, return it */
4753 if (!IS_ERR(tlink->tl_tcon))
4754 return tlink;
4755
4756 /* return error if we tried this already recently */
4757 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4758 cifs_put_tlink(tlink);
4759 return ERR_PTR(-EACCES);
4760 }
4761
4762 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4763 goto wait_for_construction;
4764 }
4765
4766 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4767 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4768 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4769
4770 if (IS_ERR(tlink->tl_tcon)) {
4771 cifs_put_tlink(tlink);
4772 return ERR_PTR(-EACCES);
4773 }
4774
4775 return tlink;
4776}
4777
4778/*
4779 * periodic workqueue job that scans tcon_tree for a superblock and closes
4780 * out tcons.
4781 */
4782static void
4783cifs_prune_tlinks(struct work_struct *work)
4784{
4785 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4786 prune_tlinks.work);
4787 struct rb_root *root = &cifs_sb->tlink_tree;
4788 struct rb_node *node;
4789 struct rb_node *tmp;
4790 struct tcon_link *tlink;
4791
4792 /*
4793 * Because we drop the spinlock in the loop in order to put the tlink
4794 * it's not guarded against removal of links from the tree. The only
4795 * places that remove entries from the tree are this function and
4796 * umounts. Because this function is non-reentrant and is canceled
4797 * before umount can proceed, this is safe.
4798 */
4799 spin_lock(&cifs_sb->tlink_tree_lock);
4800 node = rb_first(root);
4801 while (node != NULL) {
4802 tmp = node;
4803 node = rb_next(tmp);
4804 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4805
4806 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4807 atomic_read(&tlink->tl_count) != 0 ||
4808 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4809 continue;
4810
4811 cifs_get_tlink(tlink);
4812 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4813 rb_erase(tmp, root);
4814
4815 spin_unlock(&cifs_sb->tlink_tree_lock);
4816 cifs_put_tlink(tlink);
4817 spin_lock(&cifs_sb->tlink_tree_lock);
4818 }
4819 spin_unlock(&cifs_sb->tlink_tree_lock);
4820
4821 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
4822 TLINK_IDLE_EXPIRE);
4823}