connect.c 112 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/*
 *   fs/cifs/connect.c
 *
4
 *   Copyright (C) International Business Machines  Corp., 2002,2011
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *   Author(s): Steve French (sfrench@us.ibm.com)
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this library; if not, write to the Free Software
Steve French's avatar
Steve French committed
19
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds's avatar
Linus Torvalds committed
20 21 22 23 24 25
 */
#include <linux/fs.h>
#include <linux/net.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/wait.h>
26
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
27 28 29 30
#include <linux/pagemap.h>
#include <linux/ctype.h>
#include <linux/utsname.h>
#include <linux/mempool.h>
31
#include <linux/delay.h>
32
#include <linux/completion.h>
33
#include <linux/kthread.h>
34
#include <linux/pagevec.h>
35
#include <linux/freezer.h>
36
#include <linux/namei.h>
Linus Torvalds's avatar
Linus Torvalds committed
37 38
#include <asm/uaccess.h>
#include <asm/processor.h>
39
#include <linux/inet.h>
40
#include <linux/module.h>
41
#include <keys/user-type.h>
42
#include <net/ipv6.h>
43 44
#include <linux/parser.h>

Linus Torvalds's avatar
Linus Torvalds committed
45 46 47 48 49 50 51 52 53
#include "cifspdu.h"
#include "cifsglob.h"
#include "cifsproto.h"
#include "cifs_unicode.h"
#include "cifs_debug.h"
#include "cifs_fs_sb.h"
#include "ntlmssp.h"
#include "nterr.h"
#include "rfc1002pdu.h"
54
#include "fscache.h"
55 56 57
#ifdef CONFIG_CIFS_SMB2
#include "smb2proto.h"
#endif
Linus Torvalds's avatar
Linus Torvalds committed
58 59 60 61 62 63

#define CIFS_PORT 445
#define RFC1001_PORT 139

extern mempool_t *cifs_req_poolp;

64
/* FIXME: should these be tunable? */
65
#define TLINK_ERROR_EXPIRE	(1 * HZ)
66
#define TLINK_IDLE_EXPIRE	(600 * HZ)
67

68 69 70 71
enum {
	/* Mount options that take no arguments */
	Opt_user_xattr, Opt_nouser_xattr,
	Opt_forceuid, Opt_noforceuid,
72
	Opt_forcegid, Opt_noforcegid,
73 74
	Opt_noblocksend, Opt_noautotune,
	Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
75
	Opt_mapposix, Opt_nomapposix,
76 77 78 79 80
	Opt_mapchars, Opt_nomapchars, Opt_sfu,
	Opt_nosfu, Opt_nodfs, Opt_posixpaths,
	Opt_noposixpaths, Opt_nounix,
	Opt_nocase,
	Opt_brl, Opt_nobrl,
81
	Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
82 83 84 85 86 87 88
	Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
	Opt_nohard, Opt_nosoft,
	Opt_nointr, Opt_intr,
	Opt_nostrictsync, Opt_strictsync,
	Opt_serverino, Opt_noserverino,
	Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
	Opt_acl, Opt_noacl, Opt_locallease,
89
	Opt_sign, Opt_seal, Opt_noac,
90
	Opt_fsc, Opt_mfsymlinks,
91
	Opt_multiuser, Opt_sloppy, Opt_nosharesock,
92
	Opt_persistent, Opt_nopersistent,
Steve French's avatar
Steve French committed
93
	Opt_resilient, Opt_noresilient,
94 95 96 97 98 99

	/* Mount options which take numeric value */
	Opt_backupuid, Opt_backupgid, Opt_uid,
	Opt_cruid, Opt_gid, Opt_file_mode,
	Opt_dirmode, Opt_port,
	Opt_rsize, Opt_wsize, Opt_actimeo,
100
	Opt_echo_interval, Opt_max_credits,
101 102 103

	/* Mount options which take string value */
	Opt_user, Opt_pass, Opt_ip,
104
	Opt_domain, Opt_srcaddr, Opt_iocharset,
105
	Opt_netbiosname, Opt_servern,
106
	Opt_ver, Opt_vers, Opt_sec, Opt_cache,
107 108 109 110 111 112

	/* Mount options to be ignored */
	Opt_ignore,

	/* Options which could be blank */
	Opt_blank_pass,
113 114
	Opt_blank_user,
	Opt_blank_ip,
115 116 117 118 119 120 121 122 123 124

	Opt_err
};

