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"
35 #include "fs_context.h"
38 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
39 struct cifs_server_iface *iface);
42 is_server_using_iface(struct TCP_Server_Info *server,
43 struct cifs_server_iface *iface)
45 struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr;
46 struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr;
47 struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr;
48 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr;
50 if (server->dstaddr.ss_family != iface->sockaddr.ss_family)
52 if (server->dstaddr.ss_family == AF_INET) {
53 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
55 } else if (server->dstaddr.ss_family == AF_INET6) {
56 if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
57 sizeof(i6->sin6_addr)) != 0)
60 /* unknown family.. */
66 bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
70 for (i = 0; i < ses->chan_count; i++) {
71 if (is_server_using_iface(ses->chans[i].server, iface))
77 /* returns number of channels added */
78 int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
80 int old_chan_count = ses->chan_count;
81 int left = ses->chan_max - ses->chan_count;
85 struct cifs_server_iface *ifaces = NULL;
90 "ses already at max_channels (%zu), nothing to open\n",
95 if (ses->server->dialect < SMB30_PROT_ID) {
96 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
100 if (!(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
101 cifs_dbg(VFS, "server %s does not support multichannel\n", ses->server->hostname);
107 * Make a copy of the iface list at the time and use that
108 * instead so as to not hold the iface spinlock for opening
111 spin_lock(&ses->iface_lock);
112 iface_count = ses->iface_count;
113 if (iface_count <= 0) {
114 spin_unlock(&ses->iface_lock);
115 cifs_dbg(VFS, "no iface list available to open channels\n");
118 ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces),
121 spin_unlock(&ses->iface_lock);
124 spin_unlock(&ses->iface_lock);
127 * Keep connecting to same, fastest, iface for all channels as
128 * long as its RSS. Try next fastest one if not RSS or channel
132 struct cifs_server_iface *iface;
135 if (tries > 3*ses->chan_max) {
136 cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
142 if (is_ses_using_iface(ses, iface) && !iface->rss_capable) {
143 i = (i+1) % iface_count;
147 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
149 cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n",
151 i = (i+1) % iface_count;
155 cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
161 return ses->chan_count - old_chan_count;
165 * If server is a channel of ses, return the corresponding enclosing
166 * cifs_chan otherwise return NULL.
169 cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
173 for (i = 0; i < ses->chan_count; i++) {
174 if (ses->chans[i].server == server)
175 return &ses->chans[i];
181 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
182 struct cifs_server_iface *iface)
184 struct cifs_chan *chan;
185 struct smb3_fs_context ctx = {NULL};
186 static const char unc_fmt[] = "\\%s\\foo";
187 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
188 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
189 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
191 unsigned int xid = get_xid();
193 if (iface->sockaddr.ss_family == AF_INET)
194 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
195 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
198 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
199 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
203 * Setup a ctx with mostly the same info as the existing
204 * session and overwrite it with the requested iface data.
206 * We need to setup at least the fields used for negprot and
209 * We only need the ctx here, so we can reuse memory from
210 * the session and server without caring about memory
214 /* Always make new connection for now (TODO?) */
215 ctx.nosharesock = true;
218 ctx.domainauto = ses->domainAuto;
219 ctx.domainname = ses->domainName;
220 ctx.username = ses->user_name;
221 ctx.password = ses->password;
222 ctx.sectype = ses->sectype;
223 ctx.sign = ses->sign;
226 /* XXX: Use ses->server->hostname? */
227 sprintf(unc, unc_fmt, ses->ip_addr);
231 /* Reuse same version as master connection */
232 ctx.vals = ses->server->vals;
233 ctx.ops = ses->server->ops;
235 ctx.noblocksnd = ses->server->noblocksnd;
236 ctx.noautotune = ses->server->noautotune;
237 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
238 ctx.echo_interval = ses->server->echo_interval / HZ;
239 ctx.max_credits = ses->server->max_credits;
242 * This will be used for encoding/decoding user/domain/pw
243 * during sess setup auth.
245 ctx.local_nls = cifs_sb->local_nls;
247 /* Use RDMA if possible */
248 ctx.rdma = iface->rdma_capable;
249 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
251 /* reuse master con client guid */
252 memcpy(&ctx.client_guid, ses->server->client_guid,
253 SMB2_CLIENT_GUID_SIZE);
254 ctx.use_client_guid = true;
256 mutex_lock(&ses->session_mutex);
258 chan = ses->binding_chan = &ses->chans[ses->chan_count];
259 chan->server = cifs_get_tcp_session(&ctx);
260 if (IS_ERR(chan->server)) {
261 rc = PTR_ERR(chan->server);
265 spin_lock(&cifs_tcp_ses_lock);
266 chan->server->is_channel = true;
267 spin_unlock(&cifs_tcp_ses_lock);
270 * We need to allocate the server crypto now as we will need
271 * to sign packets before we generate the channel signing key
272 * (we sign with the session key)
274 rc = smb311_crypto_shash_allocate(chan->server);
276 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
281 rc = cifs_negotiate_protocol(xid, ses);
285 rc = cifs_setup_session(xid, ses, cifs_sb->local_nls);
289 /* success, put it on the list
290 * XXX: sharing ses between 2 tcp servers is not possible, the
291 * way "internal" linked lists works in linux makes element
292 * only able to belong to one list
294 * the binding session is already established so the rest of
295 * the code should be able to look it up, no need to add the
296 * ses to the new server.
300 atomic_set(&ses->chan_seq, 0);
302 ses->binding = false;
303 ses->binding_chan = NULL;
304 mutex_unlock(&ses->session_mutex);
306 if (rc && chan->server)
307 cifs_put_tcp_session(chan->server, 0);
312 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses, SESSION_SETUP_ANDX *pSMB)
314 __u32 capabilities = 0;
316 /* init fields common to all four types of SessSetup */
317 /* Note that offsets for first seven fields in req struct are same */
318 /* in CIFS Specs so does not matter which of 3 forms of struct */
319 /* that we use in next few lines */
320 /* Note that header is initialized to zero in header_assemble */
321 pSMB->req.AndXCommand = 0xFF;
322 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
323 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
325 pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
326 pSMB->req.VcNumber = cpu_to_le16(1);
328 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
330 /* BB verify whether signing required on neg or just on auth frame
333 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
334 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
336 if (ses->server->sign)
337 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
339 if (ses->capabilities & CAP_UNICODE) {
340 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
341 capabilities |= CAP_UNICODE;
343 if (ses->capabilities & CAP_STATUS32) {
344 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
345 capabilities |= CAP_STATUS32;
347 if (ses->capabilities & CAP_DFS) {
348 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
349 capabilities |= CAP_DFS;
351 if (ses->capabilities & CAP_UNIX)
352 capabilities |= CAP_UNIX;
358 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
360 char *bcc_ptr = *pbcc_area;
363 /* Copy OS version */
364 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
366 bcc_ptr += 2 * bytes_ret;
367 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
369 bcc_ptr += 2 * bytes_ret;
370 bcc_ptr += 2; /* trailing null */
372 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
374 bcc_ptr += 2 * bytes_ret;
375 bcc_ptr += 2; /* trailing null */
377 *pbcc_area = bcc_ptr;
380 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
381 const struct nls_table *nls_cp)
383 char *bcc_ptr = *pbcc_area;
387 if (ses->domainName == NULL) {
388 /* Sending null domain better than using a bogus domain name (as
389 we did briefly in 2.6.18) since server will use its default */
394 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
395 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
396 bcc_ptr += 2 * bytes_ret;
397 bcc_ptr += 2; /* account for null terminator */
399 *pbcc_area = bcc_ptr;
403 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
404 const struct nls_table *nls_cp)
406 char *bcc_ptr = *pbcc_area;
409 /* BB FIXME add check that strings total less
410 than 335 or will need to send them as arrays */
412 /* unicode strings, must be word aligned before the call */
413 /* if ((long) bcc_ptr % 2) {
418 if (ses->user_name == NULL) {
419 /* null user mount */
423 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
424 CIFS_MAX_USERNAME_LEN, nls_cp);
426 bcc_ptr += 2 * bytes_ret;
427 bcc_ptr += 2; /* account for null termination */
429 unicode_domain_string(&bcc_ptr, ses, nls_cp);
430 unicode_oslm_strings(&bcc_ptr, nls_cp);
432 *pbcc_area = bcc_ptr;
435 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
436 const struct nls_table *nls_cp)
438 char *bcc_ptr = *pbcc_area;
442 /* BB what about null user mounts - check that we do this BB */
444 if (ses->user_name != NULL) {
445 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
446 if (WARN_ON_ONCE(len < 0))
447 len = CIFS_MAX_USERNAME_LEN - 1;
450 /* else null user mount */
452 bcc_ptr++; /* account for null termination */
455 if (ses->domainName != NULL) {
456 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
457 if (WARN_ON_ONCE(len < 0))
458 len = CIFS_MAX_DOMAINNAME_LEN - 1;
460 } /* else we will send a null domain name
461 so the server will default to its own domain */
465 /* BB check for overflow here */
467 strcpy(bcc_ptr, "Linux version ");
468 bcc_ptr += strlen("Linux version ");
469 strcpy(bcc_ptr, init_utsname()->release);
470 bcc_ptr += strlen(init_utsname()->release) + 1;
472 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
473 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
475 *pbcc_area = bcc_ptr;
479 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
480 const struct nls_table *nls_cp)
483 char *data = *pbcc_area;
485 cifs_dbg(FYI, "bleft %d\n", bleft);
487 kfree(ses->serverOS);
488 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
489 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
490 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
496 kfree(ses->serverNOS);
497 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
498 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
499 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
505 kfree(ses->serverDomain);
506 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
507 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
512 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
513 struct cifs_ses *ses,
514 const struct nls_table *nls_cp)
517 char *bcc_ptr = *pbcc_area;
519 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
521 len = strnlen(bcc_ptr, bleft);
525 kfree(ses->serverOS);
527 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
529 memcpy(ses->serverOS, bcc_ptr, len);
530 ses->serverOS[len] = 0;
531 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
532 cifs_dbg(FYI, "OS/2 server\n");
538 len = strnlen(bcc_ptr, bleft);
542 kfree(ses->serverNOS);
544 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
545 if (ses->serverNOS) {
546 memcpy(ses->serverNOS, bcc_ptr, len);
547 ses->serverNOS[len] = 0;
553 len = strnlen(bcc_ptr, bleft);
557 /* No domain field in LANMAN case. Domain is
558 returned by old servers in the SMB negprot response */
559 /* BB For newer servers which do not support Unicode,
560 but thus do return domain here we could add parsing
561 for it later, but it is not very important */
562 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
565 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
566 struct cifs_ses *ses)
568 unsigned int tioffset; /* challenge message target info area */
569 unsigned int tilen; /* challenge message target info area length */
571 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
573 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
574 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
578 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
579 cifs_dbg(VFS, "blob signature incorrect %s\n",
583 if (pblob->MessageType != NtLmChallenge) {
584 cifs_dbg(VFS, "Incorrect message type %d\n",
589 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
590 /* BB we could decode pblob->NegotiateFlags; some may be useful */
591 /* In particular we can examine sign flags */
592 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
593 we must set the MIC field of the AUTHENTICATE_MESSAGE */
594 ses->ntlmssp->server_flags = le32_to_cpu(pblob->NegotiateFlags);
595 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
596 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
597 if (tioffset > blob_len || tioffset + tilen > blob_len) {
598 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
603 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
605 if (!ses->auth_key.response) {
606 cifs_dbg(VFS, "Challenge target info alloc failure\n");
609 ses->auth_key.len = tilen;
615 /* BB Move to ntlmssp.c eventually */
617 /* We do not malloc the blob, it is passed in pbuffer, because
618 it is fixed size, and small, making this approach cleaner */
619 void build_ntlmssp_negotiate_blob(unsigned char *pbuffer,
620 struct cifs_ses *ses)
622 struct TCP_Server_Info *server = cifs_ses_server(ses);
623 NEGOTIATE_MESSAGE *sec_blob = (NEGOTIATE_MESSAGE *)pbuffer;
626 memset(pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
627 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
628 sec_blob->MessageType = NtLmNegotiate;
630 /* BB is NTLMV2 session security format easier to use here? */
631 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
632 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
633 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
634 NTLMSSP_NEGOTIATE_SEAL;
636 flags |= NTLMSSP_NEGOTIATE_SIGN;
637 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
638 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
640 sec_blob->NegotiateFlags = cpu_to_le32(flags);
642 sec_blob->WorkstationName.BufferOffset = 0;
643 sec_blob->WorkstationName.Length = 0;
644 sec_blob->WorkstationName.MaximumLength = 0;
646 /* Domain name is sent on the Challenge not Negotiate NTLMSSP request */
647 sec_blob->DomainName.BufferOffset = 0;
648 sec_blob->DomainName.Length = 0;
649 sec_blob->DomainName.MaximumLength = 0;
652 static int size_of_ntlmssp_blob(struct cifs_ses *ses)
654 int sz = sizeof(AUTHENTICATE_MESSAGE) + ses->auth_key.len
655 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
658 sz += 2 * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
663 sz += 2 * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
670 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
672 struct cifs_ses *ses,
673 const struct nls_table *nls_cp)
676 AUTHENTICATE_MESSAGE *sec_blob;
680 rc = setup_ntlmv2_rsp(ses, nls_cp);
682 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
684 goto setup_ntlmv2_ret;
686 *pbuffer = kmalloc(size_of_ntlmssp_blob(ses), GFP_KERNEL);
689 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
691 goto setup_ntlmv2_ret;
693 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
695 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
696 sec_blob->MessageType = NtLmAuthenticate;
698 flags = NTLMSSP_NEGOTIATE_56 |
699 NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_TARGET_INFO |
700 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
701 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
702 NTLMSSP_NEGOTIATE_SEAL;
703 if (ses->server->sign)
704 flags |= NTLMSSP_NEGOTIATE_SIGN;
705 if (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
706 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
708 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
709 sec_blob->NegotiateFlags = cpu_to_le32(flags);
711 sec_blob->LmChallengeResponse.BufferOffset =
712 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
713 sec_blob->LmChallengeResponse.Length = 0;
714 sec_blob->LmChallengeResponse.MaximumLength = 0;
716 sec_blob->NtChallengeResponse.BufferOffset =
717 cpu_to_le32(tmp - *pbuffer);
718 if (ses->user_name != NULL) {
719 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
720 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
721 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
723 sec_blob->NtChallengeResponse.Length =
724 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
725 sec_blob->NtChallengeResponse.MaximumLength =
726 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
729 * don't send an NT Response for anonymous access
731 sec_blob->NtChallengeResponse.Length = 0;
732 sec_blob->NtChallengeResponse.MaximumLength = 0;
735 if (ses->domainName == NULL) {
736 sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
737 sec_blob->DomainName.Length = 0;
738 sec_blob->DomainName.MaximumLength = 0;
742 len = cifs_strtoUTF16((__le16 *)tmp, ses->domainName,
743 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
744 len *= 2; /* unicode is 2 bytes each */
745 sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
746 sec_blob->DomainName.Length = cpu_to_le16(len);
747 sec_blob->DomainName.MaximumLength = cpu_to_le16(len);
751 if (ses->user_name == NULL) {
752 sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
753 sec_blob->UserName.Length = 0;
754 sec_blob->UserName.MaximumLength = 0;
758 len = cifs_strtoUTF16((__le16 *)tmp, ses->user_name,
759 CIFS_MAX_USERNAME_LEN, nls_cp);
760 len *= 2; /* unicode is 2 bytes each */
761 sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
762 sec_blob->UserName.Length = cpu_to_le16(len);
763 sec_blob->UserName.MaximumLength = cpu_to_le16(len);
767 sec_blob->WorkstationName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
768 sec_blob->WorkstationName.Length = 0;
769 sec_blob->WorkstationName.MaximumLength = 0;
772 if (((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) ||
773 (ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC))
774 && !calc_seckey(ses)) {
775 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
776 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
777 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
778 sec_blob->SessionKey.MaximumLength =
779 cpu_to_le16(CIFS_CPHTXT_SIZE);
780 tmp += CIFS_CPHTXT_SIZE;
782 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
783 sec_blob->SessionKey.Length = 0;
784 sec_blob->SessionKey.MaximumLength = 0;
787 *buflen = tmp - *pbuffer;
793 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
795 switch (server->negflavor) {
796 case CIFS_NEGFLAVOR_EXTENDED:
802 if (server->sec_ntlmssp &&
803 (global_secflags & CIFSSEC_MAY_NTLMSSP))
805 if ((server->sec_kerberos || server->sec_mskerberos) &&
806 (global_secflags & CIFSSEC_MAY_KRB5))
812 case CIFS_NEGFLAVOR_UNENCAP:
818 if (global_secflags & CIFSSEC_MAY_NTLMV2)
820 if (global_secflags & CIFSSEC_MAY_NTLM)
826 fallthrough; /* to attempt LANMAN authentication next */
827 case CIFS_NEGFLAVOR_LANMAN:
832 if (global_secflags & CIFSSEC_MAY_LANMAN)
845 struct cifs_ses *ses;
846 struct nls_table *nls_cp;
847 void (*func)(struct sess_data *);
850 /* we will send the SMB in three pieces:
851 * a fixed length beginning part, an optional
852 * SPNEGO blob (which can be zero length), and a
853 * last part which will include the strings
854 * and rest of bcc area. This allows us to avoid
855 * a large buffer 17K allocation
862 sess_alloc_buffer(struct sess_data *sess_data, int wct)
865 struct cifs_ses *ses = sess_data->ses;
866 struct smb_hdr *smb_buf;
868 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
874 sess_data->iov[0].iov_base = (char *)smb_buf;
875 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
877 * This variable will be used to clear the buffer
878 * allocated above in case of any error in the calling function.
880 sess_data->buf0_type = CIFS_SMALL_BUFFER;
882 /* 2000 big enough to fit max user, domain, NOS name etc. */
883 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
884 if (!sess_data->iov[2].iov_base) {
886 goto out_free_smb_buf;
893 sess_data->iov[0].iov_base = NULL;
894 sess_data->iov[0].iov_len = 0;
895 sess_data->buf0_type = CIFS_NO_BUFFER;
900 sess_free_buffer(struct sess_data *sess_data)
903 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
904 sess_data->buf0_type = CIFS_NO_BUFFER;
905 kfree(sess_data->iov[2].iov_base);
909 sess_establish_session(struct sess_data *sess_data)
911 struct cifs_ses *ses = sess_data->ses;
913 mutex_lock(&ses->server->srv_mutex);
914 if (!ses->server->session_estab) {
915 if (ses->server->sign) {
916 ses->server->session_key.response =
917 kmemdup(ses->auth_key.response,
918 ses->auth_key.len, GFP_KERNEL);
919 if (!ses->server->session_key.response) {
920 mutex_unlock(&ses->server->srv_mutex);
923 ses->server->session_key.len =
926 ses->server->sequence_number = 0x2;
927 ses->server->session_estab = true;
929 mutex_unlock(&ses->server->srv_mutex);
931 cifs_dbg(FYI, "CIFS session established successfully\n");
932 spin_lock(&GlobalMid_Lock);
933 ses->status = CifsGood;
934 ses->need_reconnect = false;
935 spin_unlock(&GlobalMid_Lock);
941 sess_sendreceive(struct sess_data *sess_data)
944 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
946 struct kvec rsp_iov = { NULL, 0 };
948 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
949 be32_add_cpu(&smb_buf->smb_buf_length, count);
950 put_bcc(count, smb_buf);
952 rc = SendReceive2(sess_data->xid, sess_data->ses,
953 sess_data->iov, 3 /* num_iovecs */,
954 &sess_data->buf0_type,
955 CIFS_LOG_ERROR, &rsp_iov);
956 cifs_small_buf_release(sess_data->iov[0].iov_base);
957 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
963 * LANMAN and plaintext are less secure and off by default.
964 * So we make this explicitly be turned on in kconfig (in the
965 * build) and turned on at runtime (changed from the default)
966 * in proc/fs/cifs or via mount parm. Unfortunately this is
967 * needed for old Win (e.g. Win95), some obscure NAS and OS/2
969 #ifdef CONFIG_CIFS_WEAK_PW_HASH
971 sess_auth_lanman(struct sess_data *sess_data)
974 struct smb_hdr *smb_buf;
975 SESSION_SETUP_ANDX *pSMB;
977 struct cifs_ses *ses = sess_data->ses;
978 char lnm_session_key[CIFS_AUTH_RESP_SIZE];
979 __u16 bytes_remaining;
981 /* lanman 2 style sessionsetup */
983 rc = sess_alloc_buffer(sess_data, 10);
987 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
988 bcc_ptr = sess_data->iov[2].iov_base;
989 (void)cifs_ssetup_hdr(ses, pSMB);
991 pSMB->req.hdr.Flags2 &= ~SMBFLG2_UNICODE;
993 if (ses->user_name != NULL) {
994 /* no capabilities flags in old lanman negotiation */
995 pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
997 /* Calculate hash with password and copy into bcc_ptr.
998 * Encryption Key (stored as in cryptkey) gets used if the
999 * security mode bit in Negotiate Protocol response states
1000 * to use challenge/response method (i.e. Password bit is 1).
1002 rc = calc_lanman_hash(ses->password, ses->server->cryptkey,
1003 ses->server->sec_mode & SECMODE_PW_ENCRYPT ?
1004 true : false, lnm_session_key);
1008 memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_AUTH_RESP_SIZE);
1009 bcc_ptr += CIFS_AUTH_RESP_SIZE;
1011 pSMB->old_req.PasswordLength = 0;
1015 * can not sign if LANMAN negotiated so no need
1016 * to calculate signing key? but what if server
1017 * changed to do higher than lanman dialect and
1018 * we reconnected would we ever calc signing_key?
1021 cifs_dbg(FYI, "Negotiating LANMAN setting up strings\n");
1022 /* Unicode not allowed for LANMAN dialects */
1023 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1025 sess_data->iov[2].iov_len = (long) bcc_ptr -
1026 (long) sess_data->iov[2].iov_base;
1028 rc = sess_sendreceive(sess_data);
1032 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1033 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1035 /* lanman response has a word count of 3 */
1036 if (smb_buf->WordCount != 3) {
1038 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1042 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1043 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1045 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1046 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1048 bytes_remaining = get_bcc(smb_buf);
1049 bcc_ptr = pByteArea(smb_buf);
1051 /* BB check if Unicode and decode strings */
1052 if (bytes_remaining == 0) {
1053 /* no string area to decode, do nothing */
1054 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1055 /* unicode string area must be word-aligned */
1056 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1060 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1063 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1067 rc = sess_establish_session(sess_data);
1069 sess_data->result = rc;
1070 sess_data->func = NULL;
1071 sess_free_buffer(sess_data);
1077 sess_auth_ntlm(struct sess_data *sess_data)
1080 struct smb_hdr *smb_buf;
1081 SESSION_SETUP_ANDX *pSMB;
1083 struct cifs_ses *ses = sess_data->ses;
1085 __u16 bytes_remaining;
1087 /* old style NTLM sessionsetup */
1089 rc = sess_alloc_buffer(sess_data, 13);
1093 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1094 bcc_ptr = sess_data->iov[2].iov_base;
1095 capabilities = cifs_ssetup_hdr(ses, pSMB);
1097 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1098 if (ses->user_name != NULL) {
1099 pSMB->req_no_secext.CaseInsensitivePasswordLength =
1100 cpu_to_le16(CIFS_AUTH_RESP_SIZE);
1101 pSMB->req_no_secext.CaseSensitivePasswordLength =
1102 cpu_to_le16(CIFS_AUTH_RESP_SIZE);
1104 /* calculate ntlm response and session key */
1105 rc = setup_ntlm_response(ses, sess_data->nls_cp);
1107 cifs_dbg(VFS, "Error %d during NTLM authentication\n",
1112 /* copy ntlm response */
1113 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1114 CIFS_AUTH_RESP_SIZE);
1115 bcc_ptr += CIFS_AUTH_RESP_SIZE;
1116 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1117 CIFS_AUTH_RESP_SIZE);
1118 bcc_ptr += CIFS_AUTH_RESP_SIZE;
1120 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1121 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1124 if (ses->capabilities & CAP_UNICODE) {
1125 /* unicode strings must be word aligned */
1126 if (sess_data->iov[0].iov_len % 2) {
1130 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1132 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1136 sess_data->iov[2].iov_len = (long) bcc_ptr -
1137 (long) sess_data->iov[2].iov_base;
1139 rc = sess_sendreceive(sess_data);
1143 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1144 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1146 if (smb_buf->WordCount != 3) {
1148 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1152 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1153 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1155 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1156 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1158 bytes_remaining = get_bcc(smb_buf);
1159 bcc_ptr = pByteArea(smb_buf);
1161 /* BB check if Unicode and decode strings */
1162 if (bytes_remaining == 0) {
1163 /* no string area to decode, do nothing */
1164 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1165 /* unicode string area must be word-aligned */
1166 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1170 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1173 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1177 rc = sess_establish_session(sess_data);
1179 sess_data->result = rc;
1180 sess_data->func = NULL;
1181 sess_free_buffer(sess_data);
1182 kfree(ses->auth_key.response);
1183 ses->auth_key.response = NULL;
1187 sess_auth_ntlmv2(struct sess_data *sess_data)
1190 struct smb_hdr *smb_buf;
1191 SESSION_SETUP_ANDX *pSMB;
1193 struct cifs_ses *ses = sess_data->ses;
1195 __u16 bytes_remaining;
1197 /* old style NTLM sessionsetup */
1199 rc = sess_alloc_buffer(sess_data, 13);
1203 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1204 bcc_ptr = sess_data->iov[2].iov_base;
1205 capabilities = cifs_ssetup_hdr(ses, pSMB);
1207 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1209 /* LM2 password would be here if we supported it */
1210 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1212 if (ses->user_name != NULL) {
1213 /* calculate nlmv2 response and session key */
1214 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1216 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1220 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1221 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1222 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1224 /* set case sensitive password length after tilen may get
1225 * assigned, tilen is 0 otherwise.
1227 pSMB->req_no_secext.CaseSensitivePasswordLength =
1228 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1230 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1233 if (ses->capabilities & CAP_UNICODE) {
1234 if (sess_data->iov[0].iov_len % 2) {
1238 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1240 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1244 sess_data->iov[2].iov_len = (long) bcc_ptr -
1245 (long) sess_data->iov[2].iov_base;
1247 rc = sess_sendreceive(sess_data);
1251 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1252 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1254 if (smb_buf->WordCount != 3) {
1256 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1260 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1261 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1263 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1264 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1266 bytes_remaining = get_bcc(smb_buf);
1267 bcc_ptr = pByteArea(smb_buf);
1269 /* BB check if Unicode and decode strings */
1270 if (bytes_remaining == 0) {
1271 /* no string area to decode, do nothing */
1272 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1273 /* unicode string area must be word-aligned */
1274 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1278 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1281 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1285 rc = sess_establish_session(sess_data);
1287 sess_data->result = rc;
1288 sess_data->func = NULL;
1289 sess_free_buffer(sess_data);
1290 kfree(ses->auth_key.response);
1291 ses->auth_key.response = NULL;
1294 #ifdef CONFIG_CIFS_UPCALL
1296 sess_auth_kerberos(struct sess_data *sess_data)
1299 struct smb_hdr *smb_buf;
1300 SESSION_SETUP_ANDX *pSMB;
1302 struct cifs_ses *ses = sess_data->ses;
1304 __u16 bytes_remaining;
1305 struct key *spnego_key = NULL;
1306 struct cifs_spnego_msg *msg;
1309 /* extended security */
1311 rc = sess_alloc_buffer(sess_data, 12);
1315 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1316 bcc_ptr = sess_data->iov[2].iov_base;
1317 capabilities = cifs_ssetup_hdr(ses, pSMB);
1319 spnego_key = cifs_get_spnego_key(ses);
1320 if (IS_ERR(spnego_key)) {
1321 rc = PTR_ERR(spnego_key);
1326 msg = spnego_key->payload.data[0];
1328 * check version field to make sure that cifs.upcall is
1329 * sending us a response in an expected form
1331 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1332 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1333 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1335 goto out_put_spnego_key;
1338 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1340 if (!ses->auth_key.response) {
1341 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1344 goto out_put_spnego_key;
1346 ses->auth_key.len = msg->sesskey_len;
1348 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1349 capabilities |= CAP_EXTENDED_SECURITY;
1350 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1351 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1352 sess_data->iov[1].iov_len = msg->secblob_len;
1353 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1355 if (ses->capabilities & CAP_UNICODE) {
1356 /* unicode strings must be word aligned */
1357 if ((sess_data->iov[0].iov_len
1358 + sess_data->iov[1].iov_len) % 2) {
1362 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1363 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1365 /* BB: is this right? */
1366 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1369 sess_data->iov[2].iov_len = (long) bcc_ptr -
1370 (long) sess_data->iov[2].iov_base;
1372 rc = sess_sendreceive(sess_data);
1374 goto out_put_spnego_key;
1376 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1377 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1379 if (smb_buf->WordCount != 4) {
1381 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1382 goto out_put_spnego_key;
1385 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1386 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1388 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1389 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1391 bytes_remaining = get_bcc(smb_buf);
1392 bcc_ptr = pByteArea(smb_buf);
1394 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1395 if (blob_len > bytes_remaining) {
1396 cifs_dbg(VFS, "bad security blob length %d\n",
1399 goto out_put_spnego_key;
1401 bcc_ptr += blob_len;
1402 bytes_remaining -= blob_len;
1404 /* BB check if Unicode and decode strings */
1405 if (bytes_remaining == 0) {
1406 /* no string area to decode, do nothing */
1407 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1408 /* unicode string area must be word-aligned */
1409 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1413 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1416 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1420 rc = sess_establish_session(sess_data);
1422 key_invalidate(spnego_key);
1423 key_put(spnego_key);
1425 sess_data->result = rc;
1426 sess_data->func = NULL;
1427 sess_free_buffer(sess_data);
1428 kfree(ses->auth_key.response);
1429 ses->auth_key.response = NULL;
1432 #endif /* ! CONFIG_CIFS_UPCALL */
1435 * The required kvec buffers have to be allocated before calling this
1439 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1441 SESSION_SETUP_ANDX *pSMB;
1442 struct cifs_ses *ses = sess_data->ses;
1446 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1448 capabilities = cifs_ssetup_hdr(ses, pSMB);
1449 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1450 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1454 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1455 capabilities |= CAP_EXTENDED_SECURITY;
1456 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1458 bcc_ptr = sess_data->iov[2].iov_base;
1459 /* unicode strings must be word aligned */
1460 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1464 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1466 sess_data->iov[2].iov_len = (long) bcc_ptr -
1467 (long) sess_data->iov[2].iov_base;
1473 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1476 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1479 struct smb_hdr *smb_buf;
1480 SESSION_SETUP_ANDX *pSMB;
1481 struct cifs_ses *ses = sess_data->ses;
1482 __u16 bytes_remaining;
1486 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1489 * if memory allocation is successful, caller of this function
1492 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1493 if (!ses->ntlmssp) {
1497 ses->ntlmssp->sesskey_per_smbsess = false;
1500 rc = sess_alloc_buffer(sess_data, 12);
1504 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1506 /* Build security blob before we assemble the request */
1507 build_ntlmssp_negotiate_blob(pSMB->req.SecurityBlob, ses);
1508 sess_data->iov[1].iov_len = sizeof(NEGOTIATE_MESSAGE);
1509 sess_data->iov[1].iov_base = pSMB->req.SecurityBlob;
1510 pSMB->req.SecurityBlobLength = cpu_to_le16(sizeof(NEGOTIATE_MESSAGE));
1512 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1516 rc = sess_sendreceive(sess_data);
1518 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1519 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1521 /* If true, rc here is expected and not an error */
1522 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1523 smb_buf->Status.CifsError ==
1524 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1530 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1532 if (smb_buf->WordCount != 4) {
1534 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1538 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1539 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1541 bytes_remaining = get_bcc(smb_buf);
1542 bcc_ptr = pByteArea(smb_buf);
1544 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1545 if (blob_len > bytes_remaining) {
1546 cifs_dbg(VFS, "bad security blob length %d\n",
1552 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1554 sess_free_buffer(sess_data);
1557 sess_data->func = sess_auth_rawntlmssp_authenticate;
1561 /* Else error. Cleanup */
1562 kfree(ses->auth_key.response);
1563 ses->auth_key.response = NULL;
1564 kfree(ses->ntlmssp);
1565 ses->ntlmssp = NULL;
1567 sess_data->func = NULL;
1568 sess_data->result = rc;
1572 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1575 struct smb_hdr *smb_buf;
1576 SESSION_SETUP_ANDX *pSMB;
1577 struct cifs_ses *ses = sess_data->ses;
1578 __u16 bytes_remaining;
1580 unsigned char *ntlmsspblob = NULL;
1583 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1586 rc = sess_alloc_buffer(sess_data, 12);
1590 /* Build security blob before we assemble the request */
1591 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1592 smb_buf = (struct smb_hdr *)pSMB;
1593 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1594 &blob_len, ses, sess_data->nls_cp);
1596 goto out_free_ntlmsspblob;
1597 sess_data->iov[1].iov_len = blob_len;
1598 sess_data->iov[1].iov_base = ntlmsspblob;
1599 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1601 * Make sure that we tell the server that we are using
1602 * the uid that it just gave us back on the response
1605 smb_buf->Uid = ses->Suid;
1607 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1609 goto out_free_ntlmsspblob;
1611 rc = sess_sendreceive(sess_data);
1613 goto out_free_ntlmsspblob;
1615 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1616 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1617 if (smb_buf->WordCount != 4) {
1619 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1620 goto out_free_ntlmsspblob;
1623 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1624 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1626 if (ses->Suid != smb_buf->Uid) {
1627 ses->Suid = smb_buf->Uid;
1628 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1631 bytes_remaining = get_bcc(smb_buf);
1632 bcc_ptr = pByteArea(smb_buf);
1633 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1634 if (blob_len > bytes_remaining) {
1635 cifs_dbg(VFS, "bad security blob length %d\n",
1638 goto out_free_ntlmsspblob;
1640 bcc_ptr += blob_len;
1641 bytes_remaining -= blob_len;
1644 /* BB check if Unicode and decode strings */
1645 if (bytes_remaining == 0) {
1646 /* no string area to decode, do nothing */
1647 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1648 /* unicode string area must be word-aligned */
1649 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1653 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1656 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1660 out_free_ntlmsspblob:
1663 sess_free_buffer(sess_data);
1666 rc = sess_establish_session(sess_data);
1669 kfree(ses->auth_key.response);
1670 ses->auth_key.response = NULL;
1671 kfree(ses->ntlmssp);
1672 ses->ntlmssp = NULL;
1674 sess_data->func = NULL;
1675 sess_data->result = rc;
1678 static int select_sec(struct cifs_ses *ses, struct sess_data *sess_data)
1682 type = cifs_select_sectype(ses->server, ses->sectype);
1683 cifs_dbg(FYI, "sess setup type %d\n", type);
1684 if (type == Unspecified) {
1685 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1691 /* LANMAN and plaintext are less secure and off by default.
1692 * So we make this explicitly be turned on in kconfig (in the
1693 * build) and turned on at runtime (changed from the default)
1694 * in proc/fs/cifs or via mount parm. Unfortunately this is
1695 * needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
1696 #ifdef CONFIG_CIFS_WEAK_PW_HASH
1697 sess_data->func = sess_auth_lanman;
1703 sess_data->func = sess_auth_ntlm;
1706 sess_data->func = sess_auth_ntlmv2;
1709 #ifdef CONFIG_CIFS_UPCALL
1710 sess_data->func = sess_auth_kerberos;
1713 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1715 #endif /* CONFIG_CIFS_UPCALL */
1717 sess_data->func = sess_auth_rawntlmssp_negotiate;
1720 cifs_dbg(VFS, "secType %d not supported!\n", type);
1727 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1728 const struct nls_table *nls_cp)
1731 struct sess_data *sess_data;
1734 WARN(1, "%s: ses == NULL!", __func__);
1738 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1742 rc = select_sec(ses, sess_data);
1746 sess_data->xid = xid;
1747 sess_data->ses = ses;
1748 sess_data->buf0_type = CIFS_NO_BUFFER;
1749 sess_data->nls_cp = (struct nls_table *) nls_cp;
1751 while (sess_data->func)
1752 sess_data->func(sess_data);
1754 /* Store result before we free sess_data */
1755 rc = sess_data->result;