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