static const match_table_t cifs_mount_option_tokens = {

	{ Opt_user_xattr, "user_xattr" },
	{ Opt_nouser_xattr, "nouser_xattr" },
	{ Opt_forceuid, "forceuid" },
	{ Opt_noforceuid, "noforceuid" },
125 126
	{ Opt_forcegid, "forcegid" },
	{ Opt_noforcegid, "noforcegid" },
127 128 129 130 131 132
	{ Opt_noblocksend, "noblocksend" },
	{ Opt_noautotune, "noautotune" },
	{ Opt_hard, "hard" },
	{ Opt_soft, "soft" },
	{ Opt_perm, "perm" },
	{ Opt_noperm, "noperm" },
133
	{ Opt_mapchars, "mapchars" }, /* SFU style */
134
	{ Opt_nomapchars, "nomapchars" },
135 136
	{ Opt_mapposix, "mapposix" }, /* SFM style */
	{ Opt_nomapposix, "nomapposix" },
137 138 139 140 141 142 143 144 145 146 147 148 149
	{ Opt_sfu, "sfu" },
	{ Opt_nosfu, "nosfu" },
	{ Opt_nodfs, "nodfs" },
	{ Opt_posixpaths, "posixpaths" },
	{ Opt_noposixpaths, "noposixpaths" },
	{ Opt_nounix, "nounix" },
	{ Opt_nounix, "nolinux" },
	{ Opt_nocase, "nocase" },
	{ Opt_nocase, "ignorecase" },
	{ Opt_brl, "brl" },
	{ Opt_nobrl, "nobrl" },
	{ Opt_nobrl, "nolock" },
	{ Opt_forcemandatorylock, "forcemandatorylock" },
150
	{ Opt_forcemandatorylock, "forcemand" },
151 152
	{ Opt_setuids, "setuids" },
	{ Opt_nosetuids, "nosetuids" },
153
	{ Opt_setuidfromacl, "idsfromsid" },
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	{ Opt_dynperm, "dynperm" },
	{ Opt_nodynperm, "nodynperm" },
	{ Opt_nohard, "nohard" },
	{ Opt_nosoft, "nosoft" },
	{ Opt_nointr, "nointr" },
	{ Opt_intr, "intr" },
	{ Opt_nostrictsync, "nostrictsync" },
	{ Opt_strictsync, "strictsync" },
	{ Opt_serverino, "serverino" },
	{ Opt_noserverino, "noserverino" },
	{ Opt_rwpidforward, "rwpidforward" },
	{ Opt_cifsacl, "cifsacl" },
	{ Opt_nocifsacl, "nocifsacl" },
	{ Opt_acl, "acl" },
	{ Opt_noacl, "noacl" },
	{ Opt_locallease, "locallease" },
	{ Opt_sign, "sign" },
	{ Opt_seal, "seal" },
	{ Opt_noac, "noac" },
	{ Opt_fsc, "fsc" },
	{ Opt_mfsymlinks, "mfsymlinks" },
	{ Opt_multiuser, "multiuser" },
176
	{ Opt_sloppy, "sloppy" },
177
	{ Opt_nosharesock, "nosharesock" },
178 179
	{ Opt_persistent, "persistenthandles"},
	{ Opt_nopersistent, "nopersistenthandles"},
Steve French's avatar
Steve French committed
180 181
	{ Opt_resilient, "resilienthandles"},
	{ Opt_noresilient, "noresilienthandles"},
182 183 184 185 186 187 188 189 190 191 192 193 194

	{ Opt_backupuid, "backupuid=%s" },
	{ Opt_backupgid, "backupgid=%s" },
	{ Opt_uid, "uid=%s" },
	{ Opt_cruid, "cruid=%s" },
	{ Opt_gid, "gid=%s" },
	{ Opt_file_mode, "file_mode=%s" },
	{ Opt_dirmode, "dirmode=%s" },
	{ Opt_dirmode, "dir_mode=%s" },
	{ Opt_port, "port=%s" },
	{ Opt_rsize, "rsize=%s" },
	{ Opt_wsize, "wsize=%s" },
	{ Opt_actimeo, "actimeo=%s" },
195
	{ Opt_echo_interval, "echo_interval=%s" },
196
	{ Opt_max_credits, "max_credits=%s" },
197

198 199
	{ Opt_blank_user, "user=" },
	{ Opt_blank_user, "username=" },
200 201 202
	{ Opt_user, "user=%s" },
	{ Opt_user, "username=%s" },
	{ Opt_blank_pass, "pass=" },
203
	{ Opt_blank_pass, "password=" },
204 205
	{ Opt_pass, "pass=%s" },
	{ Opt_pass, "password=%s" },
206 207
	{ Opt_blank_ip, "ip=" },
	{ Opt_blank_ip, "addr=" },
208 209
	{ Opt_ip, "ip=%s" },
	{ Opt_ip, "addr=%s" },
210 211 212
	{ Opt_ignore, "unc=%s" },
	{ Opt_ignore, "target=%s" },
	{ Opt_ignore, "path=%s" },
213 214 215 216
	{ Opt_domain, "dom=%s" },
	{ Opt_domain, "domain=%s" },
	{ Opt_domain, "workgroup=%s" },
	{ Opt_srcaddr, "srcaddr=%s" },
217
	{ Opt_ignore, "prefixpath=%s" },
218 219 220 221
	{ Opt_iocharset, "iocharset=%s" },
	{ Opt_netbiosname, "netbiosname=%s" },
	{ Opt_servern, "servern=%s" },
	{ Opt_ver, "ver=%s" },
222
	{ Opt_vers, "vers=%s" },
223
	{ Opt_sec, "sec=%s" },
224
	{ Opt_cache, "cache=%s" },
225 226 227

	{ Opt_ignore, "cred" },
	{ Opt_ignore, "credentials" },
228 229
	{ Opt_ignore, "cred=%s" },
	{ Opt_ignore, "credentials=%s" },
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	{ Opt_ignore, "guest" },
	{ Opt_ignore, "rw" },
	{ Opt_ignore, "ro" },
	{ Opt_ignore, "suid" },
	{ Opt_ignore, "nosuid" },
	{ Opt_ignore, "exec" },
	{ Opt_ignore, "noexec" },
	{ Opt_ignore, "nodev" },
	{ Opt_ignore, "noauto" },
	{ Opt_ignore, "dev" },
	{ Opt_ignore, "mand" },
	{ Opt_ignore, "nomand" },
	{ Opt_ignore, "_netdev" },

	{ Opt_err, NULL }
};

