1 // SPDX-License-Identifier: LGPL-2.1
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)
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 */
85 cifs_dbg(VFS, "unable to get chan index for server: 0x%llx",
92 cifs_chan_set_in_reconnect(struct cifs_ses *ses,
93 struct TCP_Server_Info *server)
95 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
97 ses->chans[chan_index].in_reconnect = true;
101 cifs_chan_clear_in_reconnect(struct cifs_ses *ses,
102 struct TCP_Server_Info *server)
104 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
106 ses->chans[chan_index].in_reconnect = false;
110 cifs_chan_in_reconnect(struct cifs_ses *ses,
111 struct TCP_Server_Info *server)
113 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
115 return CIFS_CHAN_IN_RECONNECT(ses, chan_index);
119 cifs_chan_set_need_reconnect(struct cifs_ses *ses,
120 struct TCP_Server_Info *server)
122 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
124 set_bit(chan_index, &ses->chans_need_reconnect);
125 cifs_dbg(FYI, "Set reconnect bitmask for chan %u; now 0x%lx\n",
126 chan_index, ses->chans_need_reconnect);
130 cifs_chan_clear_need_reconnect(struct cifs_ses *ses,
131 struct TCP_Server_Info *server)
133 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
135 clear_bit(chan_index, &ses->chans_need_reconnect);
136 cifs_dbg(FYI, "Cleared reconnect bitmask for chan %u; now 0x%lx\n",
137 chan_index, ses->chans_need_reconnect);
141 cifs_chan_needs_reconnect(struct cifs_ses *ses,
142 struct TCP_Server_Info *server)
144 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
146 return CIFS_CHAN_NEEDS_RECONNECT(ses, chan_index);
149 /* returns number of channels added */
150 int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
152 int old_chan_count, new_chan_count;
157 struct cifs_server_iface *ifaces = NULL;
160 spin_lock(&ses->chan_lock);
162 new_chan_count = old_chan_count = ses->chan_count;
163 left = ses->chan_max - ses->chan_count;
166 spin_unlock(&ses->chan_lock);
168 "ses already at max_channels (%zu), nothing to open\n",
173 if (ses->server->dialect < SMB30_PROT_ID) {
174 spin_unlock(&ses->chan_lock);
175 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
179 if (!(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
181 spin_unlock(&ses->chan_lock);
182 cifs_dbg(VFS, "server %s does not support multichannel\n", ses->server->hostname);
185 spin_unlock(&ses->chan_lock);
188 * Make a copy of the iface list at the time and use that
189 * instead so as to not hold the iface spinlock for opening
192 spin_lock(&ses->iface_lock);
193 iface_count = ses->iface_count;
194 if (iface_count <= 0) {
195 spin_unlock(&ses->iface_lock);
196 cifs_dbg(VFS, "no iface list available to open channels\n");
199 ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces),
202 spin_unlock(&ses->iface_lock);
205 spin_unlock(&ses->iface_lock);
208 * Keep connecting to same, fastest, iface for all channels as
209 * long as its RSS. Try next fastest one if not RSS or channel
213 struct cifs_server_iface *iface;
216 if (tries > 3*ses->chan_max) {
217 cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
223 if (is_ses_using_iface(ses, iface) && !iface->rss_capable) {
224 i = (i+1) % iface_count;
228 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
230 cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n",
232 i = (i+1) % iface_count;
236 cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
243 return new_chan_count - old_chan_count;
247 * If server is a channel of ses, return the corresponding enclosing
248 * cifs_chan otherwise return NULL.
251 cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
255 spin_lock(&ses->chan_lock);
256 for (i = 0; i < ses->chan_count; i++) {
257 if (ses->chans[i].server == server) {
258 spin_unlock(&ses->chan_lock);
259 return &ses->chans[i];
262 spin_unlock(&ses->chan_lock);
267 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
268 struct cifs_server_iface *iface)
270 struct TCP_Server_Info *chan_server;
271 struct cifs_chan *chan;
272 struct smb3_fs_context ctx = {NULL};
273 static const char unc_fmt[] = "\\%s\\foo";
274 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
275 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
276 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
278 unsigned int xid = get_xid();
280 if (iface->sockaddr.ss_family == AF_INET)
281 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
282 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
285 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
286 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
290 * Setup a ctx with mostly the same info as the existing
291 * session and overwrite it with the requested iface data.
293 * We need to setup at least the fields used for negprot and
296 * We only need the ctx here, so we can reuse memory from
297 * the session and server without caring about memory
301 /* Always make new connection for now (TODO?) */
302 ctx.nosharesock = true;
305 ctx.domainauto = ses->domainAuto;
306 ctx.domainname = ses->domainName;
308 /* no hostname for extra channels */
309 ctx.server_hostname = "";
311 ctx.username = ses->user_name;
312 ctx.password = ses->password;
313 ctx.sectype = ses->sectype;
314 ctx.sign = ses->sign;
317 /* XXX: Use ses->server->hostname? */
318 sprintf(unc, unc_fmt, ses->ip_addr);
322 /* Reuse same version as master connection */
323 ctx.vals = ses->server->vals;
324 ctx.ops = ses->server->ops;
326 ctx.noblocksnd = ses->server->noblocksnd;
327 ctx.noautotune = ses->server->noautotune;
328 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
329 ctx.echo_interval = ses->server->echo_interval / HZ;
330 ctx.max_credits = ses->server->max_credits;
333 * This will be used for encoding/decoding user/domain/pw
334 * during sess setup auth.
336 ctx.local_nls = cifs_sb->local_nls;
338 /* Use RDMA if possible */
339 ctx.rdma = iface->rdma_capable;
340 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
342 /* reuse master con client guid */
343 memcpy(&ctx.client_guid, ses->server->client_guid,
344 SMB2_CLIENT_GUID_SIZE);
345 ctx.use_client_guid = true;
347 chan_server = cifs_get_tcp_session(&ctx, ses->server);
349 spin_lock(&ses->chan_lock);
350 chan = &ses->chans[ses->chan_count];
351 chan->server = chan_server;
352 if (IS_ERR(chan->server)) {
353 rc = PTR_ERR(chan->server);
355 spin_unlock(&ses->chan_lock);
359 atomic_set(&ses->chan_seq, 0);
361 /* Mark this channel as needing connect/setup */
362 cifs_chan_set_need_reconnect(ses, chan->server);
364 spin_unlock(&ses->chan_lock);
366 mutex_lock(&ses->session_mutex);
368 * We need to allocate the server crypto now as we will need
369 * to sign packets before we generate the channel signing key
370 * (we sign with the session key)
372 rc = smb311_crypto_shash_allocate(chan->server);
374 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
375 mutex_unlock(&ses->session_mutex);
379 rc = cifs_negotiate_protocol(xid, ses, chan->server);
381 rc = cifs_setup_session(xid, ses, chan->server, cifs_sb->local_nls);
383 mutex_unlock(&ses->session_mutex);
386 if (rc && chan->server) {
387 spin_lock(&ses->chan_lock);
388 /* we rely on all bits beyond chan_count to be clear */
389 cifs_chan_clear_need_reconnect(ses, chan->server);
392 * chan_count should never reach 0 as at least the primary
393 * channel is always allocated
395 WARN_ON(ses->chan_count < 1);
396 spin_unlock(&ses->chan_lock);
399 if (rc && chan->server)
400 cifs_put_tcp_session(chan->server, 0);
405 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses,
406 struct TCP_Server_Info *server,
407 SESSION_SETUP_ANDX *pSMB)
409 __u32 capabilities = 0;
411 /* init fields common to all four types of SessSetup */
412 /* Note that offsets for first seven fields in req struct are same */
413 /* in CIFS Specs so does not matter which of 3 forms of struct */
414 /* that we use in next few lines */
415 /* Note that header is initialized to zero in header_assemble */
416 pSMB->req.AndXCommand = 0xFF;
417 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
418 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
420 pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq);
421 pSMB->req.VcNumber = cpu_to_le16(1);
423 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
425 /* BB verify whether signing required on neg or just on auth frame
428 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
429 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
432 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
434 if (ses->capabilities & CAP_UNICODE) {
435 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
436 capabilities |= CAP_UNICODE;
438 if (ses->capabilities & CAP_STATUS32) {
439 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
440 capabilities |= CAP_STATUS32;
442 if (ses->capabilities & CAP_DFS) {
443 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
444 capabilities |= CAP_DFS;
446 if (ses->capabilities & CAP_UNIX)
447 capabilities |= CAP_UNIX;
453 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
455 char *bcc_ptr = *pbcc_area;
458 /* Copy OS version */
459 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
461 bcc_ptr += 2 * bytes_ret;
462 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
464 bcc_ptr += 2 * bytes_ret;
465 bcc_ptr += 2; /* trailing null */
467 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
469 bcc_ptr += 2 * bytes_ret;
470 bcc_ptr += 2; /* trailing null */
472 *pbcc_area = bcc_ptr;
475 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
476 const struct nls_table *nls_cp)
478 char *bcc_ptr = *pbcc_area;
482 if (ses->domainName == NULL) {
483 /* Sending null domain better than using a bogus domain name (as
484 we did briefly in 2.6.18) since server will use its default */
489 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
490 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
491 bcc_ptr += 2 * bytes_ret;
492 bcc_ptr += 2; /* account for null terminator */
494 *pbcc_area = bcc_ptr;
498 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
499 const struct nls_table *nls_cp)
501 char *bcc_ptr = *pbcc_area;
504 /* BB FIXME add check that strings total less
505 than 335 or will need to send them as arrays */
507 /* unicode strings, must be word aligned before the call */
508 /* if ((long) bcc_ptr % 2) {
513 if (ses->user_name == NULL) {
514 /* null user mount */
518 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
519 CIFS_MAX_USERNAME_LEN, nls_cp);
521 bcc_ptr += 2 * bytes_ret;
522 bcc_ptr += 2; /* account for null termination */
524 unicode_domain_string(&bcc_ptr, ses, nls_cp);
525 unicode_oslm_strings(&bcc_ptr, nls_cp);
527 *pbcc_area = bcc_ptr;
530 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
531 const struct nls_table *nls_cp)
533 char *bcc_ptr = *pbcc_area;
537 /* BB what about null user mounts - check that we do this BB */
539 if (ses->user_name != NULL) {
540 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
541 if (WARN_ON_ONCE(len < 0))
542 len = CIFS_MAX_USERNAME_LEN - 1;
545 /* else null user mount */
547 bcc_ptr++; /* account for null termination */
550 if (ses->domainName != NULL) {
551 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
552 if (WARN_ON_ONCE(len < 0))
553 len = CIFS_MAX_DOMAINNAME_LEN - 1;
555 } /* else we will send a null domain name
556 so the server will default to its own domain */
560 /* BB check for overflow here */
562 strcpy(bcc_ptr, "Linux version ");
563 bcc_ptr += strlen("Linux version ");
564 strcpy(bcc_ptr, init_utsname()->release);
565 bcc_ptr += strlen(init_utsname()->release) + 1;
567 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
568 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
570 *pbcc_area = bcc_ptr;
574 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
575 const struct nls_table *nls_cp)
578 char *data = *pbcc_area;
580 cifs_dbg(FYI, "bleft %d\n", bleft);
582 kfree(ses->serverOS);
583 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
584 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
585 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
591 kfree(ses->serverNOS);
592 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
593 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
594 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
600 kfree(ses->serverDomain);
601 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
602 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
607 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
608 struct cifs_ses *ses,
609 const struct nls_table *nls_cp)
612 char *bcc_ptr = *pbcc_area;
614 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
616 len = strnlen(bcc_ptr, bleft);
620 kfree(ses->serverOS);
622 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
624 memcpy(ses->serverOS, bcc_ptr, len);
625 ses->serverOS[len] = 0;
626 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
627 cifs_dbg(FYI, "OS/2 server\n");
633 len = strnlen(bcc_ptr, bleft);
637 kfree(ses->serverNOS);
639 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
640 if (ses->serverNOS) {
641 memcpy(ses->serverNOS, bcc_ptr, len);
642 ses->serverNOS[len] = 0;
648 len = strnlen(bcc_ptr, bleft);
652 /* No domain field in LANMAN case. Domain is
653 returned by old servers in the SMB negprot response */
654 /* BB For newer servers which do not support Unicode,
655 but thus do return domain here we could add parsing
656 for it later, but it is not very important */
657 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
660 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
661 struct cifs_ses *ses)
663 unsigned int tioffset; /* challenge message target info area */
664 unsigned int tilen; /* challenge message target info area length */
665 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
668 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
669 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
673 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
674 cifs_dbg(VFS, "blob signature incorrect %s\n",
678 if (pblob->MessageType != NtLmChallenge) {
679 cifs_dbg(VFS, "Incorrect message type %d\n",
684 server_flags = le32_to_cpu(pblob->NegotiateFlags);
685 cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__,
686 ses->ntlmssp->client_flags, server_flags);
688 if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) &&
689 (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) {
690 cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n",
694 if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) {
695 cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__);
698 if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) {
699 cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n",
703 if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
704 !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH))
705 pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n",
708 ses->ntlmssp->server_flags = server_flags;
710 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
711 /* In particular we can examine sign flags */
712 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
713 we must set the MIC field of the AUTHENTICATE_MESSAGE */
715 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
716 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
717 if (tioffset > blob_len || tioffset + tilen > blob_len) {
718 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
723 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
725 if (!ses->auth_key.response) {
726 cifs_dbg(VFS, "Challenge target info alloc failure\n");
729 ses->auth_key.len = tilen;
735 static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
737 int sz = base_size + ses->auth_key.len
738 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
741 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
743 sz += sizeof(__le16);
746 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
748 sz += sizeof(__le16);
750 if (ses->workstation_name[0])
751 sz += sizeof(__le16) * strnlen(ses->workstation_name,
752 ntlmssp_workstation_name_size(ses));
754 sz += sizeof(__le16);
759 static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
762 unsigned char *pstart,
763 unsigned char **pcur,
764 const struct nls_table *nls_cp)
766 unsigned char *tmp = pstart;
776 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
778 pbuf->MaximumLength = 0;
779 *pcur += sizeof(__le16);
781 len = cifs_strtoUTF16((__le16 *)*pcur,
785 len *= sizeof(__le16);
786 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
787 pbuf->Length = cpu_to_le16(len);
788 pbuf->MaximumLength = cpu_to_le16(len);
793 /* BB Move to ntlmssp.c eventually */
795 int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
797 struct cifs_ses *ses,
798 struct TCP_Server_Info *server,
799 const struct nls_table *nls_cp)
802 NEGOTIATE_MESSAGE *sec_blob;
807 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
808 *pbuffer = kmalloc(len, GFP_KERNEL);
811 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
813 goto setup_ntlm_neg_ret;
815 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
817 memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
818 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
819 sec_blob->MessageType = NtLmNegotiate;
821 /* BB is NTLMV2 session security format easier to use here? */
822 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
823 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
824 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
825 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
826 NTLMSSP_NEGOTIATE_SIGN;
827 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
828 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
830 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
831 ses->ntlmssp->client_flags = flags;
832 sec_blob->NegotiateFlags = cpu_to_le32(flags);
834 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
835 cifs_security_buffer_from_str(&sec_blob->DomainName,
837 CIFS_MAX_DOMAINNAME_LEN,
841 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
843 CIFS_MAX_WORKSTATION_LEN,
847 *buflen = tmp - *pbuffer;
853 * Build ntlmssp blob with additional fields, such as version,
854 * supported by modern servers. For safety limit to SMB3 or later
855 * See notes in MS-NLMP Section 2.2.2.1 e.g.
857 int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer,
859 struct cifs_ses *ses,
860 struct TCP_Server_Info *server,
861 const struct nls_table *nls_cp)
864 struct negotiate_message *sec_blob;
869 len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message));
870 *pbuffer = kmalloc(len, GFP_KERNEL);
873 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
875 goto setup_ntlm_smb3_neg_ret;
877 sec_blob = (struct negotiate_message *)*pbuffer;
879 memset(*pbuffer, 0, sizeof(struct negotiate_message));
880 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
881 sec_blob->MessageType = NtLmNegotiate;
883 /* BB is NTLMV2 session security format easier to use here? */
884 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
885 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
886 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
887 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
888 NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION;
889 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
890 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
892 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
893 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
894 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
895 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
897 tmp = *pbuffer + sizeof(struct negotiate_message);
898 ses->ntlmssp->client_flags = flags;
899 sec_blob->NegotiateFlags = cpu_to_le32(flags);
901 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
902 cifs_security_buffer_from_str(&sec_blob->DomainName,
904 CIFS_MAX_DOMAINNAME_LEN,
908 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
910 CIFS_MAX_WORKSTATION_LEN,
914 *buflen = tmp - *pbuffer;
915 setup_ntlm_smb3_neg_ret:
920 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
922 struct cifs_ses *ses,
923 struct TCP_Server_Info *server,
924 const struct nls_table *nls_cp)
927 AUTHENTICATE_MESSAGE *sec_blob;
932 rc = setup_ntlmv2_rsp(ses, nls_cp);
934 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
936 goto setup_ntlmv2_ret;
939 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
940 *pbuffer = kmalloc(len, GFP_KERNEL);
943 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
945 goto setup_ntlmv2_ret;
947 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
949 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
950 sec_blob->MessageType = NtLmAuthenticate;
952 flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET |
953 NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
955 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
956 sec_blob->NegotiateFlags = cpu_to_le32(flags);
958 sec_blob->LmChallengeResponse.BufferOffset =
959 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
960 sec_blob->LmChallengeResponse.Length = 0;
961 sec_blob->LmChallengeResponse.MaximumLength = 0;
963 sec_blob->NtChallengeResponse.BufferOffset =
964 cpu_to_le32(tmp - *pbuffer);
965 if (ses->user_name != NULL) {
966 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
967 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
968 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
970 sec_blob->NtChallengeResponse.Length =
971 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
972 sec_blob->NtChallengeResponse.MaximumLength =
973 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
976 * don't send an NT Response for anonymous access
978 sec_blob->NtChallengeResponse.Length = 0;
979 sec_blob->NtChallengeResponse.MaximumLength = 0;
982 cifs_security_buffer_from_str(&sec_blob->DomainName,
984 CIFS_MAX_DOMAINNAME_LEN,
988 cifs_security_buffer_from_str(&sec_blob->UserName,
990 CIFS_MAX_USERNAME_LEN,
994 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
995 ses->workstation_name,
996 ntlmssp_workstation_name_size(ses),
1000 if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
1001 (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) &&
1002 !calc_seckey(ses)) {
1003 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
1004 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1005 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
1006 sec_blob->SessionKey.MaximumLength =
1007 cpu_to_le16(CIFS_CPHTXT_SIZE);
1008 tmp += CIFS_CPHTXT_SIZE;
1010 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1011 sec_blob->SessionKey.Length = 0;
1012 sec_blob->SessionKey.MaximumLength = 0;
1015 *buflen = tmp - *pbuffer;
1021 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1023 switch (server->negflavor) {
1024 case CIFS_NEGFLAVOR_EXTENDED:
1025 switch (requested) {
1030 if (server->sec_ntlmssp &&
1031 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1033 if ((server->sec_kerberos || server->sec_mskerberos) &&
1034 (global_secflags & CIFSSEC_MAY_KRB5))
1040 case CIFS_NEGFLAVOR_UNENCAP:
1041 switch (requested) {
1045 if (global_secflags & CIFSSEC_MAY_NTLMV2)
1059 struct cifs_ses *ses;
1060 struct TCP_Server_Info *server;
1061 struct nls_table *nls_cp;
1062 void (*func)(struct sess_data *);
1065 /* we will send the SMB in three pieces:
1066 * a fixed length beginning part, an optional
1067 * SPNEGO blob (which can be zero length), and a
1068 * last part which will include the strings
1069 * and rest of bcc area. This allows us to avoid
1070 * a large buffer 17K allocation
1077 sess_alloc_buffer(struct sess_data *sess_data, int wct)
1080 struct cifs_ses *ses = sess_data->ses;
1081 struct smb_hdr *smb_buf;
1083 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
1089 sess_data->iov[0].iov_base = (char *)smb_buf;
1090 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
1092 * This variable will be used to clear the buffer
1093 * allocated above in case of any error in the calling function.
1095 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1097 /* 2000 big enough to fit max user, domain, NOS name etc. */
1098 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
1099 if (!sess_data->iov[2].iov_base) {
1101 goto out_free_smb_buf;
1107 cifs_small_buf_release(smb_buf);
1108 sess_data->iov[0].iov_base = NULL;
1109 sess_data->iov[0].iov_len = 0;
1110 sess_data->buf0_type = CIFS_NO_BUFFER;
1115 sess_free_buffer(struct sess_data *sess_data)
1118 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1119 sess_data->buf0_type = CIFS_NO_BUFFER;
1120 kfree(sess_data->iov[2].iov_base);
1124 sess_establish_session(struct sess_data *sess_data)
1126 struct cifs_ses *ses = sess_data->ses;
1127 struct TCP_Server_Info *server = sess_data->server;
1129 cifs_server_lock(server);
1130 if (!server->session_estab) {
1132 server->session_key.response =
1133 kmemdup(ses->auth_key.response,
1134 ses->auth_key.len, GFP_KERNEL);
1135 if (!server->session_key.response) {
1136 cifs_server_unlock(server);
1139 server->session_key.len =
1142 server->sequence_number = 0x2;
1143 server->session_estab = true;
1145 cifs_server_unlock(server);
1147 cifs_dbg(FYI, "CIFS session established successfully\n");
1152 sess_sendreceive(struct sess_data *sess_data)
1155 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
1157 struct kvec rsp_iov = { NULL, 0 };
1159 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
1160 be32_add_cpu(&smb_buf->smb_buf_length, count);
1161 put_bcc(count, smb_buf);
1163 rc = SendReceive2(sess_data->xid, sess_data->ses,
1164 sess_data->iov, 3 /* num_iovecs */,
1165 &sess_data->buf0_type,
1166 CIFS_LOG_ERROR, &rsp_iov);
1167 cifs_small_buf_release(sess_data->iov[0].iov_base);
1168 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1174 sess_auth_ntlmv2(struct sess_data *sess_data)
1177 struct smb_hdr *smb_buf;
1178 SESSION_SETUP_ANDX *pSMB;
1180 struct cifs_ses *ses = sess_data->ses;
1181 struct TCP_Server_Info *server = sess_data->server;
1183 __u16 bytes_remaining;
1185 /* old style NTLM sessionsetup */
1187 rc = sess_alloc_buffer(sess_data, 13);
1191 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1192 bcc_ptr = sess_data->iov[2].iov_base;
1193 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1195 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1197 /* LM2 password would be here if we supported it */
1198 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1200 if (ses->user_name != NULL) {
1201 /* calculate nlmv2 response and session key */
1202 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1204 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1208 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1209 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1210 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1212 /* set case sensitive password length after tilen may get
1213 * assigned, tilen is 0 otherwise.
1215 pSMB->req_no_secext.CaseSensitivePasswordLength =
1216 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1218 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1221 if (ses->capabilities & CAP_UNICODE) {
1222 if (sess_data->iov[0].iov_len % 2) {
1226 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1228 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1232 sess_data->iov[2].iov_len = (long) bcc_ptr -
1233 (long) sess_data->iov[2].iov_base;
1235 rc = sess_sendreceive(sess_data);
1239 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1240 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1242 if (smb_buf->WordCount != 3) {
1244 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1248 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1249 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1251 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1252 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1254 bytes_remaining = get_bcc(smb_buf);
1255 bcc_ptr = pByteArea(smb_buf);
1257 /* BB check if Unicode and decode strings */
1258 if (bytes_remaining == 0) {
1259 /* no string area to decode, do nothing */
1260 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1261 /* unicode string area must be word-aligned */
1262 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1266 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1269 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1273 rc = sess_establish_session(sess_data);
1275 sess_data->result = rc;
1276 sess_data->func = NULL;
1277 sess_free_buffer(sess_data);
1278 kfree(ses->auth_key.response);
1279 ses->auth_key.response = NULL;
1282 #ifdef CONFIG_CIFS_UPCALL
1284 sess_auth_kerberos(struct sess_data *sess_data)
1287 struct smb_hdr *smb_buf;
1288 SESSION_SETUP_ANDX *pSMB;
1290 struct cifs_ses *ses = sess_data->ses;
1291 struct TCP_Server_Info *server = sess_data->server;
1293 __u16 bytes_remaining;
1294 struct key *spnego_key = NULL;
1295 struct cifs_spnego_msg *msg;
1298 /* extended security */
1300 rc = sess_alloc_buffer(sess_data, 12);
1304 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1305 bcc_ptr = sess_data->iov[2].iov_base;
1306 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1308 spnego_key = cifs_get_spnego_key(ses, server);
1309 if (IS_ERR(spnego_key)) {
1310 rc = PTR_ERR(spnego_key);
1315 msg = spnego_key->payload.data[0];
1317 * check version field to make sure that cifs.upcall is
1318 * sending us a response in an expected form
1320 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1321 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1322 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1324 goto out_put_spnego_key;
1327 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1329 if (!ses->auth_key.response) {
1330 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1333 goto out_put_spnego_key;
1335 ses->auth_key.len = msg->sesskey_len;
1337 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1338 capabilities |= CAP_EXTENDED_SECURITY;
1339 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1340 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1341 sess_data->iov[1].iov_len = msg->secblob_len;
1342 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1344 if (ses->capabilities & CAP_UNICODE) {
1345 /* unicode strings must be word aligned */
1346 if ((sess_data->iov[0].iov_len
1347 + sess_data->iov[1].iov_len) % 2) {
1351 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1352 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1354 /* BB: is this right? */
1355 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1358 sess_data->iov[2].iov_len = (long) bcc_ptr -
1359 (long) sess_data->iov[2].iov_base;
1361 rc = sess_sendreceive(sess_data);
1363 goto out_put_spnego_key;
1365 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1366 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1368 if (smb_buf->WordCount != 4) {
1370 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1371 goto out_put_spnego_key;
1374 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1375 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1377 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1378 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1380 bytes_remaining = get_bcc(smb_buf);
1381 bcc_ptr = pByteArea(smb_buf);
1383 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1384 if (blob_len > bytes_remaining) {
1385 cifs_dbg(VFS, "bad security blob length %d\n",
1388 goto out_put_spnego_key;
1390 bcc_ptr += blob_len;
1391 bytes_remaining -= blob_len;
1393 /* BB check if Unicode and decode strings */
1394 if (bytes_remaining == 0) {
1395 /* no string area to decode, do nothing */
1396 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1397 /* unicode string area must be word-aligned */
1398 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1402 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1405 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1409 rc = sess_establish_session(sess_data);
1411 key_invalidate(spnego_key);
1412 key_put(spnego_key);
1414 sess_data->result = rc;
1415 sess_data->func = NULL;
1416 sess_free_buffer(sess_data);
1417 kfree(ses->auth_key.response);
1418 ses->auth_key.response = NULL;
1421 #endif /* ! CONFIG_CIFS_UPCALL */
1424 * The required kvec buffers have to be allocated before calling this
1428 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1430 SESSION_SETUP_ANDX *pSMB;
1431 struct cifs_ses *ses = sess_data->ses;
1432 struct TCP_Server_Info *server = sess_data->server;
1436 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1438 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1439 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1440 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1444 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1445 capabilities |= CAP_EXTENDED_SECURITY;
1446 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1448 bcc_ptr = sess_data->iov[2].iov_base;
1449 /* unicode strings must be word aligned */
1450 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1454 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1456 sess_data->iov[2].iov_len = (long) bcc_ptr -
1457 (long) sess_data->iov[2].iov_base;
1463 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1466 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1469 struct smb_hdr *smb_buf;
1470 SESSION_SETUP_ANDX *pSMB;
1471 struct cifs_ses *ses = sess_data->ses;
1472 struct TCP_Server_Info *server = sess_data->server;
1473 __u16 bytes_remaining;
1475 unsigned char *ntlmsspblob = NULL;
1478 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1481 * if memory allocation is successful, caller of this function
1484 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1485 if (!ses->ntlmssp) {
1489 ses->ntlmssp->sesskey_per_smbsess = false;
1492 rc = sess_alloc_buffer(sess_data, 12);
1496 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1498 /* Build security blob before we assemble the request */
1499 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
1500 &blob_len, ses, server,
1503 goto out_free_ntlmsspblob;
1505 sess_data->iov[1].iov_len = blob_len;
1506 sess_data->iov[1].iov_base = ntlmsspblob;
1507 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1509 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1511 goto out_free_ntlmsspblob;
1513 rc = sess_sendreceive(sess_data);
1515 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1516 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1518 /* If true, rc here is expected and not an error */
1519 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1520 smb_buf->Status.CifsError ==
1521 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1525 goto out_free_ntlmsspblob;
1527 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1529 if (smb_buf->WordCount != 4) {
1531 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1532 goto out_free_ntlmsspblob;
1535 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1536 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1538 bytes_remaining = get_bcc(smb_buf);
1539 bcc_ptr = pByteArea(smb_buf);
1541 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1542 if (blob_len > bytes_remaining) {
1543 cifs_dbg(VFS, "bad security blob length %d\n",
1546 goto out_free_ntlmsspblob;
1549 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1551 out_free_ntlmsspblob:
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 struct TCP_Server_Info *server = sess_data->server;
1579 __u16 bytes_remaining;
1581 unsigned char *ntlmsspblob = NULL;
1584 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1587 rc = sess_alloc_buffer(sess_data, 12);
1591 /* Build security blob before we assemble the request */
1592 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1593 smb_buf = (struct smb_hdr *)pSMB;
1594 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1595 &blob_len, ses, server,
1598 goto out_free_ntlmsspblob;
1599 sess_data->iov[1].iov_len = blob_len;
1600 sess_data->iov[1].iov_base = ntlmsspblob;
1601 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1603 * Make sure that we tell the server that we are using
1604 * the uid that it just gave us back on the response
1607 smb_buf->Uid = ses->Suid;
1609 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1611 goto out_free_ntlmsspblob;
1613 rc = sess_sendreceive(sess_data);
1615 goto out_free_ntlmsspblob;
1617 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1618 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1619 if (smb_buf->WordCount != 4) {
1621 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1622 goto out_free_ntlmsspblob;
1625 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1626 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1628 if (ses->Suid != smb_buf->Uid) {
1629 ses->Suid = smb_buf->Uid;
1630 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1633 bytes_remaining = get_bcc(smb_buf);
1634 bcc_ptr = pByteArea(smb_buf);
1635 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1636 if (blob_len > bytes_remaining) {
1637 cifs_dbg(VFS, "bad security blob length %d\n",
1640 goto out_free_ntlmsspblob;
1642 bcc_ptr += blob_len;
1643 bytes_remaining -= blob_len;
1646 /* BB check if Unicode and decode strings */
1647 if (bytes_remaining == 0) {
1648 /* no string area to decode, do nothing */
1649 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1650 /* unicode string area must be word-aligned */
1651 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1655 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1658 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1662 out_free_ntlmsspblob:
1665 sess_free_buffer(sess_data);
1668 rc = sess_establish_session(sess_data);
1671 kfree(ses->auth_key.response);
1672 ses->auth_key.response = NULL;
1673 kfree(ses->ntlmssp);
1674 ses->ntlmssp = NULL;
1676 sess_data->func = NULL;
1677 sess_data->result = rc;
1680 static int select_sec(struct sess_data *sess_data)
1683 struct cifs_ses *ses = sess_data->ses;
1684 struct TCP_Server_Info *server = sess_data->server;
1686 type = cifs_select_sectype(server, ses->sectype);
1687 cifs_dbg(FYI, "sess setup type %d\n", type);
1688 if (type == Unspecified) {
1689 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1695 sess_data->func = sess_auth_ntlmv2;
1698 #ifdef CONFIG_CIFS_UPCALL
1699 sess_data->func = sess_auth_kerberos;
1702 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1704 #endif /* CONFIG_CIFS_UPCALL */
1706 sess_data->func = sess_auth_rawntlmssp_negotiate;
1709 cifs_dbg(VFS, "secType %d not supported!\n", type);
1716 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1717 struct TCP_Server_Info *server,
1718 const struct nls_table *nls_cp)
1721 struct sess_data *sess_data;
1724 WARN(1, "%s: ses == NULL!", __func__);
1728 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1732 sess_data->xid = xid;
1733 sess_data->ses = ses;
1734 sess_data->server = server;
1735 sess_data->buf0_type = CIFS_NO_BUFFER;
1736 sess_data->nls_cp = (struct nls_table *) nls_cp;
1738 rc = select_sec(sess_data);
1742 while (sess_data->func)
1743 sess_data->func(sess_data);
1745 /* Store result before we free sess_data */
1746 rc = sess_data->result;