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_in_reconnect(struct cifs_ses *ses,
90 struct TCP_Server_Info *server)
92 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
94 ses->chans[chan_index].in_reconnect = true;
98 cifs_chan_clear_in_reconnect(struct cifs_ses *ses,
99 struct TCP_Server_Info *server)
101 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
103 ses->chans[chan_index].in_reconnect = false;
107 cifs_chan_in_reconnect(struct cifs_ses *ses,
108 struct TCP_Server_Info *server)
110 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
112 return CIFS_CHAN_IN_RECONNECT(ses, chan_index);
116 cifs_chan_set_need_reconnect(struct cifs_ses *ses,
117 struct TCP_Server_Info *server)
119 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
121 set_bit(chan_index, &ses->chans_need_reconnect);
122 cifs_dbg(FYI, "Set reconnect bitmask for chan %u; now 0x%lx\n",
123 chan_index, ses->chans_need_reconnect);
127 cifs_chan_clear_need_reconnect(struct cifs_ses *ses,
128 struct TCP_Server_Info *server)
130 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
132 clear_bit(chan_index, &ses->chans_need_reconnect);
133 cifs_dbg(FYI, "Cleared reconnect bitmask for chan %u; now 0x%lx\n",
134 chan_index, ses->chans_need_reconnect);
138 cifs_chan_needs_reconnect(struct cifs_ses *ses,
139 struct TCP_Server_Info *server)
141 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
143 return CIFS_CHAN_NEEDS_RECONNECT(ses, chan_index);
146 /* returns number of channels added */
147 int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
149 int old_chan_count, new_chan_count;
154 struct cifs_server_iface *ifaces = NULL;
157 spin_lock(&ses->chan_lock);
159 new_chan_count = old_chan_count = ses->chan_count;
160 left = ses->chan_max - ses->chan_count;
163 spin_unlock(&ses->chan_lock);
165 "ses already at max_channels (%zu), nothing to open\n",
170 if (ses->server->dialect < SMB30_PROT_ID) {
171 spin_unlock(&ses->chan_lock);
172 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
176 if (!(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
178 spin_unlock(&ses->chan_lock);
179 cifs_dbg(VFS, "server %s does not support multichannel\n", ses->server->hostname);
182 spin_unlock(&ses->chan_lock);
185 * Make a copy of the iface list at the time and use that
186 * instead so as to not hold the iface spinlock for opening
189 spin_lock(&ses->iface_lock);
190 iface_count = ses->iface_count;
191 if (iface_count <= 0) {
192 spin_unlock(&ses->iface_lock);
193 cifs_dbg(VFS, "no iface list available to open channels\n");
196 ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces),
199 spin_unlock(&ses->iface_lock);
202 spin_unlock(&ses->iface_lock);
205 * Keep connecting to same, fastest, iface for all channels as
206 * long as its RSS. Try next fastest one if not RSS or channel
210 struct cifs_server_iface *iface;
213 if (tries > 3*ses->chan_max) {
214 cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
220 if (is_ses_using_iface(ses, iface) && !iface->rss_capable) {
221 i = (i+1) % iface_count;
225 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
227 cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n",
229 i = (i+1) % iface_count;
233 cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
240 return new_chan_count - old_chan_count;
244 * If server is a channel of ses, return the corresponding enclosing
245 * cifs_chan otherwise return NULL.
248 cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
252 spin_lock(&ses->chan_lock);
253 for (i = 0; i < ses->chan_count; i++) {
254 if (ses->chans[i].server == server) {
255 spin_unlock(&ses->chan_lock);
256 return &ses->chans[i];
259 spin_unlock(&ses->chan_lock);
264 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
265 struct cifs_server_iface *iface)
267 struct TCP_Server_Info *chan_server;
268 struct cifs_chan *chan;
269 struct smb3_fs_context ctx = {NULL};
270 static const char unc_fmt[] = "\\%s\\foo";
271 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
272 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
273 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
275 unsigned int xid = get_xid();
277 if (iface->sockaddr.ss_family == AF_INET)
278 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
279 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
282 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
283 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
287 * Setup a ctx with mostly the same info as the existing
288 * session and overwrite it with the requested iface data.
290 * We need to setup at least the fields used for negprot and
293 * We only need the ctx here, so we can reuse memory from
294 * the session and server without caring about memory
298 /* Always make new connection for now (TODO?) */
299 ctx.nosharesock = true;
302 ctx.domainauto = ses->domainAuto;
303 ctx.domainname = ses->domainName;
304 ctx.server_hostname = ses->server->hostname;
305 ctx.username = ses->user_name;
306 ctx.password = ses->password;
307 ctx.sectype = ses->sectype;
308 ctx.sign = ses->sign;
311 /* XXX: Use ses->server->hostname? */
312 sprintf(unc, unc_fmt, ses->ip_addr);
316 /* Reuse same version as master connection */
317 ctx.vals = ses->server->vals;
318 ctx.ops = ses->server->ops;
320 ctx.noblocksnd = ses->server->noblocksnd;
321 ctx.noautotune = ses->server->noautotune;
322 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
323 ctx.echo_interval = ses->server->echo_interval / HZ;
324 ctx.max_credits = ses->server->max_credits;
327 * This will be used for encoding/decoding user/domain/pw
328 * during sess setup auth.
330 ctx.local_nls = cifs_sb->local_nls;
332 /* Use RDMA if possible */
333 ctx.rdma = iface->rdma_capable;
334 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
336 /* reuse master con client guid */
337 memcpy(&ctx.client_guid, ses->server->client_guid,
338 SMB2_CLIENT_GUID_SIZE);
339 ctx.use_client_guid = true;
341 chan_server = cifs_get_tcp_session(&ctx, ses->server);
343 spin_lock(&ses->chan_lock);
344 chan = &ses->chans[ses->chan_count];
345 chan->server = chan_server;
346 if (IS_ERR(chan->server)) {
347 rc = PTR_ERR(chan->server);
349 spin_unlock(&ses->chan_lock);
353 atomic_set(&ses->chan_seq, 0);
355 /* Mark this channel as needing connect/setup */
356 cifs_chan_set_need_reconnect(ses, chan->server);
358 spin_unlock(&ses->chan_lock);
360 mutex_lock(&ses->session_mutex);
362 * We need to allocate the server crypto now as we will need
363 * to sign packets before we generate the channel signing key
364 * (we sign with the session key)
366 rc = smb311_crypto_shash_allocate(chan->server);
368 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
369 mutex_unlock(&ses->session_mutex);
373 rc = cifs_negotiate_protocol(xid, ses, chan->server);
375 rc = cifs_setup_session(xid, ses, chan->server, cifs_sb->local_nls);
377 mutex_unlock(&ses->session_mutex);
380 if (rc && chan->server) {
381 spin_lock(&ses->chan_lock);
382 /* we rely on all bits beyond chan_count to be clear */
383 cifs_chan_clear_need_reconnect(ses, chan->server);
386 * chan_count should never reach 0 as at least the primary
387 * channel is always allocated
389 WARN_ON(ses->chan_count < 1);
390 spin_unlock(&ses->chan_lock);
393 if (rc && chan->server)
394 cifs_put_tcp_session(chan->server, 0);
399 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses,
400 struct TCP_Server_Info *server,
401 SESSION_SETUP_ANDX *pSMB)
403 __u32 capabilities = 0;
405 /* init fields common to all four types of SessSetup */
406 /* Note that offsets for first seven fields in req struct are same */
407 /* in CIFS Specs so does not matter which of 3 forms of struct */
408 /* that we use in next few lines */
409 /* Note that header is initialized to zero in header_assemble */
410 pSMB->req.AndXCommand = 0xFF;
411 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
412 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
414 pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq);
415 pSMB->req.VcNumber = cpu_to_le16(1);
417 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
419 /* BB verify whether signing required on neg or just on auth frame
422 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
423 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
426 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
428 if (ses->capabilities & CAP_UNICODE) {
429 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
430 capabilities |= CAP_UNICODE;
432 if (ses->capabilities & CAP_STATUS32) {
433 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
434 capabilities |= CAP_STATUS32;
436 if (ses->capabilities & CAP_DFS) {
437 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
438 capabilities |= CAP_DFS;
440 if (ses->capabilities & CAP_UNIX)
441 capabilities |= CAP_UNIX;
447 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
449 char *bcc_ptr = *pbcc_area;
452 /* Copy OS version */
453 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
455 bcc_ptr += 2 * bytes_ret;
456 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
458 bcc_ptr += 2 * bytes_ret;
459 bcc_ptr += 2; /* trailing null */
461 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
463 bcc_ptr += 2 * bytes_ret;
464 bcc_ptr += 2; /* trailing null */
466 *pbcc_area = bcc_ptr;
469 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
470 const struct nls_table *nls_cp)
472 char *bcc_ptr = *pbcc_area;
476 if (ses->domainName == NULL) {
477 /* Sending null domain better than using a bogus domain name (as
478 we did briefly in 2.6.18) since server will use its default */
483 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
484 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
485 bcc_ptr += 2 * bytes_ret;
486 bcc_ptr += 2; /* account for null terminator */
488 *pbcc_area = bcc_ptr;
492 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
493 const struct nls_table *nls_cp)
495 char *bcc_ptr = *pbcc_area;
498 /* BB FIXME add check that strings total less
499 than 335 or will need to send them as arrays */
501 /* unicode strings, must be word aligned before the call */
502 /* if ((long) bcc_ptr % 2) {
507 if (ses->user_name == NULL) {
508 /* null user mount */
512 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
513 CIFS_MAX_USERNAME_LEN, nls_cp);
515 bcc_ptr += 2 * bytes_ret;
516 bcc_ptr += 2; /* account for null termination */
518 unicode_domain_string(&bcc_ptr, ses, nls_cp);
519 unicode_oslm_strings(&bcc_ptr, nls_cp);
521 *pbcc_area = bcc_ptr;
524 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
525 const struct nls_table *nls_cp)
527 char *bcc_ptr = *pbcc_area;
531 /* BB what about null user mounts - check that we do this BB */
533 if (ses->user_name != NULL) {
534 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
535 if (WARN_ON_ONCE(len < 0))
536 len = CIFS_MAX_USERNAME_LEN - 1;
539 /* else null user mount */
541 bcc_ptr++; /* account for null termination */
544 if (ses->domainName != NULL) {
545 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
546 if (WARN_ON_ONCE(len < 0))
547 len = CIFS_MAX_DOMAINNAME_LEN - 1;
549 } /* else we will send a null domain name
550 so the server will default to its own domain */
554 /* BB check for overflow here */
556 strcpy(bcc_ptr, "Linux version ");
557 bcc_ptr += strlen("Linux version ");
558 strcpy(bcc_ptr, init_utsname()->release);
559 bcc_ptr += strlen(init_utsname()->release) + 1;
561 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
562 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
564 *pbcc_area = bcc_ptr;
568 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
569 const struct nls_table *nls_cp)
572 char *data = *pbcc_area;
574 cifs_dbg(FYI, "bleft %d\n", bleft);
576 kfree(ses->serverOS);
577 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
578 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
579 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
585 kfree(ses->serverNOS);
586 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
587 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
588 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
594 kfree(ses->serverDomain);
595 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
596 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
601 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
602 struct cifs_ses *ses,
603 const struct nls_table *nls_cp)
606 char *bcc_ptr = *pbcc_area;
608 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
610 len = strnlen(bcc_ptr, bleft);
614 kfree(ses->serverOS);
616 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
618 memcpy(ses->serverOS, bcc_ptr, len);
619 ses->serverOS[len] = 0;
620 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
621 cifs_dbg(FYI, "OS/2 server\n");
627 len = strnlen(bcc_ptr, bleft);
631 kfree(ses->serverNOS);
633 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
634 if (ses->serverNOS) {
635 memcpy(ses->serverNOS, bcc_ptr, len);
636 ses->serverNOS[len] = 0;
642 len = strnlen(bcc_ptr, bleft);
646 /* No domain field in LANMAN case. Domain is
647 returned by old servers in the SMB negprot response */
648 /* BB For newer servers which do not support Unicode,
649 but thus do return domain here we could add parsing
650 for it later, but it is not very important */
651 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
654 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
655 struct cifs_ses *ses)
657 unsigned int tioffset; /* challenge message target info area */
658 unsigned int tilen; /* challenge message target info area length */
659 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
662 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
663 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
667 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
668 cifs_dbg(VFS, "blob signature incorrect %s\n",
672 if (pblob->MessageType != NtLmChallenge) {
673 cifs_dbg(VFS, "Incorrect message type %d\n",
678 server_flags = le32_to_cpu(pblob->NegotiateFlags);
679 cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__,
680 ses->ntlmssp->client_flags, server_flags);
682 if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) &&
683 (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) {
684 cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n",
688 if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) {
689 cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__);
692 if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) {
693 cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n",
697 if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
698 !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH))
699 pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n",
702 ses->ntlmssp->server_flags = server_flags;
704 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
705 /* In particular we can examine sign flags */
706 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
707 we must set the MIC field of the AUTHENTICATE_MESSAGE */
709 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
710 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
711 if (tioffset > blob_len || tioffset + tilen > blob_len) {
712 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
717 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
719 if (!ses->auth_key.response) {
720 cifs_dbg(VFS, "Challenge target info alloc failure\n");
723 ses->auth_key.len = tilen;
729 static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
731 int sz = base_size + ses->auth_key.len
732 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
735 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
737 sz += sizeof(__le16);
740 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
742 sz += sizeof(__le16);
744 if (ses->workstation_name[0])
745 sz += sizeof(__le16) * strnlen(ses->workstation_name,
746 ntlmssp_workstation_name_size(ses));
748 sz += sizeof(__le16);
753 static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
756 unsigned char *pstart,
757 unsigned char **pcur,
758 const struct nls_table *nls_cp)
760 unsigned char *tmp = pstart;
770 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
772 pbuf->MaximumLength = 0;
773 *pcur += sizeof(__le16);
775 len = cifs_strtoUTF16((__le16 *)*pcur,
779 len *= sizeof(__le16);
780 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
781 pbuf->Length = cpu_to_le16(len);
782 pbuf->MaximumLength = cpu_to_le16(len);
787 /* BB Move to ntlmssp.c eventually */
789 int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
791 struct cifs_ses *ses,
792 struct TCP_Server_Info *server,
793 const struct nls_table *nls_cp)
796 NEGOTIATE_MESSAGE *sec_blob;
801 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
802 *pbuffer = kmalloc(len, GFP_KERNEL);
805 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
807 goto setup_ntlm_neg_ret;
809 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
811 memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
812 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
813 sec_blob->MessageType = NtLmNegotiate;
815 /* BB is NTLMV2 session security format easier to use here? */
816 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
817 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
818 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
819 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
820 NTLMSSP_NEGOTIATE_SIGN;
821 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
822 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
824 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
825 ses->ntlmssp->client_flags = flags;
826 sec_blob->NegotiateFlags = cpu_to_le32(flags);
828 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
829 cifs_security_buffer_from_str(&sec_blob->DomainName,
831 CIFS_MAX_DOMAINNAME_LEN,
835 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
837 CIFS_MAX_WORKSTATION_LEN,
841 *buflen = tmp - *pbuffer;
847 * Build ntlmssp blob with additional fields, such as version,
848 * supported by modern servers. For safety limit to SMB3 or later
849 * See notes in MS-NLMP Section 2.2.2.1 e.g.
851 int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer,
853 struct cifs_ses *ses,
854 struct TCP_Server_Info *server,
855 const struct nls_table *nls_cp)
858 struct negotiate_message *sec_blob;
863 len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message));
864 *pbuffer = kmalloc(len, GFP_KERNEL);
867 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
869 goto setup_ntlm_smb3_neg_ret;
871 sec_blob = (struct negotiate_message *)*pbuffer;
873 memset(*pbuffer, 0, sizeof(struct negotiate_message));
874 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
875 sec_blob->MessageType = NtLmNegotiate;
877 /* BB is NTLMV2 session security format easier to use here? */
878 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
879 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
880 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
881 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
882 NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION;
883 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
884 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
886 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
887 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
888 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
889 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
891 tmp = *pbuffer + sizeof(struct negotiate_message);
892 ses->ntlmssp->client_flags = flags;
893 sec_blob->NegotiateFlags = cpu_to_le32(flags);
895 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
896 cifs_security_buffer_from_str(&sec_blob->DomainName,
898 CIFS_MAX_DOMAINNAME_LEN,
902 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
904 CIFS_MAX_WORKSTATION_LEN,
908 *buflen = tmp - *pbuffer;
909 setup_ntlm_smb3_neg_ret:
914 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
916 struct cifs_ses *ses,
917 struct TCP_Server_Info *server,
918 const struct nls_table *nls_cp)
921 AUTHENTICATE_MESSAGE *sec_blob;
926 rc = setup_ntlmv2_rsp(ses, nls_cp);
928 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
930 goto setup_ntlmv2_ret;
933 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
934 *pbuffer = kmalloc(len, GFP_KERNEL);
937 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
939 goto setup_ntlmv2_ret;
941 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
943 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
944 sec_blob->MessageType = NtLmAuthenticate;
946 flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET |
947 NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
949 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
950 sec_blob->NegotiateFlags = cpu_to_le32(flags);
952 sec_blob->LmChallengeResponse.BufferOffset =
953 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
954 sec_blob->LmChallengeResponse.Length = 0;
955 sec_blob->LmChallengeResponse.MaximumLength = 0;
957 sec_blob->NtChallengeResponse.BufferOffset =
958 cpu_to_le32(tmp - *pbuffer);
959 if (ses->user_name != NULL) {
960 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
961 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
962 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
964 sec_blob->NtChallengeResponse.Length =
965 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
966 sec_blob->NtChallengeResponse.MaximumLength =
967 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
970 * don't send an NT Response for anonymous access
972 sec_blob->NtChallengeResponse.Length = 0;
973 sec_blob->NtChallengeResponse.MaximumLength = 0;
976 cifs_security_buffer_from_str(&sec_blob->DomainName,
978 CIFS_MAX_DOMAINNAME_LEN,
982 cifs_security_buffer_from_str(&sec_blob->UserName,
984 CIFS_MAX_USERNAME_LEN,
988 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
989 ses->workstation_name,
990 ntlmssp_workstation_name_size(ses),
994 if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
995 (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) &&
997 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
998 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
999 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
1000 sec_blob->SessionKey.MaximumLength =
1001 cpu_to_le16(CIFS_CPHTXT_SIZE);
1002 tmp += CIFS_CPHTXT_SIZE;
1004 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1005 sec_blob->SessionKey.Length = 0;
1006 sec_blob->SessionKey.MaximumLength = 0;
1009 *buflen = tmp - *pbuffer;
1015 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1017 switch (server->negflavor) {
1018 case CIFS_NEGFLAVOR_EXTENDED:
1019 switch (requested) {
1024 if (server->sec_ntlmssp &&
1025 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1027 if ((server->sec_kerberos || server->sec_mskerberos) &&
1028 (global_secflags & CIFSSEC_MAY_KRB5))
1034 case CIFS_NEGFLAVOR_UNENCAP:
1035 switch (requested) {
1039 if (global_secflags & CIFSSEC_MAY_NTLMV2)
1053 struct cifs_ses *ses;
1054 struct TCP_Server_Info *server;
1055 struct nls_table *nls_cp;
1056 void (*func)(struct sess_data *);
1059 /* we will send the SMB in three pieces:
1060 * a fixed length beginning part, an optional
1061 * SPNEGO blob (which can be zero length), and a
1062 * last part which will include the strings
1063 * and rest of bcc area. This allows us to avoid
1064 * a large buffer 17K allocation
1071 sess_alloc_buffer(struct sess_data *sess_data, int wct)
1074 struct cifs_ses *ses = sess_data->ses;
1075 struct smb_hdr *smb_buf;
1077 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
1083 sess_data->iov[0].iov_base = (char *)smb_buf;
1084 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
1086 * This variable will be used to clear the buffer
1087 * allocated above in case of any error in the calling function.
1089 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1091 /* 2000 big enough to fit max user, domain, NOS name etc. */
1092 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
1093 if (!sess_data->iov[2].iov_base) {
1095 goto out_free_smb_buf;
1101 cifs_small_buf_release(smb_buf);
1102 sess_data->iov[0].iov_base = NULL;
1103 sess_data->iov[0].iov_len = 0;
1104 sess_data->buf0_type = CIFS_NO_BUFFER;
1109 sess_free_buffer(struct sess_data *sess_data)
1112 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1113 sess_data->buf0_type = CIFS_NO_BUFFER;
1114 kfree(sess_data->iov[2].iov_base);
1118 sess_establish_session(struct sess_data *sess_data)
1120 struct cifs_ses *ses = sess_data->ses;
1121 struct TCP_Server_Info *server = sess_data->server;
1123 cifs_server_lock(server);
1124 if (!server->session_estab) {
1126 server->session_key.response =
1127 kmemdup(ses->auth_key.response,
1128 ses->auth_key.len, GFP_KERNEL);
1129 if (!server->session_key.response) {
1130 cifs_server_unlock(server);
1133 server->session_key.len =
1136 server->sequence_number = 0x2;
1137 server->session_estab = true;
1139 cifs_server_unlock(server);
1141 cifs_dbg(FYI, "CIFS session established successfully\n");
1146 sess_sendreceive(struct sess_data *sess_data)
1149 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
1151 struct kvec rsp_iov = { NULL, 0 };
1153 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
1154 be32_add_cpu(&smb_buf->smb_buf_length, count);
1155 put_bcc(count, smb_buf);
1157 rc = SendReceive2(sess_data->xid, sess_data->ses,
1158 sess_data->iov, 3 /* num_iovecs */,
1159 &sess_data->buf0_type,
1160 CIFS_LOG_ERROR, &rsp_iov);
1161 cifs_small_buf_release(sess_data->iov[0].iov_base);
1162 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1168 sess_auth_ntlmv2(struct sess_data *sess_data)
1171 struct smb_hdr *smb_buf;
1172 SESSION_SETUP_ANDX *pSMB;
1174 struct cifs_ses *ses = sess_data->ses;
1175 struct TCP_Server_Info *server = sess_data->server;
1177 __u16 bytes_remaining;
1179 /* old style NTLM sessionsetup */
1181 rc = sess_alloc_buffer(sess_data, 13);
1185 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1186 bcc_ptr = sess_data->iov[2].iov_base;
1187 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1189 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1191 /* LM2 password would be here if we supported it */
1192 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1194 if (ses->user_name != NULL) {
1195 /* calculate nlmv2 response and session key */
1196 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1198 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1202 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1203 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1204 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1206 /* set case sensitive password length after tilen may get
1207 * assigned, tilen is 0 otherwise.
1209 pSMB->req_no_secext.CaseSensitivePasswordLength =
1210 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1212 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1215 if (ses->capabilities & CAP_UNICODE) {
1216 if (sess_data->iov[0].iov_len % 2) {
1220 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1222 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1226 sess_data->iov[2].iov_len = (long) bcc_ptr -
1227 (long) sess_data->iov[2].iov_base;
1229 rc = sess_sendreceive(sess_data);
1233 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1234 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1236 if (smb_buf->WordCount != 3) {
1238 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1242 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1243 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1245 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1246 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1248 bytes_remaining = get_bcc(smb_buf);
1249 bcc_ptr = pByteArea(smb_buf);
1251 /* BB check if Unicode and decode strings */
1252 if (bytes_remaining == 0) {
1253 /* no string area to decode, do nothing */
1254 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1255 /* unicode string area must be word-aligned */
1256 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1260 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1263 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1267 rc = sess_establish_session(sess_data);
1269 sess_data->result = rc;
1270 sess_data->func = NULL;
1271 sess_free_buffer(sess_data);
1272 kfree(ses->auth_key.response);
1273 ses->auth_key.response = NULL;
1276 #ifdef CONFIG_CIFS_UPCALL
1278 sess_auth_kerberos(struct sess_data *sess_data)
1281 struct smb_hdr *smb_buf;
1282 SESSION_SETUP_ANDX *pSMB;
1284 struct cifs_ses *ses = sess_data->ses;
1285 struct TCP_Server_Info *server = sess_data->server;
1287 __u16 bytes_remaining;
1288 struct key *spnego_key = NULL;
1289 struct cifs_spnego_msg *msg;
1292 /* extended security */
1294 rc = sess_alloc_buffer(sess_data, 12);
1298 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1299 bcc_ptr = sess_data->iov[2].iov_base;
1300 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1302 spnego_key = cifs_get_spnego_key(ses, server);
1303 if (IS_ERR(spnego_key)) {
1304 rc = PTR_ERR(spnego_key);
1309 msg = spnego_key->payload.data[0];
1311 * check version field to make sure that cifs.upcall is
1312 * sending us a response in an expected form
1314 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1315 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1316 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1318 goto out_put_spnego_key;
1321 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1323 if (!ses->auth_key.response) {
1324 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1327 goto out_put_spnego_key;
1329 ses->auth_key.len = msg->sesskey_len;
1331 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1332 capabilities |= CAP_EXTENDED_SECURITY;
1333 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1334 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1335 sess_data->iov[1].iov_len = msg->secblob_len;
1336 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1338 if (ses->capabilities & CAP_UNICODE) {
1339 /* unicode strings must be word aligned */
1340 if ((sess_data->iov[0].iov_len
1341 + sess_data->iov[1].iov_len) % 2) {
1345 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1346 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1348 /* BB: is this right? */
1349 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1352 sess_data->iov[2].iov_len = (long) bcc_ptr -
1353 (long) sess_data->iov[2].iov_base;
1355 rc = sess_sendreceive(sess_data);
1357 goto out_put_spnego_key;
1359 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1360 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1362 if (smb_buf->WordCount != 4) {
1364 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1365 goto out_put_spnego_key;
1368 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1369 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1371 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1372 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1374 bytes_remaining = get_bcc(smb_buf);
1375 bcc_ptr = pByteArea(smb_buf);
1377 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1378 if (blob_len > bytes_remaining) {
1379 cifs_dbg(VFS, "bad security blob length %d\n",
1382 goto out_put_spnego_key;
1384 bcc_ptr += blob_len;
1385 bytes_remaining -= blob_len;
1387 /* BB check if Unicode and decode strings */
1388 if (bytes_remaining == 0) {
1389 /* no string area to decode, do nothing */
1390 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1391 /* unicode string area must be word-aligned */
1392 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1396 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1399 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1403 rc = sess_establish_session(sess_data);
1405 key_invalidate(spnego_key);
1406 key_put(spnego_key);
1408 sess_data->result = rc;
1409 sess_data->func = NULL;
1410 sess_free_buffer(sess_data);
1411 kfree(ses->auth_key.response);
1412 ses->auth_key.response = NULL;
1415 #endif /* ! CONFIG_CIFS_UPCALL */
1418 * The required kvec buffers have to be allocated before calling this
1422 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1424 SESSION_SETUP_ANDX *pSMB;
1425 struct cifs_ses *ses = sess_data->ses;
1426 struct TCP_Server_Info *server = sess_data->server;
1430 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1432 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1433 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1434 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1438 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1439 capabilities |= CAP_EXTENDED_SECURITY;
1440 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1442 bcc_ptr = sess_data->iov[2].iov_base;
1443 /* unicode strings must be word aligned */
1444 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1448 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1450 sess_data->iov[2].iov_len = (long) bcc_ptr -
1451 (long) sess_data->iov[2].iov_base;
1457 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1460 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1463 struct smb_hdr *smb_buf;
1464 SESSION_SETUP_ANDX *pSMB;
1465 struct cifs_ses *ses = sess_data->ses;
1466 struct TCP_Server_Info *server = sess_data->server;
1467 __u16 bytes_remaining;
1469 unsigned char *ntlmsspblob = NULL;
1472 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1475 * if memory allocation is successful, caller of this function
1478 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1479 if (!ses->ntlmssp) {
1483 ses->ntlmssp->sesskey_per_smbsess = false;
1486 rc = sess_alloc_buffer(sess_data, 12);
1490 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1492 /* Build security blob before we assemble the request */
1493 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
1494 &blob_len, ses, server,
1497 goto out_free_ntlmsspblob;
1499 sess_data->iov[1].iov_len = blob_len;
1500 sess_data->iov[1].iov_base = ntlmsspblob;
1501 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1503 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1505 goto out_free_ntlmsspblob;
1507 rc = sess_sendreceive(sess_data);
1509 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1510 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1512 /* If true, rc here is expected and not an error */
1513 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1514 smb_buf->Status.CifsError ==
1515 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1519 goto out_free_ntlmsspblob;
1521 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1523 if (smb_buf->WordCount != 4) {
1525 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1526 goto out_free_ntlmsspblob;
1529 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1530 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1532 bytes_remaining = get_bcc(smb_buf);
1533 bcc_ptr = pByteArea(smb_buf);
1535 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1536 if (blob_len > bytes_remaining) {
1537 cifs_dbg(VFS, "bad security blob length %d\n",
1540 goto out_free_ntlmsspblob;
1543 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1545 out_free_ntlmsspblob:
1548 sess_free_buffer(sess_data);
1551 sess_data->func = sess_auth_rawntlmssp_authenticate;
1555 /* Else error. Cleanup */
1556 kfree(ses->auth_key.response);
1557 ses->auth_key.response = NULL;
1558 kfree(ses->ntlmssp);
1559 ses->ntlmssp = NULL;
1561 sess_data->func = NULL;
1562 sess_data->result = rc;
1566 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1569 struct smb_hdr *smb_buf;
1570 SESSION_SETUP_ANDX *pSMB;
1571 struct cifs_ses *ses = sess_data->ses;
1572 struct TCP_Server_Info *server = sess_data->server;
1573 __u16 bytes_remaining;
1575 unsigned char *ntlmsspblob = NULL;
1578 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1581 rc = sess_alloc_buffer(sess_data, 12);
1585 /* Build security blob before we assemble the request */
1586 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1587 smb_buf = (struct smb_hdr *)pSMB;
1588 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1589 &blob_len, ses, server,
1592 goto out_free_ntlmsspblob;
1593 sess_data->iov[1].iov_len = blob_len;
1594 sess_data->iov[1].iov_base = ntlmsspblob;
1595 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1597 * Make sure that we tell the server that we are using
1598 * the uid that it just gave us back on the response
1601 smb_buf->Uid = ses->Suid;
1603 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1605 goto out_free_ntlmsspblob;
1607 rc = sess_sendreceive(sess_data);
1609 goto out_free_ntlmsspblob;
1611 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1612 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1613 if (smb_buf->WordCount != 4) {
1615 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1616 goto out_free_ntlmsspblob;
1619 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1620 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1622 if (ses->Suid != smb_buf->Uid) {
1623 ses->Suid = smb_buf->Uid;
1624 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1627 bytes_remaining = get_bcc(smb_buf);
1628 bcc_ptr = pByteArea(smb_buf);
1629 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1630 if (blob_len > bytes_remaining) {
1631 cifs_dbg(VFS, "bad security blob length %d\n",
1634 goto out_free_ntlmsspblob;
1636 bcc_ptr += blob_len;
1637 bytes_remaining -= blob_len;
1640 /* BB check if Unicode and decode strings */
1641 if (bytes_remaining == 0) {
1642 /* no string area to decode, do nothing */
1643 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1644 /* unicode string area must be word-aligned */
1645 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1649 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1652 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1656 out_free_ntlmsspblob:
1659 sess_free_buffer(sess_data);
1662 rc = sess_establish_session(sess_data);
1665 kfree(ses->auth_key.response);
1666 ses->auth_key.response = NULL;
1667 kfree(ses->ntlmssp);
1668 ses->ntlmssp = NULL;
1670 sess_data->func = NULL;
1671 sess_data->result = rc;
1674 static int select_sec(struct sess_data *sess_data)
1677 struct cifs_ses *ses = sess_data->ses;
1678 struct TCP_Server_Info *server = sess_data->server;
1680 type = cifs_select_sectype(server, ses->sectype);
1681 cifs_dbg(FYI, "sess setup type %d\n", type);
1682 if (type == Unspecified) {
1683 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1689 sess_data->func = sess_auth_ntlmv2;
1692 #ifdef CONFIG_CIFS_UPCALL
1693 sess_data->func = sess_auth_kerberos;
1696 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1698 #endif /* CONFIG_CIFS_UPCALL */
1700 sess_data->func = sess_auth_rawntlmssp_negotiate;
1703 cifs_dbg(VFS, "secType %d not supported!\n", type);
1710 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1711 struct TCP_Server_Info *server,
1712 const struct nls_table *nls_cp)
1715 struct sess_data *sess_data;
1718 WARN(1, "%s: ses == NULL!", __func__);
1722 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1726 sess_data->xid = xid;
1727 sess_data->ses = ses;
1728 sess_data->server = server;
1729 sess_data->buf0_type = CIFS_NO_BUFFER;
1730 sess_data->nls_cp = (struct nls_table *) nls_cp;
1732 rc = select_sec(sess_data);
1736 while (sess_data->func)
1737 sess_data->func(sess_data);
1739 /* Store result before we free sess_data */
1740 rc = sess_data->result;