enum {
	Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
	Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
250 251
	Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
	Opt_sec_ntlmv2i, Opt_sec_lanman,
252 253 254 255 256 257 258 259 260 261 262 263 264
	Opt_sec_none,

	Opt_sec_err
};

static const match_table_t cifs_secflavor_tokens = {
	{ Opt_sec_krb5, "krb5" },
	{ Opt_sec_krb5i, "krb5i" },
	{ Opt_sec_krb5p, "krb5p" },
	{ Opt_sec_ntlmsspi, "ntlmsspi" },
	{ Opt_sec_ntlmssp, "ntlmssp" },
	{ Opt_ntlm, "ntlm" },
	{ Opt_sec_ntlmi, "ntlmi" },
265 266
	{ Opt_sec_ntlmv2, "nontlm" },
	{ Opt_sec_ntlmv2, "ntlmv2" },
267 268 269 270 271 272 273
	{ Opt_sec_ntlmv2i, "ntlmv2i" },
	{ Opt_sec_lanman, "lanman" },
	{ Opt_sec_none, "none" },

	{ Opt_sec_err, NULL }
};

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
/* cache flavors */
enum {
	Opt_cache_loose,
	Opt_cache_strict,
	Opt_cache_none,
	Opt_cache_err
};

static const match_table_t cifs_cacheflavor_tokens = {
	{ Opt_cache_loose, "loose" },
	{ Opt_cache_strict, "strict" },
	{ Opt_cache_none, "none" },
	{ Opt_cache_err, NULL }
};

289 290
static const match_table_t cifs_smb_version_tokens = {
	{ Smb_1, SMB1_VERSION_STRING },
Steve French's avatar
Steve French committed
291
	{ Smb_20, SMB20_VERSION_STRING},
292
	{ Smb_21, SMB21_VERSION_STRING },
293
	{ Smb_30, SMB30_VERSION_STRING },
Steve French's avatar
Steve French committed
294
	{ Smb_302, SMB302_VERSION_STRING },
295 296
#ifdef CONFIG_CIFS_SMB311
	{ Smb_311, SMB311_VERSION_STRING },
297
	{ Smb_311, ALT_SMB311_VERSION_STRING },
298 299
#endif /* SMB311 */
	{ Smb_version_err, NULL }
300 301
};

302 303
static int ip_connect(struct TCP_Server_Info *server);
static int generic_ip_connect(struct TCP_Server_Info *server);
304
static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
305
static void cifs_prune_tlinks(struct work_struct *work);
Jeff Layton's avatar
Jeff Layton committed
306 307
static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
					const char *devname);
Linus Torvalds's avatar
Linus Torvalds committed
308

309 310 311 312 313 314 315 316
/*
 * cifs tcp session reconnection
 *
 * mark tcp session as reconnecting so temporarily locked
 * mark all smb sessions as reconnecting for tcp session
 * reconnect tcp session
 * wake up waiters on reconnection? - (not needed currently)
 */
317
int
Linus Torvalds's avatar
Linus Torvalds committed
318 319 320
cifs_reconnect(struct TCP_Server_Info *server)
{
	int rc = 0;
321
	struct list_head *tmp, *tmp2;
322 323
	struct cifs_ses *ses;
	struct cifs_tcon *tcon;
Steve French's avatar
Steve French committed
324
	struct mid_q_entry *mid_entry;
325
	struct list_head retry_list;
326

Linus Torvalds's avatar
Linus Torvalds committed
327
	spin_lock(&GlobalMid_Lock);
328
	if (server->tcpStatus == CifsExiting) {
Steve French's avatar
Steve French committed
329
		/* the demux thread will exit normally
Linus Torvalds's avatar
Linus Torvalds committed
330 331 332 333 334 335 336
		next time through the loop */
		spin_unlock(&GlobalMid_Lock);
		return rc;
	} else
		server->tcpStatus = CifsNeedReconnect;
	spin_unlock(&GlobalMid_Lock);
	server->maxBuf = 0;
337 338 339
#ifdef CONFIG_CIFS_SMB2
	server->max_read = 0;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
340

341
	cifs_dbg(FYI, "Reconnecting tcp session\n");
Linus Torvalds's avatar
Linus Torvalds committed
342 343 344

	/* before reconnecting the tcp session, mark the smb session (uid)
		and the tid bad so they are not used until reconnected */
345 346
	cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
		 __func__);
