1 // SPDX-License-Identifier: LGPL-2.1
5 * SMB/CIFS session setup handling routines
7 * Copyright (c) International Business Machines Corp., 2006, 2009
8 * Author(s): Steve French (sfrench@us.ibm.com)
14 #include "cifsproto.h"
15 #include "cifs_unicode.h"
16 #include "cifs_debug.h"
19 #include <linux/utsname.h>
20 #include <linux/slab.h>
21 #include "cifs_spnego.h"
22 #include "smb2proto.h"
23 #include "fs_context.h"
26 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
27 struct cifs_server_iface *iface);
30 is_server_using_iface(struct TCP_Server_Info *server,
31 struct cifs_server_iface *iface)
33 struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr;
34 struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr;
35 struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr;
36 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr;
38 if (server->dstaddr.ss_family != iface->sockaddr.ss_family)
40 if (server->dstaddr.ss_family == AF_INET) {
41 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
43 } else if (server->dstaddr.ss_family == AF_INET6) {
44 if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
45 sizeof(i6->sin6_addr)) != 0)
48 /* unknown family.. */
54 bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
58 for (i = 0; i < ses->chan_count; i++) {
59 if (is_server_using_iface(ses->chans[i].server, iface))
65 /* returns number of channels added */
66 int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
68 int old_chan_count = ses->chan_count;
69 int left = ses->chan_max - ses->chan_count;
73 struct cifs_server_iface *ifaces = NULL;
78 "ses already at max_channels (%zu), nothing to open\n",
83 if (ses->server->dialect < SMB30_PROT_ID) {
84 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
88 if (!(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
89 cifs_dbg(VFS, "server %s does not support multichannel\n", ses->server->hostname);
95 * Make a copy of the iface list at the time and use that
96 * instead so as to not hold the iface spinlock for opening
99 spin_lock(&ses->iface_lock);
100 iface_count = ses->iface_count;
101 if (iface_count <= 0) {
102 spin_unlock(&ses->iface_lock);
103 cifs_dbg(VFS, "no iface list available to open channels\n");
106 ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces),
109 spin_unlock(&ses->iface_lock);
112 spin_unlock(&ses->iface_lock);
115 * Keep connecting to same, fastest, iface for all channels as
116 * long as its RSS. Try next fastest one if not RSS or channel
120 struct cifs_server_iface *iface;
123 if (tries > 3*ses->chan_max) {
124 cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
130 if (is_ses_using_iface(ses, iface) && !iface->rss_capable) {
131 i = (i+1) % iface_count;
135 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
137 cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n",
139 i = (i+1) % iface_count;
143 cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
149 return ses->chan_count - old_chan_count;
153 * If server is a channel of ses, return the corresponding enclosing
154 * cifs_chan otherwise return NULL.
157 cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
161 for (i = 0; i < ses->chan_count; i++) {
162 if (ses->chans[i].server == server)
163 return &ses->chans[i];
169 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
170 struct cifs_server_iface *iface)
172 struct cifs_chan *chan;
173 struct smb3_fs_context ctx = {NULL};
174 static const char unc_fmt[] = "\\%s\\foo";
175 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
176 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
177 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
179 unsigned int xid = get_xid();
181 if (iface->sockaddr.ss_family == AF_INET)
182 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
183 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
186 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
187 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
191 * Setup a ctx with mostly the same info as the existing
192 * session and overwrite it with the requested iface data.
194 * We need to setup at least the fields used for negprot and
197 * We only need the ctx here, so we can reuse memory from
198 * the session and server without caring about memory
202 /* Always make new connection for now (TODO?) */
203 ctx.nosharesock = true;
206 ctx.domainauto = ses->domainAuto;
207 ctx.domainname = ses->domainName;
208 ctx.username = ses->user_name;
209 ctx.password = ses->password;
210 ctx.sectype = ses->sectype;
211 ctx.sign = ses->sign;
214 /* XXX: Use ses->server->hostname? */
215 sprintf(unc, unc_fmt, ses->ip_addr);
219 /* Reuse same version as master connection */
220 ctx.vals = ses->server->vals;
221 ctx.ops = ses->server->ops;
223 ctx.noblocksnd = ses->server->noblocksnd;
224 ctx.noautotune = ses->server->noautotune;
225 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
226 ctx.echo_interval = ses->server->echo_interval / HZ;
227 ctx.max_credits = ses->server->max_credits;
230 * This will be used for encoding/decoding user/domain/pw
231 * during sess setup auth.
233 ctx.local_nls = cifs_sb->local_nls;
235 /* Use RDMA if possible */
236 ctx.rdma = iface->rdma_capable;
237 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
239 /* reuse master con client guid */
240 memcpy(&ctx.client_guid, ses->server->client_guid,
241 SMB2_CLIENT_GUID_SIZE);
242 ctx.use_client_guid = true;
244 mutex_lock(&ses->session_mutex);
246 chan = ses->binding_chan = &ses->chans[ses->chan_count];
247 chan->server = cifs_get_tcp_session(&ctx);
248 if (IS_ERR(chan->server)) {
249 rc = PTR_ERR(chan->server);
253 spin_lock(&cifs_tcp_ses_lock);
254 chan->server->is_channel = true;
255 spin_unlock(&cifs_tcp_ses_lock);
258 * We need to allocate the server crypto now as we will need
259 * to sign packets before we generate the channel signing key
260 * (we sign with the session key)
262 rc = smb311_crypto_shash_allocate(chan->server);
264 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
269 rc = cifs_negotiate_protocol(xid, ses);
273 rc = cifs_setup_session(xid, ses, cifs_sb->local_nls);
277 /* success, put it on the list
278 * XXX: sharing ses between 2 tcp servers is not possible, the
279 * way "internal" linked lists works in linux makes element
280 * only able to belong to one list
282 * the binding session is already established so the rest of
283 * the code should be able to look it up, no need to add the
284 * ses to the new server.
288 atomic_set(&ses->chan_seq, 0);
290 ses->binding = false;
291 ses->binding_chan = NULL;
292 mutex_unlock(&ses->session_mutex);
294 if (rc && chan->server)
295 cifs_put_tcp_session(chan->server, 0);
300 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses, SESSION_SETUP_ANDX *pSMB)
302 __u32 capabilities = 0;
304 /* init fields common to all four types of SessSetup */
305 /* Note that offsets for first seven fields in req struct are same */
306 /* in CIFS Specs so does not matter which of 3 forms of struct */
307 /* that we use in next few lines */
308 /* Note that header is initialized to zero in header_assemble */
309 pSMB->req.AndXCommand = 0xFF;
310 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
311 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
313 pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
314 pSMB->req.VcNumber = cpu_to_le16(1);
316 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
318 /* BB verify whether signing required on neg or just on auth frame
321 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
322 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
324 if (ses->server->sign)
325 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
327 if (ses->capabilities & CAP_UNICODE) {
328 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
329 capabilities |= CAP_UNICODE;
331 if (ses->capabilities & CAP_STATUS32) {
332 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
333 capabilities |= CAP_STATUS32;
335 if (ses->capabilities & CAP_DFS) {
336 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
337 capabilities |= CAP_DFS;
339 if (ses->capabilities & CAP_UNIX)
340 capabilities |= CAP_UNIX;
346 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
348 char *bcc_ptr = *pbcc_area;
351 /* Copy OS version */
352 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
354 bcc_ptr += 2 * bytes_ret;
355 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
357 bcc_ptr += 2 * bytes_ret;
358 bcc_ptr += 2; /* trailing null */
360 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
362 bcc_ptr += 2 * bytes_ret;
363 bcc_ptr += 2; /* trailing null */
365 *pbcc_area = bcc_ptr;
368 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
369 const struct nls_table *nls_cp)
371 char *bcc_ptr = *pbcc_area;
375 if (ses->domainName == NULL) {
376 /* Sending null domain better than using a bogus domain name (as
377 we did briefly in 2.6.18) since server will use its default */
382 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
383 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
384 bcc_ptr += 2 * bytes_ret;
385 bcc_ptr += 2; /* account for null terminator */
387 *pbcc_area = bcc_ptr;
391 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
392 const struct nls_table *nls_cp)
394 char *bcc_ptr = *pbcc_area;
397 /* BB FIXME add check that strings total less
398 than 335 or will need to send them as arrays */
400 /* unicode strings, must be word aligned before the call */
401 /* if ((long) bcc_ptr % 2) {
406 if (ses->user_name == NULL) {
407 /* null user mount */
411 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
412 CIFS_MAX_USERNAME_LEN, nls_cp);
414 bcc_ptr += 2 * bytes_ret;
415 bcc_ptr += 2; /* account for null termination */
417 unicode_domain_string(&bcc_ptr, ses, nls_cp);
418 unicode_oslm_strings(&bcc_ptr, nls_cp);
420 *pbcc_area = bcc_ptr;
423 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
424 const struct nls_table *nls_cp)
426 char *bcc_ptr = *pbcc_area;
430 /* BB what about null user mounts - check that we do this BB */
432 if (ses->user_name != NULL) {
433 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
434 if (WARN_ON_ONCE(len < 0))
435 len = CIFS_MAX_USERNAME_LEN - 1;
438 /* else null user mount */
440 bcc_ptr++; /* account for null termination */
443 if (ses->domainName != NULL) {
444 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
445 if (WARN_ON_ONCE(len < 0))
446 len = CIFS_MAX_DOMAINNAME_LEN - 1;
448 } /* else we will send a null domain name
449 so the server will default to its own domain */
453 /* BB check for overflow here */
455 strcpy(bcc_ptr, "Linux version ");
456 bcc_ptr += strlen("Linux version ");
457 strcpy(bcc_ptr, init_utsname()->release);
458 bcc_ptr += strlen(init_utsname()->release) + 1;
460 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
461 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
463 *pbcc_area = bcc_ptr;
467 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
468 const struct nls_table *nls_cp)
471 char *data = *pbcc_area;
473 cifs_dbg(FYI, "bleft %d\n", bleft);
475 kfree(ses->serverOS);
476 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
477 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
478 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
484 kfree(ses->serverNOS);
485 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
486 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
487 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
493 kfree(ses->serverDomain);
494 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
495 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
500 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
501 struct cifs_ses *ses,
502 const struct nls_table *nls_cp)
505 char *bcc_ptr = *pbcc_area;
507 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
509 len = strnlen(bcc_ptr, bleft);
513 kfree(ses->serverOS);
515 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
517 memcpy(ses->serverOS, bcc_ptr, len);
518 ses->serverOS[len] = 0;
519 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
520 cifs_dbg(FYI, "OS/2 server\n");
526 len = strnlen(bcc_ptr, bleft);
530 kfree(ses->serverNOS);
532 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
533 if (ses->serverNOS) {
534 memcpy(ses->serverNOS, bcc_ptr, len);
535 ses->serverNOS[len] = 0;
541 len = strnlen(bcc_ptr, bleft);
545 /* No domain field in LANMAN case. Domain is
546 returned by old servers in the SMB negprot response */
547 /* BB For newer servers which do not support Unicode,
548 but thus do return domain here we could add parsing
549 for it later, but it is not very important */
550 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
553 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
554 struct cifs_ses *ses)
556 unsigned int tioffset; /* challenge message target info area */
557 unsigned int tilen; /* challenge message target info area length */
559 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
561 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
562 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
566 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
567 cifs_dbg(VFS, "blob signature incorrect %s\n",
571 if (pblob->MessageType != NtLmChallenge) {
572 cifs_dbg(VFS, "Incorrect message type %d\n",
577 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
578 /* BB we could decode pblob->NegotiateFlags; some may be useful */
579 /* In particular we can examine sign flags */
580 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
581 we must set the MIC field of the AUTHENTICATE_MESSAGE */
582 ses->ntlmssp->server_flags = le32_to_cpu(pblob->NegotiateFlags);
583 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
584 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
585 if (tioffset > blob_len || tioffset + tilen > blob_len) {
586 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
591 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
593 if (!ses->auth_key.response) {
594 cifs_dbg(VFS, "Challenge target info alloc failure\n");
597 ses->auth_key.len = tilen;
603 /* BB Move to ntlmssp.c eventually */
605 /* We do not malloc the blob, it is passed in pbuffer, because
606 it is fixed size, and small, making this approach cleaner */
607 void build_ntlmssp_negotiate_blob(unsigned char *pbuffer,
608 struct cifs_ses *ses)
610 struct TCP_Server_Info *server = cifs_ses_server(ses);
611 NEGOTIATE_MESSAGE *sec_blob = (NEGOTIATE_MESSAGE *)pbuffer;
614 memset(pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
615 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
616 sec_blob->MessageType = NtLmNegotiate;
618 /* BB is NTLMV2 session security format easier to use here? */
619 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
620 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
621 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
622 NTLMSSP_NEGOTIATE_SEAL;
624 flags |= NTLMSSP_NEGOTIATE_SIGN;
625 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
626 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
628 sec_blob->NegotiateFlags = cpu_to_le32(flags);
630 sec_blob->WorkstationName.BufferOffset = 0;
631 sec_blob->WorkstationName.Length = 0;
632 sec_blob->WorkstationName.MaximumLength = 0;
634 /* Domain name is sent on the Challenge not Negotiate NTLMSSP request */
635 sec_blob->DomainName.BufferOffset = 0;
636 sec_blob->DomainName.Length = 0;
637 sec_blob->DomainName.MaximumLength = 0;
640 static int size_of_ntlmssp_blob(struct cifs_ses *ses)
642 int sz = sizeof(AUTHENTICATE_MESSAGE) + ses->auth_key.len
643 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
646 sz += 2 * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
651 sz += 2 * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
658 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
660 struct cifs_ses *ses,
661 const struct nls_table *nls_cp)
664 AUTHENTICATE_MESSAGE *sec_blob;
668 rc = setup_ntlmv2_rsp(ses, nls_cp);
670 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
672 goto setup_ntlmv2_ret;
674 *pbuffer = kmalloc(size_of_ntlmssp_blob(ses), GFP_KERNEL);
677 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
679 goto setup_ntlmv2_ret;
681 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
683 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
684 sec_blob->MessageType = NtLmAuthenticate;
686 flags = NTLMSSP_NEGOTIATE_56 |
687 NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_TARGET_INFO |
688 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
689 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
690 NTLMSSP_NEGOTIATE_SEAL;
691 if (ses->server->sign)
692 flags |= NTLMSSP_NEGOTIATE_SIGN;
693 if (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
694 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
696 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
697 sec_blob->NegotiateFlags = cpu_to_le32(flags);
699 sec_blob->LmChallengeResponse.BufferOffset =
700 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
701 sec_blob->LmChallengeResponse.Length = 0;
702 sec_blob->LmChallengeResponse.MaximumLength = 0;
704 sec_blob->NtChallengeResponse.BufferOffset =
705 cpu_to_le32(tmp - *pbuffer);
706 if (ses->user_name != NULL) {
707 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
708 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
709 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
711 sec_blob->NtChallengeResponse.Length =
712 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
713 sec_blob->NtChallengeResponse.MaximumLength =
714 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
717 * don't send an NT Response for anonymous access
719 sec_blob->NtChallengeResponse.Length = 0;
720 sec_blob->NtChallengeResponse.MaximumLength = 0;
723 if (ses->domainName == NULL) {
724 sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
725 sec_blob->DomainName.Length = 0;
726 sec_blob->DomainName.MaximumLength = 0;
730 len = cifs_strtoUTF16((__le16 *)tmp, ses->domainName,
731 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
732 len *= 2; /* unicode is 2 bytes each */
733 sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
734 sec_blob->DomainName.Length = cpu_to_le16(len);
735 sec_blob->DomainName.MaximumLength = cpu_to_le16(len);
739 if (ses->user_name == NULL) {
740 sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
741 sec_blob->UserName.Length = 0;
742 sec_blob->UserName.MaximumLength = 0;
746 len = cifs_strtoUTF16((__le16 *)tmp, ses->user_name,
747 CIFS_MAX_USERNAME_LEN, nls_cp);
748 len *= 2; /* unicode is 2 bytes each */
749 sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
750 sec_blob->UserName.Length = cpu_to_le16(len);
751 sec_blob->UserName.MaximumLength = cpu_to_le16(len);
755 sec_blob->WorkstationName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
756 sec_blob->WorkstationName.Length = 0;
757 sec_blob->WorkstationName.MaximumLength = 0;
760 if (((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) ||
761 (ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC))
762 && !calc_seckey(ses)) {
763 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
764 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
765 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
766 sec_blob->SessionKey.MaximumLength =
767 cpu_to_le16(CIFS_CPHTXT_SIZE);
768 tmp += CIFS_CPHTXT_SIZE;
770 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
771 sec_blob->SessionKey.Length = 0;
772 sec_blob->SessionKey.MaximumLength = 0;
775 *buflen = tmp - *pbuffer;
781 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
783 switch (server->negflavor) {
784 case CIFS_NEGFLAVOR_EXTENDED:
790 if (server->sec_ntlmssp &&
791 (global_secflags & CIFSSEC_MAY_NTLMSSP))
793 if ((server->sec_kerberos || server->sec_mskerberos) &&
794 (global_secflags & CIFSSEC_MAY_KRB5))
800 case CIFS_NEGFLAVOR_UNENCAP:
806 if (global_secflags & CIFSSEC_MAY_NTLMV2)
808 if (global_secflags & CIFSSEC_MAY_NTLM)
814 fallthrough; /* to attempt LANMAN authentication next */
815 case CIFS_NEGFLAVOR_LANMAN:
820 if (global_secflags & CIFSSEC_MAY_LANMAN)
833 struct cifs_ses *ses;
834 struct nls_table *nls_cp;
835 void (*func)(struct sess_data *);
838 /* we will send the SMB in three pieces:
839 * a fixed length beginning part, an optional
840 * SPNEGO blob (which can be zero length), and a
841 * last part which will include the strings
842 * and rest of bcc area. This allows us to avoid
843 * a large buffer 17K allocation
850 sess_alloc_buffer(struct sess_data *sess_data, int wct)
853 struct cifs_ses *ses = sess_data->ses;
854 struct smb_hdr *smb_buf;
856 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
862 sess_data->iov[0].iov_base = (char *)smb_buf;
863 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
865 * This variable will be used to clear the buffer
866 * allocated above in case of any error in the calling function.
868 sess_data->buf0_type = CIFS_SMALL_BUFFER;
870 /* 2000 big enough to fit max user, domain, NOS name etc. */
871 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
872 if (!sess_data->iov[2].iov_base) {
874 goto out_free_smb_buf;
881 sess_data->iov[0].iov_base = NULL;
882 sess_data->iov[0].iov_len = 0;
883 sess_data->buf0_type = CIFS_NO_BUFFER;
888 sess_free_buffer(struct sess_data *sess_data)
891 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
892 sess_data->buf0_type = CIFS_NO_BUFFER;
893 kfree(sess_data->iov[2].iov_base);
897 sess_establish_session(struct sess_data *sess_data)
899 struct cifs_ses *ses = sess_data->ses;
901 mutex_lock(&ses->server->srv_mutex);
902 if (!ses->server->session_estab) {
903 if (ses->server->sign) {
904 ses->server->session_key.response =
905 kmemdup(ses->auth_key.response,
906 ses->auth_key.len, GFP_KERNEL);
907 if (!ses->server->session_key.response) {
908 mutex_unlock(&ses->server->srv_mutex);
911 ses->server->session_key.len =
914 ses->server->sequence_number = 0x2;
915 ses->server->session_estab = true;
917 mutex_unlock(&ses->server->srv_mutex);
919 cifs_dbg(FYI, "CIFS session established successfully\n");
920 spin_lock(&GlobalMid_Lock);
921 ses->status = CifsGood;
922 ses->need_reconnect = false;
923 spin_unlock(&GlobalMid_Lock);
929 sess_sendreceive(struct sess_data *sess_data)
932 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
934 struct kvec rsp_iov = { NULL, 0 };
936 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
937 be32_add_cpu(&smb_buf->smb_buf_length, count);
938 put_bcc(count, smb_buf);
940 rc = SendReceive2(sess_data->xid, sess_data->ses,
941 sess_data->iov, 3 /* num_iovecs */,
942 &sess_data->buf0_type,
943 CIFS_LOG_ERROR, &rsp_iov);
944 cifs_small_buf_release(sess_data->iov[0].iov_base);
945 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
951 * LANMAN and plaintext are less secure and off by default.
952 * So we make this explicitly be turned on in kconfig (in the
953 * build) and turned on at runtime (changed from the default)
954 * in proc/fs/cifs or via mount parm. Unfortunately this is
955 * needed for old Win (e.g. Win95), some obscure NAS and OS/2
957 #ifdef CONFIG_CIFS_WEAK_PW_HASH
959 sess_auth_lanman(struct sess_data *sess_data)
962 struct smb_hdr *smb_buf;
963 SESSION_SETUP_ANDX *pSMB;
965 struct cifs_ses *ses = sess_data->ses;
966 char lnm_session_key[CIFS_AUTH_RESP_SIZE];
967 __u16 bytes_remaining;
969 /* lanman 2 style sessionsetup */
971 rc = sess_alloc_buffer(sess_data, 10);
975 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
976 bcc_ptr = sess_data->iov[2].iov_base;
977 (void)cifs_ssetup_hdr(ses, pSMB);
979 pSMB->req.hdr.Flags2 &= ~SMBFLG2_UNICODE;
981 if (ses->user_name != NULL) {
982 /* no capabilities flags in old lanman negotiation */
983 pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
985 /* Calculate hash with password and copy into bcc_ptr.
986 * Encryption Key (stored as in cryptkey) gets used if the
987 * security mode bit in Negotiate Protocol response states
988 * to use challenge/response method (i.e. Password bit is 1).
990 rc = calc_lanman_hash(ses->password, ses->server->cryptkey,
991 ses->server->sec_mode & SECMODE_PW_ENCRYPT ?
992 true : false, lnm_session_key);
996 memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_AUTH_RESP_SIZE);
997 bcc_ptr += CIFS_AUTH_RESP_SIZE;
999 pSMB->old_req.PasswordLength = 0;
1003 * can not sign if LANMAN negotiated so no need
1004 * to calculate signing key? but what if server
1005 * changed to do higher than lanman dialect and
1006 * we reconnected would we ever calc signing_key?
1009 cifs_dbg(FYI, "Negotiating LANMAN setting up strings\n");
1010 /* Unicode not allowed for LANMAN dialects */
1011 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1013 sess_data->iov[2].iov_len = (long) bcc_ptr -
1014 (long) sess_data->iov[2].iov_base;
1016 rc = sess_sendreceive(sess_data);
1020 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1021 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1023 /* lanman response has a word count of 3 */
1024 if (smb_buf->WordCount != 3) {
1026 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1030 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1031 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1033 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1034 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1036 bytes_remaining = get_bcc(smb_buf);
1037 bcc_ptr = pByteArea(smb_buf);
1039 /* BB check if Unicode and decode strings */
1040 if (bytes_remaining == 0) {
1041 /* no string area to decode, do nothing */
1042 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1043 /* unicode string area must be word-aligned */
1044 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1048 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1051 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1055 rc = sess_establish_session(sess_data);
1057 sess_data->result = rc;
1058 sess_data->func = NULL;
1059 sess_free_buffer(sess_data);
1065 sess_auth_ntlm(struct sess_data *sess_data)
1068 struct smb_hdr *smb_buf;
1069 SESSION_SETUP_ANDX *pSMB;
1071 struct cifs_ses *ses = sess_data->ses;
1073 __u16 bytes_remaining;
1075 /* old style NTLM sessionsetup */
1077 rc = sess_alloc_buffer(sess_data, 13);
1081 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1082 bcc_ptr = sess_data->iov[2].iov_base;
1083 capabilities = cifs_ssetup_hdr(ses, pSMB);
1085 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1086 if (ses->user_name != NULL) {
1087 pSMB->req_no_secext.CaseInsensitivePasswordLength =
1088 cpu_to_le16(CIFS_AUTH_RESP_SIZE);
1089 pSMB->req_no_secext.CaseSensitivePasswordLength =
1090 cpu_to_le16(CIFS_AUTH_RESP_SIZE);
1092 /* calculate ntlm response and session key */
1093 rc = setup_ntlm_response(ses, sess_data->nls_cp);
1095 cifs_dbg(VFS, "Error %d during NTLM authentication\n",
1100 /* copy ntlm response */
1101 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1102 CIFS_AUTH_RESP_SIZE);
1103 bcc_ptr += CIFS_AUTH_RESP_SIZE;
1104 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1105 CIFS_AUTH_RESP_SIZE);
1106 bcc_ptr += CIFS_AUTH_RESP_SIZE;
1108 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1109 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1112 if (ses->capabilities & CAP_UNICODE) {
1113 /* unicode strings must be word aligned */
1114 if (sess_data->iov[0].iov_len % 2) {
1118 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1120 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1124 sess_data->iov[2].iov_len = (long) bcc_ptr -
1125 (long) sess_data->iov[2].iov_base;
1127 rc = sess_sendreceive(sess_data);
1131 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1132 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1134 if (smb_buf->WordCount != 3) {
1136 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1140 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1141 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1143 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1144 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1146 bytes_remaining = get_bcc(smb_buf);
1147 bcc_ptr = pByteArea(smb_buf);
1149 /* BB check if Unicode and decode strings */
1150 if (bytes_remaining == 0) {
1151 /* no string area to decode, do nothing */
1152 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1153 /* unicode string area must be word-aligned */
1154 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1158 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1161 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1165 rc = sess_establish_session(sess_data);
1167 sess_data->result = rc;
1168 sess_data->func = NULL;
1169 sess_free_buffer(sess_data);
1170 kfree(ses->auth_key.response);
1171 ses->auth_key.response = NULL;
1175 sess_auth_ntlmv2(struct sess_data *sess_data)
1178 struct smb_hdr *smb_buf;
1179 SESSION_SETUP_ANDX *pSMB;
1181 struct cifs_ses *ses = sess_data->ses;
1183 __u16 bytes_remaining;
1185 /* old style NTLM sessionsetup */
1187 rc = sess_alloc_buffer(sess_data, 13);
1191 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1192 bcc_ptr = sess_data->iov[2].iov_base;
1193 capabilities = cifs_ssetup_hdr(ses, pSMB);
1195 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1197 /* LM2 password would be here if we supported it */
1198 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1200 if (ses->user_name != NULL) {
1201 /* calculate nlmv2 response and session key */
1202 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1204 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1208 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1209 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1210 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1212 /* set case sensitive password length after tilen may get
1213 * assigned, tilen is 0 otherwise.
1215 pSMB->req_no_secext.CaseSensitivePasswordLength =
1216 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1218 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1221 if (ses->capabilities & CAP_UNICODE) {
1222 if (sess_data->iov[0].iov_len % 2) {
1226 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1228 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1232 sess_data->iov[2].iov_len = (long) bcc_ptr -
1233 (long) sess_data->iov[2].iov_base;
1235 rc = sess_sendreceive(sess_data);
1239 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1240 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1242 if (smb_buf->WordCount != 3) {
1244 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1248 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1249 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1251 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1252 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1254 bytes_remaining = get_bcc(smb_buf);
1255 bcc_ptr = pByteArea(smb_buf);
1257 /* BB check if Unicode and decode strings */
1258 if (bytes_remaining == 0) {
1259 /* no string area to decode, do nothing */
1260 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1261 /* unicode string area must be word-aligned */
1262 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1266 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1269 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1273 rc = sess_establish_session(sess_data);
1275 sess_data->result = rc;
1276 sess_data->func = NULL;
1277 sess_free_buffer(sess_data);
1278 kfree(ses->auth_key.response);
1279 ses->auth_key.response = NULL;
1282 #ifdef CONFIG_CIFS_UPCALL
1284 sess_auth_kerberos(struct sess_data *sess_data)
1287 struct smb_hdr *smb_buf;
1288 SESSION_SETUP_ANDX *pSMB;
1290 struct cifs_ses *ses = sess_data->ses;
1292 __u16 bytes_remaining;
1293 struct key *spnego_key = NULL;
1294 struct cifs_spnego_msg *msg;
1297 /* extended security */
1299 rc = sess_alloc_buffer(sess_data, 12);
1303 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1304 bcc_ptr = sess_data->iov[2].iov_base;
1305 capabilities = cifs_ssetup_hdr(ses, pSMB);
1307 spnego_key = cifs_get_spnego_key(ses);
1308 if (IS_ERR(spnego_key)) {
1309 rc = PTR_ERR(spnego_key);
1314 msg = spnego_key->payload.data[0];
1316 * check version field to make sure that cifs.upcall is
1317 * sending us a response in an expected form
1319 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1320 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1321 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1323 goto out_put_spnego_key;
1326 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1328 if (!ses->auth_key.response) {
1329 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1332 goto out_put_spnego_key;
1334 ses->auth_key.len = msg->sesskey_len;
1336 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1337 capabilities |= CAP_EXTENDED_SECURITY;
1338 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1339 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1340 sess_data->iov[1].iov_len = msg->secblob_len;
1341 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1343 if (ses->capabilities & CAP_UNICODE) {
1344 /* unicode strings must be word aligned */
1345 if ((sess_data->iov[0].iov_len
1346 + sess_data->iov[1].iov_len) % 2) {
1350 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1351 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1353 /* BB: is this right? */
1354 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1357 sess_data->iov[2].iov_len = (long) bcc_ptr -
1358 (long) sess_data->iov[2].iov_base;
1360 rc = sess_sendreceive(sess_data);
1362 goto out_put_spnego_key;
1364 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1365 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1367 if (smb_buf->WordCount != 4) {
1369 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1370 goto out_put_spnego_key;
1373 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1374 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1376 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1377 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1379 bytes_remaining = get_bcc(smb_buf);
1380 bcc_ptr = pByteArea(smb_buf);
1382 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1383 if (blob_len > bytes_remaining) {
1384 cifs_dbg(VFS, "bad security blob length %d\n",
1387 goto out_put_spnego_key;
1389 bcc_ptr += blob_len;
1390 bytes_remaining -= blob_len;
1392 /* BB check if Unicode and decode strings */
1393 if (bytes_remaining == 0) {
1394 /* no string area to decode, do nothing */
1395 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1396 /* unicode string area must be word-aligned */
1397 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1401 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1404 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1408 rc = sess_establish_session(sess_data);
1410 key_invalidate(spnego_key);
1411 key_put(spnego_key);
1413 sess_data->result = rc;
1414 sess_data->func = NULL;
1415 sess_free_buffer(sess_data);
1416 kfree(ses->auth_key.response);
1417 ses->auth_key.response = NULL;
1420 #endif /* ! CONFIG_CIFS_UPCALL */
1423 * The required kvec buffers have to be allocated before calling this
1427 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1429 SESSION_SETUP_ANDX *pSMB;
1430 struct cifs_ses *ses = sess_data->ses;
1434 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1436 capabilities = cifs_ssetup_hdr(ses, pSMB);
1437 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1438 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1442 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1443 capabilities |= CAP_EXTENDED_SECURITY;
1444 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1446 bcc_ptr = sess_data->iov[2].iov_base;
1447 /* unicode strings must be word aligned */
1448 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1452 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1454 sess_data->iov[2].iov_len = (long) bcc_ptr -
1455 (long) sess_data->iov[2].iov_base;
1461 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1464 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1467 struct smb_hdr *smb_buf;
1468 SESSION_SETUP_ANDX *pSMB;
1469 struct cifs_ses *ses = sess_data->ses;
1470 __u16 bytes_remaining;
1474 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1477 * if memory allocation is successful, caller of this function
1480 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1481 if (!ses->ntlmssp) {
1485 ses->ntlmssp->sesskey_per_smbsess = false;
1488 rc = sess_alloc_buffer(sess_data, 12);
1492 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1494 /* Build security blob before we assemble the request */
1495 build_ntlmssp_negotiate_blob(pSMB->req.SecurityBlob, ses);
1496 sess_data->iov[1].iov_len = sizeof(NEGOTIATE_MESSAGE);
1497 sess_data->iov[1].iov_base = pSMB->req.SecurityBlob;
1498 pSMB->req.SecurityBlobLength = cpu_to_le16(sizeof(NEGOTIATE_MESSAGE));
1500 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1504 rc = sess_sendreceive(sess_data);
1506 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1507 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1509 /* If true, rc here is expected and not an error */
1510 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1511 smb_buf->Status.CifsError ==
1512 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1518 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1520 if (smb_buf->WordCount != 4) {
1522 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1526 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1527 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1529 bytes_remaining = get_bcc(smb_buf);
1530 bcc_ptr = pByteArea(smb_buf);
1532 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1533 if (blob_len > bytes_remaining) {
1534 cifs_dbg(VFS, "bad security blob length %d\n",
1540 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1542 sess_free_buffer(sess_data);
1545 sess_data->func = sess_auth_rawntlmssp_authenticate;
1549 /* Else error. Cleanup */
1550 kfree(ses->auth_key.response);
1551 ses->auth_key.response = NULL;
1552 kfree(ses->ntlmssp);
1553 ses->ntlmssp = NULL;
1555 sess_data->func = NULL;
1556 sess_data->result = rc;
1560 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1563 struct smb_hdr *smb_buf;
1564 SESSION_SETUP_ANDX *pSMB;
1565 struct cifs_ses *ses = sess_data->ses;
1566 __u16 bytes_remaining;
1568 unsigned char *ntlmsspblob = NULL;
1571 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1574 rc = sess_alloc_buffer(sess_data, 12);
1578 /* Build security blob before we assemble the request */
1579 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1580 smb_buf = (struct smb_hdr *)pSMB;
1581 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1582 &blob_len, ses, sess_data->nls_cp);
1584 goto out_free_ntlmsspblob;
1585 sess_data->iov[1].iov_len = blob_len;
1586 sess_data->iov[1].iov_base = ntlmsspblob;
1587 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1589 * Make sure that we tell the server that we are using
1590 * the uid that it just gave us back on the response
1593 smb_buf->Uid = ses->Suid;
1595 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1597 goto out_free_ntlmsspblob;
1599 rc = sess_sendreceive(sess_data);
1601 goto out_free_ntlmsspblob;
1603 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1604 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1605 if (smb_buf->WordCount != 4) {
1607 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1608 goto out_free_ntlmsspblob;
1611 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1612 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1614 if (ses->Suid != smb_buf->Uid) {
1615 ses->Suid = smb_buf->Uid;
1616 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1619 bytes_remaining = get_bcc(smb_buf);
1620 bcc_ptr = pByteArea(smb_buf);
1621 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1622 if (blob_len > bytes_remaining) {
1623 cifs_dbg(VFS, "bad security blob length %d\n",
1626 goto out_free_ntlmsspblob;
1628 bcc_ptr += blob_len;
1629 bytes_remaining -= blob_len;
1632 /* BB check if Unicode and decode strings */
1633 if (bytes_remaining == 0) {
1634 /* no string area to decode, do nothing */
1635 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1636 /* unicode string area must be word-aligned */
1637 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1641 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1644 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1648 out_free_ntlmsspblob:
1651 sess_free_buffer(sess_data);
1654 rc = sess_establish_session(sess_data);
1657 kfree(ses->auth_key.response);
1658 ses->auth_key.response = NULL;
1659 kfree(ses->ntlmssp);
1660 ses->ntlmssp = NULL;
1662 sess_data->func = NULL;
1663 sess_data->result = rc;
1666 static int select_sec(struct cifs_ses *ses, struct sess_data *sess_data)
1670 type = cifs_select_sectype(ses->server, ses->sectype);
1671 cifs_dbg(FYI, "sess setup type %d\n", type);
1672 if (type == Unspecified) {
1673 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1679 /* LANMAN and plaintext are less secure and off by default.
1680 * So we make this explicitly be turned on in kconfig (in the
1681 * build) and turned on at runtime (changed from the default)
1682 * in proc/fs/cifs or via mount parm. Unfortunately this is
1683 * needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
1684 #ifdef CONFIG_CIFS_WEAK_PW_HASH
1685 sess_data->func = sess_auth_lanman;
1691 sess_data->func = sess_auth_ntlm;
1694 sess_data->func = sess_auth_ntlmv2;
1697 #ifdef CONFIG_CIFS_UPCALL
1698 sess_data->func = sess_auth_kerberos;
1701 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1703 #endif /* CONFIG_CIFS_UPCALL */
1705 sess_data->func = sess_auth_rawntlmssp_negotiate;
1708 cifs_dbg(VFS, "secType %d not supported!\n", type);
1715 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1716 const struct nls_table *nls_cp)
1719 struct sess_data *sess_data;
1722 WARN(1, "%s: ses == NULL!", __func__);
1726 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1730 rc = select_sec(ses, sess_data);
1734 sess_data->xid = xid;
1735 sess_data->ses = ses;
1736 sess_data->buf0_type = CIFS_NO_BUFFER;
1737 sess_data->nls_cp = (struct nls_table *) nls_cp;
1739 while (sess_data->func)
1740 sess_data->func(sess_data);
1742 /* Store result before we free sess_data */
1743 rc = sess_data->result;