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