347
	spin_lock(&cifs_tcp_ses_lock);
348
	list_for_each(tmp, &server->smb_ses_list) {
349
		ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
350 351
		ses->need_reconnect = true;
		ses->ipc_tid = 0;
352
		list_for_each(tmp2, &ses->tcon_list) {
353
			tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
354
			tcon->need_reconnect = true;
Linus Torvalds's avatar
Linus Torvalds committed
355 356
		}
	}
357
	spin_unlock(&cifs_tcp_ses_lock);
358

Linus Torvalds's avatar
Linus Torvalds committed
359
	/* do not want to be sending data on a socket we are freeing */
360
	cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton's avatar
Jeff Layton committed
361
	mutex_lock(&server->srv_mutex);
Steve French's avatar
Steve French committed
362
	if (server->ssocket) {
363 364
		cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
			 server->ssocket->state, server->ssocket->flags);
365
		kernel_sock_shutdown(server->ssocket, SHUT_WR);
366 367
		cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
			 server->ssocket->state, server->ssocket->flags);
Linus Torvalds's avatar
Linus Torvalds committed
368 369 370
		sock_release(server->ssocket);
		server->ssocket = NULL;
	}
371 372
	server->sequence_number = 0;
	server->session_estab = false;
373 374 375
	kfree(server->session_key.response);
	server->session_key.response = NULL;
	server->session_key.len = 0;
376
	server->lstrp = jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
377

378
	/* mark submitted MIDs for retry and issue callback */
379
	INIT_LIST_HEAD(&retry_list);
380
	cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
381
	spin_lock(&GlobalMid_Lock);
382 383
	list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
		mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
384 385
		if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
			mid_entry->mid_state = MID_RETRY_NEEDED;
386 387 388
		list_move(&mid_entry->qhead, &retry_list);
	}
	spin_unlock(&GlobalMid_Lock);
389
	mutex_unlock(&server->srv_mutex);
390

391
	cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
392 393
	list_for_each_safe(tmp, tmp2, &retry_list) {
		mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
394 395
		list_del_init(&mid_entry->qhead);
		mid_entry->callback(mid_entry);
Linus Torvalds's avatar
Linus Torvalds committed
396 397
	}

398
	do {
399
		try_to_freeze();
400 401

		/* we should try only the port we connected to before */
402
		mutex_lock(&server->srv_mutex);
403
		rc = generic_ip_connect(server);
Steve French's avatar
Steve French committed
404
		if (rc) {
405
			cifs_dbg(FYI, "reconnect error %d\n", rc);
406
			mutex_unlock(&server->srv_mutex);
407
			msleep(3000);
Linus Torvalds's avatar
Linus Torvalds committed
408 409 410
		} else {
			atomic_inc(&tcpSesReconnectCount);
			spin_lock(&GlobalMid_Lock);
411
			if (server->tcpStatus != CifsExiting)
412
				server->tcpStatus = CifsNeedNegotiate;
Steve French's avatar
Steve French committed
413
			spin_unlock(&GlobalMid_Lock);
414
			mutex_unlock(&server->srv_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
415
		}
416
	} while (server->tcpStatus == CifsNeedReconnect);
417

418 419 420
	if (server->tcpStatus == CifsNeedNegotiate)
		mod_delayed_work(cifsiod_wq, &server->echo, 0);

Linus Torvalds's avatar
Linus Torvalds committed
421 422 423
	return rc;
}

424 425 426 427 428 429
static void
cifs_echo_request(struct work_struct *work)
{
	int rc;
	struct TCP_Server_Info *server = container_of(work,
					struct TCP_Server_Info, echo.work);
430 431 432 433 434 435 436 437 438 439
	unsigned long echo_interval;

	/*
	 * If we need to renegotiate, set echo interval to zero to
	 * immediately call echo service where we can renegotiate.
	 */
	if (server->tcpStatus == CifsNeedNegotiate)
		echo_interval = 0;
	else
		echo_interval = server->echo_interval;
440

441
	/*
442 443
	 * We cannot send an echo if it is disabled.
	 * Also, no need to ping if we got a response recently.
444
	 */
445 446

	if (server->tcpStatus == CifsNeedReconnect ||
447 448
	    server->tcpStatus == CifsExiting ||
	    server->tcpStatus == CifsNew ||
449
	    (server->ops->can_echo && !server->ops->can_echo(server)) ||
450
	    time_before(jiffies, server->lstrp + echo_interval - HZ))
451 452
		goto requeue_echo;

453
	rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
454
	if (rc)
455 456
		cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
			 server->hostname);
