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