1 // SPDX-License-Identifier: LGPL-2.1
4 * SMB/CIFS session setup handling routines
6 * Copyright (c) International Business Machines Corp., 2006, 2009
13 #include "cifsproto.h"
14 #include "cifs_unicode.h"
15 #include "cifs_debug.h"
18 #include <linux/utsname.h>
19 #include <linux/slab.h>
20 #include <linux/version.h>
22 #include "cifs_spnego.h"
23 #include "smb2proto.h"
24 #include "fs_context.h"
27 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
28 struct cifs_server_iface *iface);
31 is_server_using_iface(struct TCP_Server_Info *server,
32 struct cifs_server_iface *iface)
34 struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr;
35 struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr;
36 struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr;
37 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr;
39 if (server->dstaddr.ss_family != iface->sockaddr.ss_family)
41 if (server->dstaddr.ss_family == AF_INET) {
42 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
44 } else if (server->dstaddr.ss_family == AF_INET6) {
45 if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
46 sizeof(i6->sin6_addr)) != 0)
49 /* unknown family.. */
55 bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
59 spin_lock(&ses->chan_lock);
60 for (i = 0; i < ses->chan_count; i++) {
61 if (is_server_using_iface(ses->chans[i].server, iface)) {
62 spin_unlock(&ses->chan_lock);
66 spin_unlock(&ses->chan_lock);
70 /* channel helper functions. assumed that chan_lock is held by caller. */
73 cifs_ses_get_chan_index(struct cifs_ses *ses,
74 struct TCP_Server_Info *server)
78 for (i = 0; i < ses->chan_count; i++) {
79 if (ses->chans[i].server == server)
83 /* If we didn't find the channel, it is likely a bug */
89 cifs_chan_set_need_reconnect(struct cifs_ses *ses,
90 struct TCP_Server_Info *server)
92 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
94 set_bit(chan_index, &ses->chans_need_reconnect);
95 cifs_dbg(FYI, "Set reconnect bitmask for chan %u; now 0x%lx\n",
96 chan_index, ses->chans_need_reconnect);
100 cifs_chan_clear_need_reconnect(struct cifs_ses *ses,
101 struct TCP_Server_Info *server)
103 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
105 clear_bit(chan_index, &ses->chans_need_reconnect);
106 cifs_dbg(FYI, "Cleared reconnect bitmask for chan %u; now 0x%lx\n",
107 chan_index, ses->chans_need_reconnect);
111 cifs_chan_needs_reconnect(struct cifs_ses *ses,
112 struct TCP_Server_Info *server)
114 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
116 return CIFS_CHAN_NEEDS_RECONNECT(ses, chan_index);
119 /* returns number of channels added */
120 int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
122 int old_chan_count, new_chan_count;
127 struct cifs_server_iface *ifaces = NULL;
130 spin_lock(&ses->chan_lock);
132 new_chan_count = old_chan_count = ses->chan_count;
133 left = ses->chan_max - ses->chan_count;
136 spin_unlock(&ses->chan_lock);
138 "ses already at max_channels (%zu), nothing to open\n",
143 if (ses->server->dialect < SMB30_PROT_ID) {
144 spin_unlock(&ses->chan_lock);
145 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
149 if (!(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
151 spin_unlock(&ses->chan_lock);
152 cifs_dbg(VFS, "server %s does not support multichannel\n", ses->server->hostname);
155 spin_unlock(&ses->chan_lock);
158 * Make a copy of the iface list at the time and use that
159 * instead so as to not hold the iface spinlock for opening
162 spin_lock(&ses->iface_lock);
163 iface_count = ses->iface_count;
164 if (iface_count <= 0) {
165 spin_unlock(&ses->iface_lock);
166 cifs_dbg(VFS, "no iface list available to open channels\n");
169 ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces),
172 spin_unlock(&ses->iface_lock);
175 spin_unlock(&ses->iface_lock);
178 * Keep connecting to same, fastest, iface for all channels as
179 * long as its RSS. Try next fastest one if not RSS or channel
183 struct cifs_server_iface *iface;
186 if (tries > 3*ses->chan_max) {
187 cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
193 if (is_ses_using_iface(ses, iface) && !iface->rss_capable) {
194 i = (i+1) % iface_count;
198 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
200 cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n",
202 i = (i+1) % iface_count;
206 cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
213 return new_chan_count - old_chan_count;
217 * If server is a channel of ses, return the corresponding enclosing
218 * cifs_chan otherwise return NULL.
221 cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
225 spin_lock(&ses->chan_lock);
226 for (i = 0; i < ses->chan_count; i++) {
227 if (ses->chans[i].server == server) {
228 spin_unlock(&ses->chan_lock);
229 return &ses->chans[i];
232 spin_unlock(&ses->chan_lock);
237 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
238 struct cifs_server_iface *iface)
240 struct TCP_Server_Info *chan_server;
241 struct cifs_chan *chan;
242 struct smb3_fs_context ctx = {NULL};
243 static const char unc_fmt[] = "\\%s\\foo";
244 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
245 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
246 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
248 unsigned int xid = get_xid();
250 if (iface->sockaddr.ss_family == AF_INET)
251 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
252 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
255 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
256 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
260 * Setup a ctx with mostly the same info as the existing
261 * session and overwrite it with the requested iface data.
263 * We need to setup at least the fields used for negprot and
266 * We only need the ctx here, so we can reuse memory from
267 * the session and server without caring about memory
271 /* Always make new connection for now (TODO?) */
272 ctx.nosharesock = true;
275 ctx.domainauto = ses->domainAuto;
276 ctx.domainname = ses->domainName;
277 ctx.server_hostname = ses->server->hostname;
278 ctx.username = ses->user_name;
279 ctx.password = ses->password;
280 ctx.sectype = ses->sectype;
281 ctx.sign = ses->sign;
284 /* XXX: Use ses->server->hostname? */
285 sprintf(unc, unc_fmt, ses->ip_addr);
289 /* Reuse same version as master connection */
290 ctx.vals = ses->server->vals;
291 ctx.ops = ses->server->ops;
293 ctx.noblocksnd = ses->server->noblocksnd;
294 ctx.noautotune = ses->server->noautotune;
295 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
296 ctx.echo_interval = ses->server->echo_interval / HZ;
297 ctx.max_credits = ses->server->max_credits;
300 * This will be used for encoding/decoding user/domain/pw
301 * during sess setup auth.
303 ctx.local_nls = cifs_sb->local_nls;
305 /* Use RDMA if possible */
306 ctx.rdma = iface->rdma_capable;
307 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
309 /* reuse master con client guid */
310 memcpy(&ctx.client_guid, ses->server->client_guid,
311 SMB2_CLIENT_GUID_SIZE);
312 ctx.use_client_guid = true;
314 chan_server = cifs_get_tcp_session(&ctx, ses->server);
316 spin_lock(&ses->chan_lock);
317 chan = &ses->chans[ses->chan_count];
318 chan->server = chan_server;
319 if (IS_ERR(chan->server)) {
320 rc = PTR_ERR(chan->server);
322 spin_unlock(&ses->chan_lock);
326 atomic_set(&ses->chan_seq, 0);
328 /* Mark this channel as needing connect/setup */
329 cifs_chan_set_need_reconnect(ses, chan->server);
331 spin_unlock(&ses->chan_lock);
333 mutex_lock(&ses->session_mutex);
335 * We need to allocate the server crypto now as we will need
336 * to sign packets before we generate the channel signing key
337 * (we sign with the session key)
339 rc = smb311_crypto_shash_allocate(chan->server);
341 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
342 mutex_unlock(&ses->session_mutex);
346 rc = cifs_negotiate_protocol(xid, ses, chan->server);
348 rc = cifs_setup_session(xid, ses, chan->server, cifs_sb->local_nls);
350 mutex_unlock(&ses->session_mutex);
353 if (rc && chan->server) {
354 spin_lock(&ses->chan_lock);
355 /* we rely on all bits beyond chan_count to be clear */
356 cifs_chan_clear_need_reconnect(ses, chan->server);
359 * chan_count should never reach 0 as at least the primary
360 * channel is always allocated
362 WARN_ON(ses->chan_count < 1);
363 spin_unlock(&ses->chan_lock);
366 if (rc && chan->server)
367 cifs_put_tcp_session(chan->server, 0);
372 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses,
373 struct TCP_Server_Info *server,
374 SESSION_SETUP_ANDX *pSMB)
376 __u32 capabilities = 0;
378 /* init fields common to all four types of SessSetup */
379 /* Note that offsets for first seven fields in req struct are same */
380 /* in CIFS Specs so does not matter which of 3 forms of struct */
381 /* that we use in next few lines */
382 /* Note that header is initialized to zero in header_assemble */
383 pSMB->req.AndXCommand = 0xFF;
384 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
385 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
387 pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq);
388 pSMB->req.VcNumber = cpu_to_le16(1);
390 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
392 /* BB verify whether signing required on neg or just on auth frame
395 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
396 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
399 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
401 if (ses->capabilities & CAP_UNICODE) {
402 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
403 capabilities |= CAP_UNICODE;
405 if (ses->capabilities & CAP_STATUS32) {
406 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
407 capabilities |= CAP_STATUS32;
409 if (ses->capabilities & CAP_DFS) {
410 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
411 capabilities |= CAP_DFS;
413 if (ses->capabilities & CAP_UNIX)
414 capabilities |= CAP_UNIX;
420 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
422 char *bcc_ptr = *pbcc_area;
425 /* Copy OS version */
426 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
428 bcc_ptr += 2 * bytes_ret;
429 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
431 bcc_ptr += 2 * bytes_ret;
432 bcc_ptr += 2; /* trailing null */
434 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
436 bcc_ptr += 2 * bytes_ret;
437 bcc_ptr += 2; /* trailing null */
439 *pbcc_area = bcc_ptr;
442 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
443 const struct nls_table *nls_cp)
445 char *bcc_ptr = *pbcc_area;
449 if (ses->domainName == NULL) {
450 /* Sending null domain better than using a bogus domain name (as
451 we did briefly in 2.6.18) since server will use its default */
456 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
457 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
458 bcc_ptr += 2 * bytes_ret;
459 bcc_ptr += 2; /* account for null terminator */
461 *pbcc_area = bcc_ptr;
465 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
466 const struct nls_table *nls_cp)
468 char *bcc_ptr = *pbcc_area;
471 /* BB FIXME add check that strings total less
472 than 335 or will need to send them as arrays */
474 /* unicode strings, must be word aligned before the call */
475 /* if ((long) bcc_ptr % 2) {
480 if (ses->user_name == NULL) {
481 /* null user mount */
485 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
486 CIFS_MAX_USERNAME_LEN, nls_cp);
488 bcc_ptr += 2 * bytes_ret;
489 bcc_ptr += 2; /* account for null termination */
491 unicode_domain_string(&bcc_ptr, ses, nls_cp);
492 unicode_oslm_strings(&bcc_ptr, nls_cp);
494 *pbcc_area = bcc_ptr;
497 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
498 const struct nls_table *nls_cp)
500 char *bcc_ptr = *pbcc_area;
504 /* BB what about null user mounts - check that we do this BB */
506 if (ses->user_name != NULL) {
507 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
508 if (WARN_ON_ONCE(len < 0))
509 len = CIFS_MAX_USERNAME_LEN - 1;
512 /* else null user mount */
514 bcc_ptr++; /* account for null termination */
517 if (ses->domainName != NULL) {
518 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
519 if (WARN_ON_ONCE(len < 0))
520 len = CIFS_MAX_DOMAINNAME_LEN - 1;
522 } /* else we will send a null domain name
523 so the server will default to its own domain */
527 /* BB check for overflow here */
529 strcpy(bcc_ptr, "Linux version ");
530 bcc_ptr += strlen("Linux version ");
531 strcpy(bcc_ptr, init_utsname()->release);
532 bcc_ptr += strlen(init_utsname()->release) + 1;
534 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
535 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
537 *pbcc_area = bcc_ptr;
541 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
542 const struct nls_table *nls_cp)
545 char *data = *pbcc_area;
547 cifs_dbg(FYI, "bleft %d\n", bleft);
549 kfree(ses->serverOS);
550 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
551 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
552 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
558 kfree(ses->serverNOS);
559 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
560 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
561 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
567 kfree(ses->serverDomain);
568 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
569 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
574 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
575 struct cifs_ses *ses,
576 const struct nls_table *nls_cp)
579 char *bcc_ptr = *pbcc_area;
581 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
583 len = strnlen(bcc_ptr, bleft);
587 kfree(ses->serverOS);
589 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
591 memcpy(ses->serverOS, bcc_ptr, len);
592 ses->serverOS[len] = 0;
593 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
594 cifs_dbg(FYI, "OS/2 server\n");
600 len = strnlen(bcc_ptr, bleft);
604 kfree(ses->serverNOS);
606 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
607 if (ses->serverNOS) {
608 memcpy(ses->serverNOS, bcc_ptr, len);
609 ses->serverNOS[len] = 0;
615 len = strnlen(bcc_ptr, bleft);
619 /* No domain field in LANMAN case. Domain is
620 returned by old servers in the SMB negprot response */
621 /* BB For newer servers which do not support Unicode,
622 but thus do return domain here we could add parsing
623 for it later, but it is not very important */
624 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
627 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
628 struct cifs_ses *ses)
630 unsigned int tioffset; /* challenge message target info area */
631 unsigned int tilen; /* challenge message target info area length */
632 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
635 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
636 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
640 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
641 cifs_dbg(VFS, "blob signature incorrect %s\n",
645 if (pblob->MessageType != NtLmChallenge) {
646 cifs_dbg(VFS, "Incorrect message type %d\n",
651 server_flags = le32_to_cpu(pblob->NegotiateFlags);
652 cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__,
653 ses->ntlmssp->client_flags, server_flags);
655 if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) &&
656 (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) {
657 cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n",
661 if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) {
662 cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__);
665 if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) {
666 cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n",
670 if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
671 !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH))
672 pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n",
675 ses->ntlmssp->server_flags = server_flags;
677 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
678 /* In particular we can examine sign flags */
679 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
680 we must set the MIC field of the AUTHENTICATE_MESSAGE */
682 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
683 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
684 if (tioffset > blob_len || tioffset + tilen > blob_len) {
685 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
690 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
692 if (!ses->auth_key.response) {
693 cifs_dbg(VFS, "Challenge target info alloc failure\n");
696 ses->auth_key.len = tilen;
702 static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
704 int sz = base_size + ses->auth_key.len
705 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
708 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
710 sz += sizeof(__le16);
713 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
715 sz += sizeof(__le16);
717 if (ses->workstation_name)
718 sz += sizeof(__le16) * strnlen(ses->workstation_name,
719 CIFS_MAX_WORKSTATION_LEN);
721 sz += sizeof(__le16);
726 static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
729 unsigned char *pstart,
730 unsigned char **pcur,
731 const struct nls_table *nls_cp)
733 unsigned char *tmp = pstart;
743 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
745 pbuf->MaximumLength = 0;
746 *pcur += sizeof(__le16);
748 len = cifs_strtoUTF16((__le16 *)*pcur,
752 len *= sizeof(__le16);
753 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
754 pbuf->Length = cpu_to_le16(len);
755 pbuf->MaximumLength = cpu_to_le16(len);
760 /* BB Move to ntlmssp.c eventually */
762 int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
764 struct cifs_ses *ses,
765 struct TCP_Server_Info *server,
766 const struct nls_table *nls_cp)
769 NEGOTIATE_MESSAGE *sec_blob;
774 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
775 *pbuffer = kmalloc(len, GFP_KERNEL);
778 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
780 goto setup_ntlm_neg_ret;
782 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
784 memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
785 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
786 sec_blob->MessageType = NtLmNegotiate;
788 /* BB is NTLMV2 session security format easier to use here? */
789 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
790 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
791 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
792 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
793 NTLMSSP_NEGOTIATE_SIGN;
794 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
795 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
797 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
798 ses->ntlmssp->client_flags = flags;
799 sec_blob->NegotiateFlags = cpu_to_le32(flags);
801 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
802 cifs_security_buffer_from_str(&sec_blob->DomainName,
804 CIFS_MAX_DOMAINNAME_LEN,
808 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
810 CIFS_MAX_WORKSTATION_LEN,
814 *buflen = tmp - *pbuffer;
820 * Build ntlmssp blob with additional fields, such as version,
821 * supported by modern servers. For safety limit to SMB3 or later
822 * See notes in MS-NLMP Section 2.2.2.1 e.g.
824 int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer,
826 struct cifs_ses *ses,
827 struct TCP_Server_Info *server,
828 const struct nls_table *nls_cp)
831 struct negotiate_message *sec_blob;
836 len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message));
837 *pbuffer = kmalloc(len, GFP_KERNEL);
840 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
842 goto setup_ntlm_smb3_neg_ret;
844 sec_blob = (struct negotiate_message *)*pbuffer;
846 memset(*pbuffer, 0, sizeof(struct negotiate_message));
847 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
848 sec_blob->MessageType = NtLmNegotiate;
850 /* BB is NTLMV2 session security format easier to use here? */
851 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
852 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
853 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
854 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
855 NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION;
856 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
857 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
859 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
860 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
861 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
862 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
864 tmp = *pbuffer + sizeof(struct negotiate_message);
865 ses->ntlmssp->client_flags = flags;
866 sec_blob->NegotiateFlags = cpu_to_le32(flags);
868 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
869 cifs_security_buffer_from_str(&sec_blob->DomainName,
871 CIFS_MAX_DOMAINNAME_LEN,
875 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
877 CIFS_MAX_WORKSTATION_LEN,
881 *buflen = tmp - *pbuffer;
882 setup_ntlm_smb3_neg_ret:
887 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
889 struct cifs_ses *ses,
890 struct TCP_Server_Info *server,
891 const struct nls_table *nls_cp)
894 AUTHENTICATE_MESSAGE *sec_blob;
899 rc = setup_ntlmv2_rsp(ses, nls_cp);
901 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
903 goto setup_ntlmv2_ret;
906 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
907 *pbuffer = kmalloc(len, GFP_KERNEL);
910 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
912 goto setup_ntlmv2_ret;
914 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
916 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
917 sec_blob->MessageType = NtLmAuthenticate;
919 flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET |
920 NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
922 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
923 sec_blob->NegotiateFlags = cpu_to_le32(flags);
925 sec_blob->LmChallengeResponse.BufferOffset =
926 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
927 sec_blob->LmChallengeResponse.Length = 0;
928 sec_blob->LmChallengeResponse.MaximumLength = 0;
930 sec_blob->NtChallengeResponse.BufferOffset =
931 cpu_to_le32(tmp - *pbuffer);
932 if (ses->user_name != NULL) {
933 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
934 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
935 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
937 sec_blob->NtChallengeResponse.Length =
938 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
939 sec_blob->NtChallengeResponse.MaximumLength =
940 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
943 * don't send an NT Response for anonymous access
945 sec_blob->NtChallengeResponse.Length = 0;
946 sec_blob->NtChallengeResponse.MaximumLength = 0;
949 cifs_security_buffer_from_str(&sec_blob->DomainName,
951 CIFS_MAX_DOMAINNAME_LEN,
955 cifs_security_buffer_from_str(&sec_blob->UserName,
957 CIFS_MAX_USERNAME_LEN,
961 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
962 ses->workstation_name,
963 CIFS_MAX_WORKSTATION_LEN,
967 if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
968 (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) &&
970 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
971 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
972 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
973 sec_blob->SessionKey.MaximumLength =
974 cpu_to_le16(CIFS_CPHTXT_SIZE);
975 tmp += CIFS_CPHTXT_SIZE;
977 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
978 sec_blob->SessionKey.Length = 0;
979 sec_blob->SessionKey.MaximumLength = 0;
982 *buflen = tmp - *pbuffer;
988 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
990 switch (server->negflavor) {
991 case CIFS_NEGFLAVOR_EXTENDED:
997 if (server->sec_ntlmssp &&
998 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1000 if ((server->sec_kerberos || server->sec_mskerberos) &&
1001 (global_secflags & CIFSSEC_MAY_KRB5))
1007 case CIFS_NEGFLAVOR_UNENCAP:
1008 switch (requested) {
1012 if (global_secflags & CIFSSEC_MAY_NTLMV2)
1026 struct cifs_ses *ses;
1027 struct TCP_Server_Info *server;
1028 struct nls_table *nls_cp;
1029 void (*func)(struct sess_data *);
1032 /* we will send the SMB in three pieces:
1033 * a fixed length beginning part, an optional
1034 * SPNEGO blob (which can be zero length), and a
1035 * last part which will include the strings
1036 * and rest of bcc area. This allows us to avoid
1037 * a large buffer 17K allocation
1044 sess_alloc_buffer(struct sess_data *sess_data, int wct)
1047 struct cifs_ses *ses = sess_data->ses;
1048 struct smb_hdr *smb_buf;
1050 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
1056 sess_data->iov[0].iov_base = (char *)smb_buf;
1057 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
1059 * This variable will be used to clear the buffer
1060 * allocated above in case of any error in the calling function.
1062 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1064 /* 2000 big enough to fit max user, domain, NOS name etc. */
1065 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
1066 if (!sess_data->iov[2].iov_base) {
1068 goto out_free_smb_buf;
1074 cifs_small_buf_release(smb_buf);
1075 sess_data->iov[0].iov_base = NULL;
1076 sess_data->iov[0].iov_len = 0;
1077 sess_data->buf0_type = CIFS_NO_BUFFER;
1082 sess_free_buffer(struct sess_data *sess_data)
1085 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1086 sess_data->buf0_type = CIFS_NO_BUFFER;
1087 kfree(sess_data->iov[2].iov_base);
1091 sess_establish_session(struct sess_data *sess_data)
1093 struct cifs_ses *ses = sess_data->ses;
1094 struct TCP_Server_Info *server = sess_data->server;
1096 mutex_lock(&server->srv_mutex);
1097 if (!server->session_estab) {
1099 server->session_key.response =
1100 kmemdup(ses->auth_key.response,
1101 ses->auth_key.len, GFP_KERNEL);
1102 if (!server->session_key.response) {
1103 mutex_unlock(&server->srv_mutex);
1106 server->session_key.len =
1109 server->sequence_number = 0x2;
1110 server->session_estab = true;
1112 mutex_unlock(&server->srv_mutex);
1114 cifs_dbg(FYI, "CIFS session established successfully\n");
1119 sess_sendreceive(struct sess_data *sess_data)
1122 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
1124 struct kvec rsp_iov = { NULL, 0 };
1126 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
1127 be32_add_cpu(&smb_buf->smb_buf_length, count);
1128 put_bcc(count, smb_buf);
1130 rc = SendReceive2(sess_data->xid, sess_data->ses,
1131 sess_data->iov, 3 /* num_iovecs */,
1132 &sess_data->buf0_type,
1133 CIFS_LOG_ERROR, &rsp_iov);
1134 cifs_small_buf_release(sess_data->iov[0].iov_base);
1135 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1141 sess_auth_ntlmv2(struct sess_data *sess_data)
1144 struct smb_hdr *smb_buf;
1145 SESSION_SETUP_ANDX *pSMB;
1147 struct cifs_ses *ses = sess_data->ses;
1148 struct TCP_Server_Info *server = sess_data->server;
1150 __u16 bytes_remaining;
1152 /* old style NTLM sessionsetup */
1154 rc = sess_alloc_buffer(sess_data, 13);
1158 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1159 bcc_ptr = sess_data->iov[2].iov_base;
1160 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1162 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1164 /* LM2 password would be here if we supported it */
1165 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1167 if (ses->user_name != NULL) {
1168 /* calculate nlmv2 response and session key */
1169 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1171 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1175 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1176 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1177 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1179 /* set case sensitive password length after tilen may get
1180 * assigned, tilen is 0 otherwise.
1182 pSMB->req_no_secext.CaseSensitivePasswordLength =
1183 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1185 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1188 if (ses->capabilities & CAP_UNICODE) {
1189 if (sess_data->iov[0].iov_len % 2) {
1193 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1195 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1199 sess_data->iov[2].iov_len = (long) bcc_ptr -
1200 (long) sess_data->iov[2].iov_base;
1202 rc = sess_sendreceive(sess_data);
1206 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1207 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1209 if (smb_buf->WordCount != 3) {
1211 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1215 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1216 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1218 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1219 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1221 bytes_remaining = get_bcc(smb_buf);
1222 bcc_ptr = pByteArea(smb_buf);
1224 /* BB check if Unicode and decode strings */
1225 if (bytes_remaining == 0) {
1226 /* no string area to decode, do nothing */
1227 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1228 /* unicode string area must be word-aligned */
1229 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1233 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1236 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1240 rc = sess_establish_session(sess_data);
1242 sess_data->result = rc;
1243 sess_data->func = NULL;
1244 sess_free_buffer(sess_data);
1245 kfree(ses->auth_key.response);
1246 ses->auth_key.response = NULL;
1249 #ifdef CONFIG_CIFS_UPCALL
1251 sess_auth_kerberos(struct sess_data *sess_data)
1254 struct smb_hdr *smb_buf;
1255 SESSION_SETUP_ANDX *pSMB;
1257 struct cifs_ses *ses = sess_data->ses;
1258 struct TCP_Server_Info *server = sess_data->server;
1260 __u16 bytes_remaining;
1261 struct key *spnego_key = NULL;
1262 struct cifs_spnego_msg *msg;
1265 /* extended security */
1267 rc = sess_alloc_buffer(sess_data, 12);
1271 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1272 bcc_ptr = sess_data->iov[2].iov_base;
1273 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1275 spnego_key = cifs_get_spnego_key(ses, server);
1276 if (IS_ERR(spnego_key)) {
1277 rc = PTR_ERR(spnego_key);
1282 msg = spnego_key->payload.data[0];
1284 * check version field to make sure that cifs.upcall is
1285 * sending us a response in an expected form
1287 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1288 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1289 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1291 goto out_put_spnego_key;
1294 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1296 if (!ses->auth_key.response) {
1297 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1300 goto out_put_spnego_key;
1302 ses->auth_key.len = msg->sesskey_len;
1304 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1305 capabilities |= CAP_EXTENDED_SECURITY;
1306 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1307 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1308 sess_data->iov[1].iov_len = msg->secblob_len;
1309 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1311 if (ses->capabilities & CAP_UNICODE) {
1312 /* unicode strings must be word aligned */
1313 if ((sess_data->iov[0].iov_len
1314 + sess_data->iov[1].iov_len) % 2) {
1318 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1319 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1321 /* BB: is this right? */
1322 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1325 sess_data->iov[2].iov_len = (long) bcc_ptr -
1326 (long) sess_data->iov[2].iov_base;
1328 rc = sess_sendreceive(sess_data);
1330 goto out_put_spnego_key;
1332 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1333 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1335 if (smb_buf->WordCount != 4) {
1337 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1338 goto out_put_spnego_key;
1341 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1342 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1344 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1345 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1347 bytes_remaining = get_bcc(smb_buf);
1348 bcc_ptr = pByteArea(smb_buf);
1350 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1351 if (blob_len > bytes_remaining) {
1352 cifs_dbg(VFS, "bad security blob length %d\n",
1355 goto out_put_spnego_key;
1357 bcc_ptr += blob_len;
1358 bytes_remaining -= blob_len;
1360 /* BB check if Unicode and decode strings */
1361 if (bytes_remaining == 0) {
1362 /* no string area to decode, do nothing */
1363 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1364 /* unicode string area must be word-aligned */
1365 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1369 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1372 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1376 rc = sess_establish_session(sess_data);
1378 key_invalidate(spnego_key);
1379 key_put(spnego_key);
1381 sess_data->result = rc;
1382 sess_data->func = NULL;
1383 sess_free_buffer(sess_data);
1384 kfree(ses->auth_key.response);
1385 ses->auth_key.response = NULL;
1388 #endif /* ! CONFIG_CIFS_UPCALL */
1391 * The required kvec buffers have to be allocated before calling this
1395 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1397 SESSION_SETUP_ANDX *pSMB;
1398 struct cifs_ses *ses = sess_data->ses;
1399 struct TCP_Server_Info *server = sess_data->server;
1403 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1405 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1406 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1407 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1411 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1412 capabilities |= CAP_EXTENDED_SECURITY;
1413 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1415 bcc_ptr = sess_data->iov[2].iov_base;
1416 /* unicode strings must be word aligned */
1417 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1421 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1423 sess_data->iov[2].iov_len = (long) bcc_ptr -
1424 (long) sess_data->iov[2].iov_base;
1430 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1433 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1436 struct smb_hdr *smb_buf;
1437 SESSION_SETUP_ANDX *pSMB;
1438 struct cifs_ses *ses = sess_data->ses;
1439 struct TCP_Server_Info *server = sess_data->server;
1440 __u16 bytes_remaining;
1442 unsigned char *ntlmsspblob = NULL;
1445 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1448 * if memory allocation is successful, caller of this function
1451 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1452 if (!ses->ntlmssp) {
1456 ses->ntlmssp->sesskey_per_smbsess = false;
1459 rc = sess_alloc_buffer(sess_data, 12);
1463 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1465 /* Build security blob before we assemble the request */
1466 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
1467 &blob_len, ses, server,
1470 goto out_free_ntlmsspblob;
1472 sess_data->iov[1].iov_len = blob_len;
1473 sess_data->iov[1].iov_base = ntlmsspblob;
1474 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1476 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1478 goto out_free_ntlmsspblob;
1480 rc = sess_sendreceive(sess_data);
1482 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1483 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1485 /* If true, rc here is expected and not an error */
1486 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1487 smb_buf->Status.CifsError ==
1488 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1492 goto out_free_ntlmsspblob;
1494 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1496 if (smb_buf->WordCount != 4) {
1498 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1499 goto out_free_ntlmsspblob;
1502 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1503 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1505 bytes_remaining = get_bcc(smb_buf);
1506 bcc_ptr = pByteArea(smb_buf);
1508 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1509 if (blob_len > bytes_remaining) {
1510 cifs_dbg(VFS, "bad security blob length %d\n",
1513 goto out_free_ntlmsspblob;
1516 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1518 out_free_ntlmsspblob:
1521 sess_free_buffer(sess_data);
1524 sess_data->func = sess_auth_rawntlmssp_authenticate;
1528 /* Else error. Cleanup */
1529 kfree(ses->auth_key.response);
1530 ses->auth_key.response = NULL;
1531 kfree(ses->ntlmssp);
1532 ses->ntlmssp = NULL;
1534 sess_data->func = NULL;
1535 sess_data->result = rc;
1539 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1542 struct smb_hdr *smb_buf;
1543 SESSION_SETUP_ANDX *pSMB;
1544 struct cifs_ses *ses = sess_data->ses;
1545 struct TCP_Server_Info *server = sess_data->server;
1546 __u16 bytes_remaining;
1548 unsigned char *ntlmsspblob = NULL;
1551 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1554 rc = sess_alloc_buffer(sess_data, 12);
1558 /* Build security blob before we assemble the request */
1559 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1560 smb_buf = (struct smb_hdr *)pSMB;
1561 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1562 &blob_len, ses, server,
1565 goto out_free_ntlmsspblob;
1566 sess_data->iov[1].iov_len = blob_len;
1567 sess_data->iov[1].iov_base = ntlmsspblob;
1568 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1570 * Make sure that we tell the server that we are using
1571 * the uid that it just gave us back on the response
1574 smb_buf->Uid = ses->Suid;
1576 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1578 goto out_free_ntlmsspblob;
1580 rc = sess_sendreceive(sess_data);
1582 goto out_free_ntlmsspblob;
1584 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1585 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1586 if (smb_buf->WordCount != 4) {
1588 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1589 goto out_free_ntlmsspblob;
1592 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1593 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1595 if (ses->Suid != smb_buf->Uid) {
1596 ses->Suid = smb_buf->Uid;
1597 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1600 bytes_remaining = get_bcc(smb_buf);
1601 bcc_ptr = pByteArea(smb_buf);
1602 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1603 if (blob_len > bytes_remaining) {
1604 cifs_dbg(VFS, "bad security blob length %d\n",
1607 goto out_free_ntlmsspblob;
1609 bcc_ptr += blob_len;
1610 bytes_remaining -= blob_len;
1613 /* BB check if Unicode and decode strings */
1614 if (bytes_remaining == 0) {
1615 /* no string area to decode, do nothing */
1616 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1617 /* unicode string area must be word-aligned */
1618 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1622 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1625 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1629 out_free_ntlmsspblob:
1632 sess_free_buffer(sess_data);
1635 rc = sess_establish_session(sess_data);
1638 kfree(ses->auth_key.response);
1639 ses->auth_key.response = NULL;
1640 kfree(ses->ntlmssp);
1641 ses->ntlmssp = NULL;
1643 sess_data->func = NULL;
1644 sess_data->result = rc;
1647 static int select_sec(struct sess_data *sess_data)
1650 struct cifs_ses *ses = sess_data->ses;
1651 struct TCP_Server_Info *server = sess_data->server;
1653 type = cifs_select_sectype(server, ses->sectype);
1654 cifs_dbg(FYI, "sess setup type %d\n", type);
1655 if (type == Unspecified) {
1656 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1662 sess_data->func = sess_auth_ntlmv2;
1665 #ifdef CONFIG_CIFS_UPCALL
1666 sess_data->func = sess_auth_kerberos;
1669 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1671 #endif /* CONFIG_CIFS_UPCALL */
1673 sess_data->func = sess_auth_rawntlmssp_negotiate;
1676 cifs_dbg(VFS, "secType %d not supported!\n", type);
1683 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1684 struct TCP_Server_Info *server,
1685 const struct nls_table *nls_cp)
1688 struct sess_data *sess_data;
1691 WARN(1, "%s: ses == NULL!", __func__);
1695 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1699 sess_data->xid = xid;
1700 sess_data->ses = ses;
1701 sess_data->server = server;
1702 sess_data->buf0_type = CIFS_NO_BUFFER;
1703 sess_data->nls_cp = (struct nls_table *) nls_cp;
1705 rc = select_sec(sess_data);
1709 while (sess_data->func)
1710 sess_data->func(sess_data);
1712 /* Store result before we free sess_data */
1713 rc = sess_data->result;