457 458

requeue_echo:
459
	queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
460 461
}

462
static bool
463
allocate_buffers(struct TCP_Server_Info *server)
464
{
465 466 467
	if (!server->bigbuf) {
		server->bigbuf = (char *)cifs_buf_get();
		if (!server->bigbuf) {
468
			cifs_dbg(VFS, "No memory for large SMB response\n");
469 470 471 472
			msleep(3000);
			/* retry will check if exiting */
			return false;
		}
473
	} else if (server->large_buf) {
474
		/* we are reusing a dirty large buf, clear its start */
475
		memset(server->bigbuf, 0, HEADER_SIZE(server));
476 477
	}

478 479 480
	if (!server->smallbuf) {
		server->smallbuf = (char *)cifs_small_buf_get();
		if (!server->smallbuf) {
481
			cifs_dbg(VFS, "No memory for SMB response\n");
482 483 484 485 486 487 488
			msleep(1000);
			/* retry will check if exiting */
			return false;
		}
		/* beginning of smb buffer is cleared in our buf_get */
	} else {
		/* if existing small buf clear beginning */
489
		memset(server->smallbuf, 0, HEADER_SIZE(server));
490 491 492 493 494
	}

	return true;
}

495 496 497
static bool
server_unresponsive(struct TCP_Server_Info *server)
{
498 499 500 501 502 503 504 505 506 507 508 509
	/*
	 * We need to wait 2 echo intervals to make sure we handle such
	 * situations right:
	 * 1s  client sends a normal SMB request
	 * 2s  client gets a response
	 * 30s echo workqueue job pops, and decides we got a response recently
	 *     and don't need to send another
	 * ...
	 * 65s kernel_recvmsg times out, and we see that we haven't gotten
	 *     a response in >60s.
	 */
	if (server->tcpStatus == CifsGood &&
510 511 512
	    time_after(jiffies, server->lstrp + 2 * server->echo_interval)) {
		cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
			 server->hostname, (2 * server->echo_interval) / HZ);
513 514 515 516 517 518 519 520
		cifs_reconnect(server);
		wake_up(&server->response_q);
		return true;
	}

	return false;
}

521 522
static int
cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
523
{
Jeff Layton's avatar
Jeff Layton committed
524 525
	int length = 0;
	int total_read;
526

527 528
	smb_msg->msg_control = NULL;
	smb_msg->msg_controllen = 0;
529

530
	for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
531 532
		try_to_freeze();

533 534
		if (server_unresponsive(server))
			return -ECONNABORTED;
535

536
		length = sock_recvmsg(server->ssocket, smb_msg, 0);
537

538 539
		if (server->tcpStatus == CifsExiting)
			return -ESHUTDOWN;
540

541
		if (server->tcpStatus == CifsNeedReconnect) {
542
			cifs_reconnect(server);
543 544 545 546 547 548
			return -ECONNABORTED;
		}

		if (length == -ERESTARTSYS ||
		    length == -EAGAIN ||
		    length == -EINTR) {
549 550 551 552 553 554 555
			/*
			 * Minimum sleep to prevent looping, allowing socket
			 * to clear and app threads to set tcpStatus
			 * CifsNeedReconnect if server hung.
			 */
			usleep_range(1000, 2000);
			length = 0;
Jeff Layton's avatar
Jeff Layton committed
556
			continue;
557 558 559
		}

		if (length <= 0) {
560
			cifs_dbg(FYI, "Received no data or error: %d\n", length);
561
			cifs_reconnect(server);
562
			return -ECONNABORTED;
563 564
		}
	}
Jeff Layton's avatar
Jeff Layton committed
565
	return total_read;
566 567
}

Jeff Layton's avatar
Jeff Layton committed
568 569 570
int
cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
		      unsigned int to_read)
571
{
572 573 574
	struct msghdr smb_msg;
	struct kvec iov = {.iov_base = buf, .iov_len = to_read};
	iov_iter_kvec(&smb_msg.msg_iter, READ | ITER_KVEC, &iov, 1, to_read);
575

576 577
	return cifs_readv_from_socket(server, &smb_msg);
}
578

579 580 581 582 583 584 585 586
int
cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
		      unsigned int to_read)
{
	struct msghdr smb_msg;
	struct bio_vec bv = {.bv_page = page, .bv_len = to_read};
	iov_iter_bvec(&smb_msg.msg_iter, READ | ITER_BVEC, &bv, 1, to_read);
	return cifs_readv_from_socket(server, &smb_msg);
587 588
}

