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