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