589
static bool
590
is_smb_response(struct TCP_Server_Info *server, unsigned char type)
591 592 593 594 595 596
{
	/*
	 * The first byte big endian of the length field,
	 * is actually not part of the length but the type
	 * with the most common, zero, as regular data.
	 */
597 598 599 600 601
	switch (type) {
	case RFC1002_SESSION_MESSAGE:
		/* Regular SMB response */
		return true;
	case RFC1002_SESSION_KEEP_ALIVE:
602
		cifs_dbg(FYI, "RFC 1002 session keep alive\n");
603 604
		break;
	case RFC1002_POSITIVE_SESSION_RESPONSE:
605
		cifs_dbg(FYI, "RFC 1002 positive session response\n");
606 607
		break;
	case RFC1002_NEGATIVE_SESSION_RESPONSE:
608 609 610 611
		/*
		 * We get this from Windows 98 instead of an error on
		 * SMB negprot response.
		 */
612
		cifs_dbg(FYI, "RFC 1002 negative session response\n");
613 614 615 616 617 618 619 620
		/* give server a second to clean up */
		msleep(1000);
		/*
		 * Always try 445 first on reconnect since we get NACK
		 * on some if we ever connected to port 139 (the NACK
		 * is since we do not begin with RFC1001 session
		 * initialize frame).
		 */
621
		cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
622 623
		cifs_reconnect(server);
		wake_up(&server->response_q);
624 625
		break;
	default:
626
		cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
627 628 629
		cifs_reconnect(server);
	}

630
	return false;
631 632
}

Jeff Layton's avatar
Jeff Layton committed
633 634
void
dequeue_mid(struct mid_q_entry *mid, bool malformed)
635
{
636
#ifdef CONFIG_CIFS_STATS2
637
	mid->when_received = jiffies;
638
#endif
639 640
	spin_lock(&GlobalMid_Lock);
	if (!malformed)
641
		mid->mid_state = MID_RESPONSE_RECEIVED;
642
	else
643
		mid->mid_state = MID_RESPONSE_MALFORMED;
644
	list_del_init(&mid->qhead);
645
	spin_unlock(&GlobalMid_Lock);
646
}
647

648 649
static void
handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
650
	   char *buf, int malformed)
651
{
652 653
	if (server->ops->check_trans2 &&
	    server->ops->check_trans2(mid, server, buf, malformed))
654
		return;
655
	mid->resp_buf = buf;
656
	mid->large_buf = server->large_buf;
657 658 659 660 661 662 663 664
	/* Was previous buf put in mpx struct for multi-rsp? */
	if (!mid->multiRsp) {
		/* smb buffer will be freed by user thread */
		if (server->large_buf)
			server->bigbuf = NULL;
		else
			server->smallbuf = NULL;
	}
665
	dequeue_mid(mid, malformed);
666 667
}

668 669 670 671 672 673 674 675 676 677 678 679 680 681
static void clean_demultiplex_info(struct TCP_Server_Info *server)
{
	int length;

	/* take it off the list, if it's not already */
	spin_lock(&cifs_tcp_ses_lock);
	list_del_init(&server->tcp_ses_list);
	spin_unlock(&cifs_tcp_ses_lock);

	spin_lock(&GlobalMid_Lock);
	server->tcpStatus = CifsExiting;
	spin_unlock(&GlobalMid_Lock);
	wake_up_all(&server->response_q);

682
	/* check if we have blocked requests that need to free */
683
	spin_lock(&server->req_lock);
684 685
	if (server->credits <= 0)
		server->credits = 1;
686
	spin_unlock(&server->req_lock);
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	/*
	 * Although there should not be any requests blocked on this queue it
	 * can not hurt to be paranoid and try to wake up requests that may
	 * haven been blocked when more than 50 at time were on the wire to the
	 * same server - they now will see the session is in exit state and get
	 * out of SendReceive.
	 */
	wake_up_all(&server->request_q);
	/* give those requests time to exit */
	msleep(125);

	if (server->ssocket) {
		sock_release(server->ssocket);
		server->ssocket = NULL;
	}

	if (!list_empty(&server->pending_mid_q)) {
		struct list_head dispose_list;
		struct mid_q_entry *mid_entry;
		struct list_head *tmp, *tmp2;

		INIT_LIST_HEAD(&dispose_list);
		spin_lock(&GlobalMid_Lock);
		list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
			mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
712
			cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
713
			mid_entry->mid_state = MID_SHUTDOWN;
714 715 716 717 718 719 720
			list_move(&mid_entry->qhead, &dispose_list);
		}
		spin_unlock(&GlobalMid_Lock);

		/* now walk dispose list and issue callbacks */
		list_for_each_safe(tmp, tmp2, &dispose_list) {
			mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
721
			cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
			list_del_init(&mid_entry->qhead);
			mid_entry->callback(mid_entry);
		}
		/* 1/8th of sec is more than enough time for them to exit */
		msleep(125);
	}

	if (!list_empty(&server->pending_mid_q)) {
		/*
		 * mpx threads have not exited yet give them at least the smb
		 * send timeout time for long ops.
		 *
		 * Due to delays on oplock break requests, we need to wait at
		 * least 45 seconds before giving up on a request getting a
		 * response and going ahead and killing cifsd.
		 */
738
		cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
739 740 741 742 743 744 745 746 747 748 749 750
		msleep(46000);
		/*
		 * If threads still have not exited they are probably never
		 * coming home not much else we can do but free the memory.
		 */
	}

	kfree(server->hostname);
	kfree(server);

	length = atomic_dec_return(&tcpSesAllocCount);
	if (length > 0)
