4 * SMB/CIFS session setup handling routines
6 * Copyright (c) International Business Machines Corp., 2006, 2009
7 * Author(s): Steve French (sfrench@us.ibm.com)
9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "cifsproto.h"
27 #include "cifs_unicode.h"
28 #include "cifs_debug.h"
31 #include <linux/utsname.h>
32 #include <linux/slab.h>
33 #include "cifs_spnego.h"
34 #include "smb2proto.h"
37 is_server_using_iface(struct TCP_Server_Info *server,
38 struct cifs_server_iface *iface)
40 struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr;
41 struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr;
42 struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr;
43 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr;
45 if (server->dstaddr.ss_family != iface->sockaddr.ss_family)
47 if (server->dstaddr.ss_family == AF_INET) {
48 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
50 } else if (server->dstaddr.ss_family == AF_INET6) {
51 if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
52 sizeof(i6->sin6_addr)) != 0)
55 /* unknown family.. */
61 bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
65 for (i = 0; i < ses->chan_count; i++) {
66 if (is_server_using_iface(ses->chans[i].server, iface))
72 /* returns number of channels added */
73 int cifs_try_adding_channels(struct cifs_ses *ses)
75 int old_chan_count = ses->chan_count;
76 int left = ses->chan_max - ses->chan_count;
80 struct cifs_server_iface *ifaces = NULL;
85 "ses already at max_channels (%zu), nothing to open\n",
90 if (ses->server->dialect < SMB30_PROT_ID) {
91 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
96 * Make a copy of the iface list at the time and use that
97 * instead so as to not hold the iface spinlock for opening
100 spin_lock(&ses->iface_lock);
101 iface_count = ses->iface_count;
102 if (iface_count <= 0) {
103 spin_unlock(&ses->iface_lock);
104 cifs_dbg(VFS, "no iface list available to open channels\n");
107 ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces),
110 spin_unlock(&ses->iface_lock);
113 spin_unlock(&ses->iface_lock);
116 * Keep connecting to same, fastest, iface for all channels as
117 * long as its RSS. Try next fastest one if not RSS or channel
121 struct cifs_server_iface *iface;
124 if (tries > 3*ses->chan_max) {
125 cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
131 if (is_ses_using_iface(ses, iface) && !iface->rss_capable) {
132 i = (i+1) % iface_count;
136 rc = cifs_ses_add_channel(ses, iface);
138 cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n",
140 i = (i+1) % iface_count;
144 cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
150 return ses->chan_count - old_chan_count;
154 cifs_ses_add_channel(struct cifs_ses *ses, struct cifs_server_iface *iface)
156 struct cifs_chan *chan;
157 struct smb_vol vol = {NULL};
158 static const char unc_fmt[] = "\\%s\\foo";
159 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
160 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
161 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
163 unsigned int xid = get_xid();
165 if (iface->sockaddr.ss_family == AF_INET)
166 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
167 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
170 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
171 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
175 * Setup a smb_vol with mostly the same info as the existing
176 * session and overwrite it with the requested iface data.
178 * We need to setup at least the fields used for negprot and
181 * We only need the volume here, so we can reuse memory from
182 * the session and server without caring about memory
186 /* Always make new connection for now (TODO?) */
187 vol.nosharesock = true;
190 vol.domainauto = ses->domainAuto;
191 vol.domainname = ses->domainName;
192 vol.username = ses->user_name;
193 vol.password = ses->password;
194 vol.sectype = ses->sectype;
195 vol.sign = ses->sign;
198 /* XXX: Use ses->server->hostname? */
199 sprintf(unc, unc_fmt, ses->serverName);
203 /* Reuse same version as master connection */
204 vol.vals = ses->server->vals;
205 vol.ops = ses->server->ops;
207 vol.noblocksnd = ses->server->noblocksnd;
208 vol.noautotune = ses->server->noautotune;
209 vol.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
210 vol.echo_interval = ses->server->echo_interval / HZ;
213 * This will be used for encoding/decoding user/domain/pw
214 * during sess setup auth.
216 * XXX: We use the default for simplicity but the proper way
217 * would be to use the one that ses used, which is not
218 * stored. This might break when dealing with non-ascii
221 vol.local_nls = load_nls_default();
223 /* Use RDMA if possible */
224 vol.rdma = iface->rdma_capable;
225 memcpy(&vol.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
227 /* reuse master con client guid */
228 memcpy(&vol.client_guid, ses->server->client_guid,
229 SMB2_CLIENT_GUID_SIZE);
230 vol.use_client_guid = true;
232 mutex_lock(&ses->session_mutex);
234 chan = ses->binding_chan = &ses->chans[ses->chan_count];
235 chan->server = cifs_get_tcp_session(&vol);
236 if (IS_ERR(chan->server)) {
237 rc = PTR_ERR(chan->server);
241 spin_lock(&cifs_tcp_ses_lock);
242 chan->server->is_channel = true;
243 spin_unlock(&cifs_tcp_ses_lock);
246 * We need to allocate the server crypto now as we will need
247 * to sign packets before we generate the channel signing key
248 * (we sign with the session key)
250 rc = smb311_crypto_shash_allocate(chan->server);
252 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
257 rc = cifs_negotiate_protocol(xid, ses);
261 rc = cifs_setup_session(xid, ses, vol.local_nls);
265 /* success, put it on the list
266 * XXX: sharing ses between 2 tcp servers is not possible, the
267 * way "internal" linked lists works in linux makes element
268 * only able to belong to one list
270 * the binding session is already established so the rest of
271 * the code should be able to look it up, no need to add the
272 * ses to the new server.
276 atomic_set(&ses->chan_seq, 0);
278 ses->binding = false;
279 ses->binding_chan = NULL;
280 mutex_unlock(&ses->session_mutex);
282 if (rc && chan->server)
283 cifs_put_tcp_session(chan->server, 0);
284 unload_nls(vol.local_nls);
289 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses, SESSION_SETUP_ANDX *pSMB)
291 __u32 capabilities = 0;
293 /* init fields common to all four types of SessSetup */
294 /* Note that offsets for first seven fields in req struct are same */
295 /* in CIFS Specs so does not matter which of 3 forms of struct */
296 /* that we use in next few lines */
297 /* Note that header is initialized to zero in header_assemble */
298 pSMB->req.AndXCommand = 0xFF;
299 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
300 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
302 pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
303 pSMB->req.VcNumber = cpu_to_le16(1);
305 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
307 /* BB verify whether signing required on neg or just on auth frame
310 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
311 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
313 if (ses->server->sign)
314 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
316 if (ses->capabilities & CAP_UNICODE) {
317 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
318 capabilities |= CAP_UNICODE;
320 if (ses->capabilities & CAP_STATUS32) {
321 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
322 capabilities |= CAP_STATUS32;
324 if (ses->capabilities & CAP_DFS) {
325 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
326 capabilities |= CAP_DFS;
328 if (ses->capabilities & CAP_UNIX)
329 capabilities |= CAP_UNIX;
335 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
337 char *bcc_ptr = *pbcc_area;
340 /* Copy OS version */
341 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
343 bcc_ptr += 2 * bytes_ret;
344 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
346 bcc_ptr += 2 * bytes_ret;
347 bcc_ptr += 2; /* trailing null */
349 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
351 bcc_ptr += 2 * bytes_ret;
352 bcc_ptr += 2; /* trailing null */
354 *pbcc_area = bcc_ptr;
357 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
358 const struct nls_table *nls_cp)
360 char *bcc_ptr = *pbcc_area;
364 if (ses->domainName == NULL) {
365 /* Sending null domain better than using a bogus domain name (as
366 we did briefly in 2.6.18) since server will use its default */
371 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
372 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
373 bcc_ptr += 2 * bytes_ret;
374 bcc_ptr += 2; /* account for null terminator */
376 *pbcc_area = bcc_ptr;
380 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
381 const struct nls_table *nls_cp)
383 char *bcc_ptr = *pbcc_area;
386 /* BB FIXME add check that strings total less
387 than 335 or will need to send them as arrays */
389 /* unicode strings, must be word aligned before the call */
390 /* if ((long) bcc_ptr % 2) {
395 if (ses->user_name == NULL) {
396 /* null user mount */
400 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
401 CIFS_MAX_USERNAME_LEN, nls_cp);
403 bcc_ptr += 2 * bytes_ret;
404 bcc_ptr += 2; /* account for null termination */
406 unicode_domain_string(&bcc_ptr, ses, nls_cp);
407 unicode_oslm_strings(&bcc_ptr, nls_cp);
409 *pbcc_area = bcc_ptr;
412 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
413 const struct nls_table *nls_cp)
415 char *bcc_ptr = *pbcc_area;
419 /* BB what about null user mounts - check that we do this BB */
421 if (ses->user_name != NULL) {
422 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
423 if (WARN_ON_ONCE(len < 0))
424 len = CIFS_MAX_USERNAME_LEN - 1;
427 /* else null user mount */
429 bcc_ptr++; /* account for null termination */
432 if (ses->domainName != NULL) {
433 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
434 if (WARN_ON_ONCE(len < 0))
435 len = CIFS_MAX_DOMAINNAME_LEN - 1;
437 } /* else we will send a null domain name
438 so the server will default to its own domain */
442 /* BB check for overflow here */
444 strcpy(bcc_ptr, "Linux version ");
445 bcc_ptr += strlen("Linux version ");
446 strcpy(bcc_ptr, init_utsname()->release);
447 bcc_ptr += strlen(init_utsname()->release) + 1;
449 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
450 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
452 *pbcc_area = bcc_ptr;
456 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
457 const struct nls_table *nls_cp)
460 char *data = *pbcc_area;
462 cifs_dbg(FYI, "bleft %d\n", bleft);
464 kfree(ses->serverOS);
465 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
466 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
467 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
473 kfree(ses->serverNOS);
474 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
475 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
476 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
482 kfree(ses->serverDomain);
483 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
484 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
489 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
490 struct cifs_ses *ses,
491 const struct nls_table *nls_cp)
494 char *bcc_ptr = *pbcc_area;
496 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
498 len = strnlen(bcc_ptr, bleft);
502 kfree(ses->serverOS);
504 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
506 memcpy(ses->serverOS, bcc_ptr, len);
507 ses->serverOS[len] = 0;
508 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
509 cifs_dbg(FYI, "OS/2 server\n");
515 len = strnlen(bcc_ptr, bleft);
519 kfree(ses->serverNOS);
521 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
522 if (ses->serverNOS) {
523 memcpy(ses->serverNOS, bcc_ptr, len);
524 ses->serverNOS[len] = 0;
530 len = strnlen(bcc_ptr, bleft);
534 /* No domain field in LANMAN case. Domain is
535 returned by old servers in the SMB negprot response */
536 /* BB For newer servers which do not support Unicode,
537 but thus do return domain here we could add parsing
538 for it later, but it is not very important */
539 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
542 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
543 struct cifs_ses *ses)
545 unsigned int tioffset; /* challenge message target info area */
546 unsigned int tilen; /* challenge message target info area length */
548 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
550 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
551 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
555 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
556 cifs_dbg(VFS, "blob signature incorrect %s\n",
560 if (pblob->MessageType != NtLmChallenge) {
561 cifs_dbg(VFS, "Incorrect message type %d\n",
566 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
567 /* BB we could decode pblob->NegotiateFlags; some may be useful */
568 /* In particular we can examine sign flags */
569 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
570 we must set the MIC field of the AUTHENTICATE_MESSAGE */
571 ses->ntlmssp->server_flags = le32_to_cpu(pblob->NegotiateFlags);
572 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
573 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
574 if (tioffset > blob_len || tioffset + tilen > blob_len) {
575 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
580 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
582 if (!ses->auth_key.response) {
583 cifs_dbg(VFS, "Challenge target info alloc failure\n");
586 ses->auth_key.len = tilen;
592 /* BB Move to ntlmssp.c eventually */
594 /* We do not malloc the blob, it is passed in pbuffer, because
595 it is fixed size, and small, making this approach cleaner */
596 void build_ntlmssp_negotiate_blob(unsigned char *pbuffer,
597 struct cifs_ses *ses)
599 struct TCP_Server_Info *server = cifs_ses_server(ses);
600 NEGOTIATE_MESSAGE *sec_blob = (NEGOTIATE_MESSAGE *)pbuffer;
603 memset(pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
604 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
605 sec_blob->MessageType = NtLmNegotiate;
607 /* BB is NTLMV2 session security format easier to use here? */
608 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
609 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
610 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
611 NTLMSSP_NEGOTIATE_SEAL;
613 flags |= NTLMSSP_NEGOTIATE_SIGN;
614 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
615 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
617 sec_blob->NegotiateFlags = cpu_to_le32(flags);
619 sec_blob->WorkstationName.BufferOffset = 0;
620 sec_blob->WorkstationName.Length = 0;
621 sec_blob->WorkstationName.MaximumLength = 0;
623 /* Domain name is sent on the Challenge not Negotiate NTLMSSP request */
624 sec_blob->DomainName.BufferOffset = 0;
625 sec_blob->DomainName.Length = 0;
626 sec_blob->DomainName.MaximumLength = 0;
629 static int size_of_ntlmssp_blob(struct cifs_ses *ses)
631 int sz = sizeof(AUTHENTICATE_MESSAGE) + ses->auth_key.len
632 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
635 sz += 2 * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
640 sz += 2 * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
647 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
649 struct cifs_ses *ses,
650 const struct nls_table *nls_cp)
653 AUTHENTICATE_MESSAGE *sec_blob;
657 rc = setup_ntlmv2_rsp(ses, nls_cp);
659 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
661 goto setup_ntlmv2_ret;
663 *pbuffer = kmalloc(size_of_ntlmssp_blob(ses), GFP_KERNEL);
666 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
668 goto setup_ntlmv2_ret;
670 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
672 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
673 sec_blob->MessageType = NtLmAuthenticate;
675 flags = NTLMSSP_NEGOTIATE_56 |
676 NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_TARGET_INFO |
677 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
678 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
679 NTLMSSP_NEGOTIATE_SEAL;
680 if (ses->server->sign)
681 flags |= NTLMSSP_NEGOTIATE_SIGN;
682 if (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
683 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
685 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
686 sec_blob->NegotiateFlags = cpu_to_le32(flags);
688 sec_blob->LmChallengeResponse.BufferOffset =
689 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
690 sec_blob->LmChallengeResponse.Length = 0;
691 sec_blob->LmChallengeResponse.MaximumLength = 0;
693 sec_blob->NtChallengeResponse.BufferOffset =
694 cpu_to_le32(tmp - *pbuffer);
695 if (ses->user_name != NULL) {
696 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
697 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
698 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
700 sec_blob->NtChallengeResponse.Length =
701 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
702 sec_blob->NtChallengeResponse.MaximumLength =
703 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
706 * don't send an NT Response for anonymous access
708 sec_blob->NtChallengeResponse.Length = 0;
709 sec_blob->NtChallengeResponse.MaximumLength = 0;
712 if (ses->domainName == NULL) {
713 sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
714 sec_blob->DomainName.Length = 0;
715 sec_blob->DomainName.MaximumLength = 0;
719 len = cifs_strtoUTF16((__le16 *)tmp, ses->domainName,
720 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
721 len *= 2; /* unicode is 2 bytes each */
722 sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
723 sec_blob->DomainName.Length = cpu_to_le16(len);
724 sec_blob->DomainName.MaximumLength = cpu_to_le16(len);
728 if (ses->user_name == NULL) {
729 sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
730 sec_blob->UserName.Length = 0;
731 sec_blob->UserName.MaximumLength = 0;
735 len = cifs_strtoUTF16((__le16 *)tmp, ses->user_name,
736 CIFS_MAX_USERNAME_LEN, nls_cp);
737 len *= 2; /* unicode is 2 bytes each */
738 sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
739 sec_blob->UserName.Length = cpu_to_le16(len);
740 sec_blob->UserName.MaximumLength = cpu_to_le16(len);
744 sec_blob->WorkstationName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
745 sec_blob->WorkstationName.Length = 0;
746 sec_blob->WorkstationName.MaximumLength = 0;
749 if (((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) ||
750 (ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC))
751 && !calc_seckey(ses)) {
752 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
753 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
754 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
755 sec_blob->SessionKey.MaximumLength =
756 cpu_to_le16(CIFS_CPHTXT_SIZE);
757 tmp += CIFS_CPHTXT_SIZE;
759 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
760 sec_blob->SessionKey.Length = 0;
761 sec_blob->SessionKey.MaximumLength = 0;
764 *buflen = tmp - *pbuffer;
770 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
772 switch (server->negflavor) {
773 case CIFS_NEGFLAVOR_EXTENDED:
779 if (server->sec_ntlmssp &&
780 (global_secflags & CIFSSEC_MAY_NTLMSSP))
782 if ((server->sec_kerberos || server->sec_mskerberos) &&
783 (global_secflags & CIFSSEC_MAY_KRB5))
789 case CIFS_NEGFLAVOR_UNENCAP:
795 if (global_secflags & CIFSSEC_MAY_NTLMV2)
797 if (global_secflags & CIFSSEC_MAY_NTLM)
802 /* Fallthrough - to attempt LANMAN authentication next */
803 case CIFS_NEGFLAVOR_LANMAN:
808 if (global_secflags & CIFSSEC_MAY_LANMAN)
821 struct cifs_ses *ses;
822 struct nls_table *nls_cp;
823 void (*func)(struct sess_data *);
826 /* we will send the SMB in three pieces:
827 * a fixed length beginning part, an optional
828 * SPNEGO blob (which can be zero length), and a
829 * last part which will include the strings
830 * and rest of bcc area. This allows us to avoid
831 * a large buffer 17K allocation
838 sess_alloc_buffer(struct sess_data *sess_data, int wct)
841 struct cifs_ses *ses = sess_data->ses;
842 struct smb_hdr *smb_buf;
844 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
850 sess_data->iov[0].iov_base = (char *)smb_buf;
851 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
853 * This variable will be used to clear the buffer
854 * allocated above in case of any error in the calling function.
856 sess_data->buf0_type = CIFS_SMALL_BUFFER;
858 /* 2000 big enough to fit max user, domain, NOS name etc. */
859 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
860 if (!sess_data->iov[2].iov_base) {
862 goto out_free_smb_buf;
869 sess_data->iov[0].iov_base = NULL;
870 sess_data->iov[0].iov_len = 0;
871 sess_data->buf0_type = CIFS_NO_BUFFER;
876 sess_free_buffer(struct sess_data *sess_data)
879 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
880 sess_data->buf0_type = CIFS_NO_BUFFER;
881 kfree(sess_data->iov[2].iov_base);
885 sess_establish_session(struct sess_data *sess_data)
887 struct cifs_ses *ses = sess_data->ses;
889 mutex_lock(&ses->server->srv_mutex);
890 if (!ses->server->session_estab) {
891 if (ses->server->sign) {
892 ses->server->session_key.response =
893 kmemdup(ses->auth_key.response,
894 ses->auth_key.len, GFP_KERNEL);
895 if (!ses->server->session_key.response) {
896 mutex_unlock(&ses->server->srv_mutex);
899 ses->server->session_key.len =
902 ses->server->sequence_number = 0x2;
903 ses->server->session_estab = true;
905 mutex_unlock(&ses->server->srv_mutex);
907 cifs_dbg(FYI, "CIFS session established successfully\n");
908 spin_lock(&GlobalMid_Lock);
909 ses->status = CifsGood;
910 ses->need_reconnect = false;
911 spin_unlock(&GlobalMid_Lock);
917 sess_sendreceive(struct sess_data *sess_data)
920 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
922 struct kvec rsp_iov = { NULL, 0 };
924 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
925 smb_buf->smb_buf_length =
926 cpu_to_be32(be32_to_cpu(smb_buf->smb_buf_length) + count);
927 put_bcc(count, smb_buf);
929 rc = SendReceive2(sess_data->xid, sess_data->ses,
930 sess_data->iov, 3 /* num_iovecs */,
931 &sess_data->buf0_type,
932 CIFS_LOG_ERROR, &rsp_iov);
933 cifs_small_buf_release(sess_data->iov[0].iov_base);
934 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
940 * LANMAN and plaintext are less secure and off by default.
941 * So we make this explicitly be turned on in kconfig (in the
942 * build) and turned on at runtime (changed from the default)
943 * in proc/fs/cifs or via mount parm. Unfortunately this is
944 * needed for old Win (e.g. Win95), some obscure NAS and OS/2
946 #ifdef CONFIG_CIFS_WEAK_PW_HASH
948 sess_auth_lanman(struct sess_data *sess_data)
951 struct smb_hdr *smb_buf;
952 SESSION_SETUP_ANDX *pSMB;
954 struct cifs_ses *ses = sess_data->ses;
955 char lnm_session_key[CIFS_AUTH_RESP_SIZE];
956 __u16 bytes_remaining;
958 /* lanman 2 style sessionsetup */
960 rc = sess_alloc_buffer(sess_data, 10);
964 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
965 bcc_ptr = sess_data->iov[2].iov_base;
966 (void)cifs_ssetup_hdr(ses, pSMB);
968 pSMB->req.hdr.Flags2 &= ~SMBFLG2_UNICODE;
970 if (ses->user_name != NULL) {
971 /* no capabilities flags in old lanman negotiation */
972 pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
974 /* Calculate hash with password and copy into bcc_ptr.
975 * Encryption Key (stored as in cryptkey) gets used if the
976 * security mode bit in Negotiate Protocol response states
977 * to use challenge/response method (i.e. Password bit is 1).
979 rc = calc_lanman_hash(ses->password, ses->server->cryptkey,
980 ses->server->sec_mode & SECMODE_PW_ENCRYPT ?
981 true : false, lnm_session_key);
985 memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_AUTH_RESP_SIZE);
986 bcc_ptr += CIFS_AUTH_RESP_SIZE;
988 pSMB->old_req.PasswordLength = 0;
992 * can not sign if LANMAN negotiated so no need
993 * to calculate signing key? but what if server
994 * changed to do higher than lanman dialect and
995 * we reconnected would we ever calc signing_key?
998 cifs_dbg(FYI, "Negotiating LANMAN setting up strings\n");
999 /* Unicode not allowed for LANMAN dialects */
1000 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1002 sess_data->iov[2].iov_len = (long) bcc_ptr -
1003 (long) sess_data->iov[2].iov_base;
1005 rc = sess_sendreceive(sess_data);
1009 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1010 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1012 /* lanman response has a word count of 3 */
1013 if (smb_buf->WordCount != 3) {
1015 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1019 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1020 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1022 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1023 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1025 bytes_remaining = get_bcc(smb_buf);
1026 bcc_ptr = pByteArea(smb_buf);
1028 /* BB check if Unicode and decode strings */
1029 if (bytes_remaining == 0) {
1030 /* no string area to decode, do nothing */
1031 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1032 /* unicode string area must be word-aligned */
1033 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1037 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1040 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1044 rc = sess_establish_session(sess_data);
1046 sess_data->result = rc;
1047 sess_data->func = NULL;
1048 sess_free_buffer(sess_data);
1054 sess_auth_ntlm(struct sess_data *sess_data)
1057 struct smb_hdr *smb_buf;
1058 SESSION_SETUP_ANDX *pSMB;
1060 struct cifs_ses *ses = sess_data->ses;
1062 __u16 bytes_remaining;
1064 /* old style NTLM sessionsetup */
1066 rc = sess_alloc_buffer(sess_data, 13);
1070 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1071 bcc_ptr = sess_data->iov[2].iov_base;
1072 capabilities = cifs_ssetup_hdr(ses, pSMB);
1074 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1075 if (ses->user_name != NULL) {
1076 pSMB->req_no_secext.CaseInsensitivePasswordLength =
1077 cpu_to_le16(CIFS_AUTH_RESP_SIZE);
1078 pSMB->req_no_secext.CaseSensitivePasswordLength =
1079 cpu_to_le16(CIFS_AUTH_RESP_SIZE);
1081 /* calculate ntlm response and session key */
1082 rc = setup_ntlm_response(ses, sess_data->nls_cp);
1084 cifs_dbg(VFS, "Error %d during NTLM authentication\n",
1089 /* copy ntlm response */
1090 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1091 CIFS_AUTH_RESP_SIZE);
1092 bcc_ptr += CIFS_AUTH_RESP_SIZE;
1093 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1094 CIFS_AUTH_RESP_SIZE);
1095 bcc_ptr += CIFS_AUTH_RESP_SIZE;
1097 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1098 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1101 if (ses->capabilities & CAP_UNICODE) {
1102 /* unicode strings must be word aligned */
1103 if (sess_data->iov[0].iov_len % 2) {
1107 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1109 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1113 sess_data->iov[2].iov_len = (long) bcc_ptr -
1114 (long) sess_data->iov[2].iov_base;
1116 rc = sess_sendreceive(sess_data);
1120 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1121 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1123 if (smb_buf->WordCount != 3) {
1125 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1129 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1130 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1132 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1133 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1135 bytes_remaining = get_bcc(smb_buf);
1136 bcc_ptr = pByteArea(smb_buf);
1138 /* BB check if Unicode and decode strings */
1139 if (bytes_remaining == 0) {
1140 /* no string area to decode, do nothing */
1141 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1142 /* unicode string area must be word-aligned */
1143 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1147 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1150 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1154 rc = sess_establish_session(sess_data);
1156 sess_data->result = rc;
1157 sess_data->func = NULL;
1158 sess_free_buffer(sess_data);
1159 kfree(ses->auth_key.response);
1160 ses->auth_key.response = NULL;
1164 sess_auth_ntlmv2(struct sess_data *sess_data)
1167 struct smb_hdr *smb_buf;
1168 SESSION_SETUP_ANDX *pSMB;
1170 struct cifs_ses *ses = sess_data->ses;
1172 __u16 bytes_remaining;
1174 /* old style NTLM sessionsetup */
1176 rc = sess_alloc_buffer(sess_data, 13);
1180 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1181 bcc_ptr = sess_data->iov[2].iov_base;
1182 capabilities = cifs_ssetup_hdr(ses, pSMB);
1184 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1186 /* LM2 password would be here if we supported it */
1187 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1189 if (ses->user_name != NULL) {
1190 /* calculate nlmv2 response and session key */
1191 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1193 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1197 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1198 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1199 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1201 /* set case sensitive password length after tilen may get
1202 * assigned, tilen is 0 otherwise.
1204 pSMB->req_no_secext.CaseSensitivePasswordLength =
1205 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1207 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1210 if (ses->capabilities & CAP_UNICODE) {
1211 if (sess_data->iov[0].iov_len % 2) {
1215 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1217 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1221 sess_data->iov[2].iov_len = (long) bcc_ptr -
1222 (long) sess_data->iov[2].iov_base;
1224 rc = sess_sendreceive(sess_data);
1228 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1229 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1231 if (smb_buf->WordCount != 3) {
1233 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1237 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1238 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1240 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1241 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1243 bytes_remaining = get_bcc(smb_buf);
1244 bcc_ptr = pByteArea(smb_buf);
1246 /* BB check if Unicode and decode strings */
1247 if (bytes_remaining == 0) {
1248 /* no string area to decode, do nothing */
1249 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1250 /* unicode string area must be word-aligned */
1251 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1255 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1258 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1262 rc = sess_establish_session(sess_data);
1264 sess_data->result = rc;
1265 sess_data->func = NULL;
1266 sess_free_buffer(sess_data);
1267 kfree(ses->auth_key.response);
1268 ses->auth_key.response = NULL;
1271 #ifdef CONFIG_CIFS_UPCALL
1273 sess_auth_kerberos(struct sess_data *sess_data)
1276 struct smb_hdr *smb_buf;
1277 SESSION_SETUP_ANDX *pSMB;
1279 struct cifs_ses *ses = sess_data->ses;
1281 __u16 bytes_remaining;
1282 struct key *spnego_key = NULL;
1283 struct cifs_spnego_msg *msg;
1286 /* extended security */
1288 rc = sess_alloc_buffer(sess_data, 12);
1292 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1293 bcc_ptr = sess_data->iov[2].iov_base;
1294 capabilities = cifs_ssetup_hdr(ses, pSMB);
1296 spnego_key = cifs_get_spnego_key(ses);
1297 if (IS_ERR(spnego_key)) {
1298 rc = PTR_ERR(spnego_key);
1303 msg = spnego_key->payload.data[0];
1305 * check version field to make sure that cifs.upcall is
1306 * sending us a response in an expected form
1308 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1309 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1310 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1312 goto out_put_spnego_key;
1315 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1317 if (!ses->auth_key.response) {
1318 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1321 goto out_put_spnego_key;
1323 ses->auth_key.len = msg->sesskey_len;
1325 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1326 capabilities |= CAP_EXTENDED_SECURITY;
1327 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1328 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1329 sess_data->iov[1].iov_len = msg->secblob_len;
1330 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1332 if (ses->capabilities & CAP_UNICODE) {
1333 /* unicode strings must be word aligned */
1334 if ((sess_data->iov[0].iov_len
1335 + sess_data->iov[1].iov_len) % 2) {
1339 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1340 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1342 /* BB: is this right? */
1343 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1346 sess_data->iov[2].iov_len = (long) bcc_ptr -
1347 (long) sess_data->iov[2].iov_base;
1349 rc = sess_sendreceive(sess_data);
1351 goto out_put_spnego_key;
1353 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1354 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1356 if (smb_buf->WordCount != 4) {
1358 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1359 goto out_put_spnego_key;
1362 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1363 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1365 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1366 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1368 bytes_remaining = get_bcc(smb_buf);
1369 bcc_ptr = pByteArea(smb_buf);
1371 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1372 if (blob_len > bytes_remaining) {
1373 cifs_dbg(VFS, "bad security blob length %d\n",
1376 goto out_put_spnego_key;
1378 bcc_ptr += blob_len;
1379 bytes_remaining -= blob_len;
1381 /* BB check if Unicode and decode strings */
1382 if (bytes_remaining == 0) {
1383 /* no string area to decode, do nothing */
1384 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1385 /* unicode string area must be word-aligned */
1386 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1390 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1393 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1397 rc = sess_establish_session(sess_data);
1399 key_invalidate(spnego_key);
1400 key_put(spnego_key);
1402 sess_data->result = rc;
1403 sess_data->func = NULL;
1404 sess_free_buffer(sess_data);
1405 kfree(ses->auth_key.response);
1406 ses->auth_key.response = NULL;
1409 #endif /* ! CONFIG_CIFS_UPCALL */
1412 * The required kvec buffers have to be allocated before calling this
1416 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1418 SESSION_SETUP_ANDX *pSMB;
1419 struct cifs_ses *ses = sess_data->ses;
1423 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1425 capabilities = cifs_ssetup_hdr(ses, pSMB);
1426 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1427 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1431 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1432 capabilities |= CAP_EXTENDED_SECURITY;
1433 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1435 bcc_ptr = sess_data->iov[2].iov_base;
1436 /* unicode strings must be word aligned */
1437 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1441 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1443 sess_data->iov[2].iov_len = (long) bcc_ptr -
1444 (long) sess_data->iov[2].iov_base;
1450 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1453 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1456 struct smb_hdr *smb_buf;
1457 SESSION_SETUP_ANDX *pSMB;
1458 struct cifs_ses *ses = sess_data->ses;
1459 __u16 bytes_remaining;
1463 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1466 * if memory allocation is successful, caller of this function
1469 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1470 if (!ses->ntlmssp) {
1474 ses->ntlmssp->sesskey_per_smbsess = false;
1477 rc = sess_alloc_buffer(sess_data, 12);
1481 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1483 /* Build security blob before we assemble the request */
1484 build_ntlmssp_negotiate_blob(pSMB->req.SecurityBlob, ses);
1485 sess_data->iov[1].iov_len = sizeof(NEGOTIATE_MESSAGE);
1486 sess_data->iov[1].iov_base = pSMB->req.SecurityBlob;
1487 pSMB->req.SecurityBlobLength = cpu_to_le16(sizeof(NEGOTIATE_MESSAGE));
1489 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1493 rc = sess_sendreceive(sess_data);
1495 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1496 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1498 /* If true, rc here is expected and not an error */
1499 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1500 smb_buf->Status.CifsError ==
1501 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1507 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1509 if (smb_buf->WordCount != 4) {
1511 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1515 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1516 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1518 bytes_remaining = get_bcc(smb_buf);
1519 bcc_ptr = pByteArea(smb_buf);
1521 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1522 if (blob_len > bytes_remaining) {
1523 cifs_dbg(VFS, "bad security blob length %d\n",
1529 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1531 sess_free_buffer(sess_data);
1534 sess_data->func = sess_auth_rawntlmssp_authenticate;
1538 /* Else error. Cleanup */
1539 kfree(ses->auth_key.response);
1540 ses->auth_key.response = NULL;
1541 kfree(ses->ntlmssp);
1542 ses->ntlmssp = NULL;
1544 sess_data->func = NULL;
1545 sess_data->result = rc;
1549 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1552 struct smb_hdr *smb_buf;
1553 SESSION_SETUP_ANDX *pSMB;
1554 struct cifs_ses *ses = sess_data->ses;
1555 __u16 bytes_remaining;
1557 unsigned char *ntlmsspblob = NULL;
1560 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1563 rc = sess_alloc_buffer(sess_data, 12);
1567 /* Build security blob before we assemble the request */
1568 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1569 smb_buf = (struct smb_hdr *)pSMB;
1570 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1571 &blob_len, ses, sess_data->nls_cp);
1573 goto out_free_ntlmsspblob;
1574 sess_data->iov[1].iov_len = blob_len;
1575 sess_data->iov[1].iov_base = ntlmsspblob;
1576 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1578 * Make sure that we tell the server that we are using
1579 * the uid that it just gave us back on the response
1582 smb_buf->Uid = ses->Suid;
1584 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1586 goto out_free_ntlmsspblob;
1588 rc = sess_sendreceive(sess_data);
1590 goto out_free_ntlmsspblob;
1592 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1593 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1594 if (smb_buf->WordCount != 4) {
1596 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1597 goto out_free_ntlmsspblob;
1600 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1601 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1603 if (ses->Suid != smb_buf->Uid) {
1604 ses->Suid = smb_buf->Uid;
1605 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1608 bytes_remaining = get_bcc(smb_buf);
1609 bcc_ptr = pByteArea(smb_buf);
1610 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1611 if (blob_len > bytes_remaining) {
1612 cifs_dbg(VFS, "bad security blob length %d\n",
1615 goto out_free_ntlmsspblob;
1617 bcc_ptr += blob_len;
1618 bytes_remaining -= blob_len;
1621 /* BB check if Unicode and decode strings */
1622 if (bytes_remaining == 0) {
1623 /* no string area to decode, do nothing */
1624 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1625 /* unicode string area must be word-aligned */
1626 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1630 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1633 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1637 out_free_ntlmsspblob:
1640 sess_free_buffer(sess_data);
1643 rc = sess_establish_session(sess_data);
1646 kfree(ses->auth_key.response);
1647 ses->auth_key.response = NULL;
1648 kfree(ses->ntlmssp);
1649 ses->ntlmssp = NULL;
1651 sess_data->func = NULL;
1652 sess_data->result = rc;
1655 static int select_sec(struct cifs_ses *ses, struct sess_data *sess_data)
1659 type = cifs_select_sectype(ses->server, ses->sectype);
1660 cifs_dbg(FYI, "sess setup type %d\n", type);
1661 if (type == Unspecified) {
1662 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1668 /* LANMAN and plaintext are less secure and off by default.
1669 * So we make this explicitly be turned on in kconfig (in the
1670 * build) and turned on at runtime (changed from the default)
1671 * in proc/fs/cifs or via mount parm. Unfortunately this is
1672 * needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
1673 #ifdef CONFIG_CIFS_WEAK_PW_HASH
1674 sess_data->func = sess_auth_lanman;
1680 sess_data->func = sess_auth_ntlm;
1683 sess_data->func = sess_auth_ntlmv2;
1686 #ifdef CONFIG_CIFS_UPCALL
1687 sess_data->func = sess_auth_kerberos;
1690 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1693 #endif /* CONFIG_CIFS_UPCALL */
1695 sess_data->func = sess_auth_rawntlmssp_negotiate;
1698 cifs_dbg(VFS, "secType %d not supported!\n", type);
1705 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1706 const struct nls_table *nls_cp)
1709 struct sess_data *sess_data;
1712 WARN(1, "%s: ses == NULL!", __func__);
1716 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1720 rc = select_sec(ses, sess_data);
1724 sess_data->xid = xid;
1725 sess_data->ses = ses;
1726 sess_data->buf0_type = CIFS_NO_BUFFER;
1727 sess_data->nls_cp = (struct nls_table *) nls_cp;
1729 while (sess_data->func)
1730 sess_data->func(sess_data);
1732 /* Store result before we free sess_data */
1733 rc = sess_data->result;