751
		mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
752 753
}

754 755 756 757 758
static int
standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
{
	int length;
	char *buf = server->smallbuf;
759
	unsigned int pdu_length = get_rfc1002_length(buf);
760 761

	/* make sure this will fit in a large buffer */
762
	if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) - 4) {
763
		cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
764 765
		cifs_reconnect(server);
		wake_up(&server->response_q);
766
		return -ECONNABORTED;
767 768 769 770 771
	}

	/* switch to large buffer if too big for a small one */
	if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
		server->large_buf = true;
772
		memcpy(server->bigbuf, buf, server->total_read);
773 774 775 776
		buf = server->bigbuf;
	}

	/* now read the rest */
777 778
	length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
				pdu_length - HEADER_SIZE(server) + 1 + 4);
779 780 781 782
	if (length < 0)
		return length;
	server->total_read += length;

783
	dump_smb(buf, server->total_read);
784 785 786 787 788 789 790 791 792 793

	/*
	 * We know that we received enough to get to the MID as we
	 * checked the pdu_length earlier. Now check to see
	 * if the rest of the header is OK. We borrow the length
	 * var for the rest of the loop to avoid a new stack var.
	 *
	 * 48 bytes is enough to display the header and a little bit
	 * into the payload for debugging purposes.
	 */
794
	length = server->ops->check_message(buf, server->total_read, server);
795 796 797 798
	if (length != 0)
		cifs_dump_mem("Bad SMB: ", buf,
			min_t(unsigned int, server->total_read, 48));

799 800 801 802 803 804 805
	if (server->ops->is_session_expired &&
	    server->ops->is_session_expired(buf)) {
		cifs_reconnect(server);
		wake_up(&server->response_q);
		return -1;
	}

806 807 808 809
	if (server->ops->is_status_pending &&
	    server->ops->is_status_pending(buf, server, length))
		return -1;

810 811
	if (!mid)
		return length;
812

813
	handle_mid(mid, server, buf, length);
814
	return 0;
815 816
}

Linus Torvalds's avatar
Linus Torvalds committed
817
static int
818
cifs_demultiplex_thread(void *p)
Linus Torvalds's avatar
Linus Torvalds committed
819 820
{
	int length;
821
	struct TCP_Server_Info *server = p;
822 823
	unsigned int pdu_length;
	char *buf = NULL;
824
	struct task_struct *task_to_wake = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
825 826 827
	struct mid_q_entry *mid_entry;

	current->flags |= PF_MEMALLOC;
828
	cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
829 830 831

	length = atomic_inc_return(&tcpSesAllocCount);
	if (length > 1)
832
		mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds's avatar
Linus Torvalds committed
833

834
	set_freezable();
835
	while (server->tcpStatus != CifsExiting) {
836 837
		if (try_to_freeze())
			continue;
838

839
		if (!allocate_buffers(server))
840
			continue;
841

842 843
		server->large_buf = false;
		buf = server->smallbuf;
844
		pdu_length = 4; /* enough to get RFC1001 header */
845

Jeff Layton's avatar
Jeff Layton committed
846
		length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Layton's avatar
Jeff Layton committed
847
		if (length < 0)
Linus Torvalds's avatar
Linus Torvalds committed
848
			continue;
849
		server->total_read = length;
Linus Torvalds's avatar
Linus Torvalds committed
850

851 852 853 854
		/*
		 * The right amount was read from socket - 4 bytes,
		 * so we can now interpret the length field.
		 */
855
		pdu_length = get_rfc1002_length(buf);
856

857
		cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
858
		if (!is_smb_response(server, buf[0]))
Steve French's avatar
Steve French committed
859
			continue;
860

861
		/* make sure we have enough to get to the MID */
862
		if (pdu_length < HEADER_SIZE(server) - 1 - 4) {
863 864
			cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
				 pdu_length);
865 866 867
			cifs_reconnect(server);
			wake_up(&server->response_q);
			continue;
868
		}
869

870
		/* read down to the MID */
Jeff Layton's avatar
Jeff Layton committed
871
		length = cifs_read_from_socket(server, buf + 4,
872
					       HEADER_SIZE(server) - 1 - 4);
873
		if (length < 0)
874
			continue;
875
		server->total_read += length;
Linus Torvalds's avatar
Linus Torvalds committed
876

877
		mid_entry = server->ops->find_mid(server, buf);
878

879 880 881 882
		if (!mid_entry || !mid_entry->receive)
			length = standard_receive3(server, mid_entry);
		else
			length = mid_entry->receive(server, mid_entry);
883

884
		if (length < 0)
885
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
886

887
		if (server->large_buf)
888
			buf = server->bigbuf;
889 890

		server->lstrp = jiffies;
891
		if (mid_entry != NULL) {
Sachin Prabhu's avatar
Sachin Prabhu committed
892 893 894 895 896 897 898
			if ((mid_entry->mid_flags & MID_WAIT_CANCELLED) &&
			     mid_entry->mid_state == MID_RESPONSE_RECEIVED &&
					server->ops->handle_cancelled_mid)
				server->ops->handle_cancelled_mid(
							mid_entry->resp_buf,
							server);

899 900
			if (!mid_entry->multiRsp || mid_entry->multiEnd)
				mid_entry->callback(mid_entry);
Sachin Prabhu's avatar
Sachin Prabhu committed
901 902 903 904
		} else if (server->ops->is_oplock_break &&
			   server->ops->is_oplock_break(buf, server)) {
			cifs_dbg(FYI, "Received oplock break\n");
		} else {
905 906
			cifs_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
				 atomic_read(&midCount));
907 908
			cifs_dump_mem("Received Data is: ", buf,
				      HEADER_SIZE(server));
909
#ifdef CONFIG_CIFS_DEBUG2
910 911
			if (server->ops->dump_detail)
				server->ops->dump_detail(buf);
912 913
			cifs_dump_mids(server);
#endif /* CIFS_DEBUG2 */
914

915 916 917
		}
	} /* end while !EXITING */

918
	/* buffer usually freed in free_mid - need to free it here on exit */
919 920 921
	cifs_buf_release(server->bigbuf);
	if (server->smallbuf) /* no sense logging a debug message if NULL */
		cifs_small_buf_release(server->smallbuf);
Linus Torvalds's avatar
Linus Torvalds committed
922

923
	task_to_wake = xchg(&server->tsk, NULL);
924
	clean_demultiplex_info(server);
925 926 927 928 929 930 931 932 933 934 935

	/* if server->tsk was NULL then wait for a signal before exiting */
	if (!task_to_wake) {
		set_current_state(TASK_INTERRUPTIBLE);
		while (!signal_pending(current)) {
			schedule();
			set_current_state(TASK_INTERRUPTIBLE);
		}
		set_current_state(TASK_RUNNING);
	}

936
	module_put_and_exit(0);
Linus Torvalds's avatar
Linus Torvalds committed
937 938
}

939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
/* extract the host portion of the UNC string */
static char *
extract_hostname(const char *unc)
{
	const char *src;
	char *dst, *delim;
	unsigned int len;

	/* skip double chars at beginning of string */
	/* BB: check validity of these bytes? */
	src = unc + 2;

	/* delimiter between hostname and sharename is always '\\' now */
	delim = strchr(src, '\\');
	if (!delim)
		return ERR_PTR(-EINVAL);

	len = delim - src;
	dst = kmalloc((len + 1), GFP_KERNEL);
	if (dst == NULL)
		return ERR_PTR(-ENOMEM);

	memcpy(dst, src, len);
	dst[len] = '\0';

	return dst;
}

967 968 969 970 971 972 973 974
static int get_option_ul(substring_t args[], unsigned long *option)
{
	int rc;
	char *string;

	string = match_strdup(args);
	if (string == NULL)
		return -ENOMEM;
975
	rc = kstrtoul(string, 0, option);
976 977 978 979 980
	kfree(string);

	return rc;
}

981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
static int get_option_uid(substring_t args[], kuid_t *result)
{
	unsigned long value;
	kuid_t uid;
	int rc;

	rc = get_option_ul(args, &value);
	if (rc)
		return rc;

	uid = make_kuid(current_user_ns(), value);
	if (!uid_valid(uid))
		return -EINVAL;

	*result = uid;
	return 0;
}

static int get_option_gid(substring_t args[], kgid_t *result)
{
	unsigned long value;
	kgid_t gid;
	int rc;

	rc = get_option_ul(args, &value);
	if (rc)
		return rc;

	gid = make_kgid(current_user_ns(), value);
	if (!gid_valid(gid))
		return -EINVAL;

	*result = gid;
	return 0;
}
1016 1017 1018 1019 1020 1021 1022

static int cifs_parse_security_flavors(char *value,
				       struct smb_vol *vol)
{

	substring_t args[MAX_OPT_ARGS];

1023 1024 1025 1026 1027 1028 1029
	/*
	 * With mount options, the last one should win. Reset any existing
	 * settings back to default.
	 */
	vol->sectype = Unspecified;
	vol->sign = false;

1030
	switch (match_token(value, cifs_secflavor_tokens, args)) {
1031 1032 1033 1034 1035 1036
	case Opt_sec_krb5p:
		cifs_dbg(VFS, "sec=krb5p is not supported!\n");
		return 1;
	case Opt_sec_krb5i:
		vol->sign = true;
		/* Fallthrough */
1037
	case Opt_sec_krb5:
1038
		vol->sectype = Kerberos;
1039
		break;