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 if (ses->server->dialect < SMB30_PROT_ID) {
131 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
135 spin_lock(&ses->chan_lock);
137 new_chan_count = old_chan_count = ses->chan_count;
138 left = ses->chan_max - ses->chan_count;
141 spin_unlock(&ses->chan_lock);
143 "ses already at max_channels (%zu), nothing to open\n",
148 if (!(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
150 spin_unlock(&ses->chan_lock);
151 cifs_dbg(VFS, "server %s does not support multichannel\n", ses->server->hostname);
154 spin_unlock(&ses->chan_lock);
157 * Make a copy of the iface list at the time and use that
158 * instead so as to not hold the iface spinlock for opening
161 spin_lock(&ses->iface_lock);
162 iface_count = ses->iface_count;
163 if (iface_count <= 0) {
164 spin_unlock(&ses->iface_lock);
165 cifs_dbg(VFS, "no iface list available to open channels\n");
168 ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces),
171 spin_unlock(&ses->iface_lock);
174 spin_unlock(&ses->iface_lock);
177 * Keep connecting to same, fastest, iface for all channels as
178 * long as its RSS. Try next fastest one if not RSS or channel
182 struct cifs_server_iface *iface;
185 if (tries > 3*ses->chan_max) {
186 cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
192 if (is_ses_using_iface(ses, iface) && !iface->rss_capable) {
193 i = (i+1) % iface_count;
197 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
199 cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n",
201 i = (i+1) % iface_count;
205 cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
212 return new_chan_count - old_chan_count;
216 * If server is a channel of ses, return the corresponding enclosing
217 * cifs_chan otherwise return NULL.
220 cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
224 spin_lock(&ses->chan_lock);
225 for (i = 0; i < ses->chan_count; i++) {
226 if (ses->chans[i].server == server) {
227 spin_unlock(&ses->chan_lock);
228 return &ses->chans[i];
231 spin_unlock(&ses->chan_lock);
236 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
237 struct cifs_server_iface *iface)
239 struct TCP_Server_Info *chan_server;
240 struct cifs_chan *chan;
241 struct smb3_fs_context ctx = {NULL};
242 static const char unc_fmt[] = "\\%s\\foo";
243 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
244 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
245 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
247 unsigned int xid = get_xid();
249 if (iface->sockaddr.ss_family == AF_INET)
250 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
251 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
254 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
255 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
259 * Setup a ctx with mostly the same info as the existing
260 * session and overwrite it with the requested iface data.
262 * We need to setup at least the fields used for negprot and
265 * We only need the ctx here, so we can reuse memory from
266 * the session and server without caring about memory
270 /* Always make new connection for now (TODO?) */
271 ctx.nosharesock = true;
274 ctx.domainauto = ses->domainAuto;
275 ctx.domainname = ses->domainName;
276 ctx.server_hostname = ses->server->hostname;
277 ctx.username = ses->user_name;
278 ctx.password = ses->password;
279 ctx.sectype = ses->sectype;
280 ctx.sign = ses->sign;
283 /* XXX: Use ses->server->hostname? */
284 sprintf(unc, unc_fmt, ses->ip_addr);
288 /* Reuse same version as master connection */
289 ctx.vals = ses->server->vals;
290 ctx.ops = ses->server->ops;
292 ctx.noblocksnd = ses->server->noblocksnd;
293 ctx.noautotune = ses->server->noautotune;
294 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
295 ctx.echo_interval = ses->server->echo_interval / HZ;
296 ctx.max_credits = ses->server->max_credits;
299 * This will be used for encoding/decoding user/domain/pw
300 * during sess setup auth.
302 ctx.local_nls = cifs_sb->local_nls;
304 /* Use RDMA if possible */
305 ctx.rdma = iface->rdma_capable;
306 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
308 /* reuse master con client guid */
309 memcpy(&ctx.client_guid, ses->server->client_guid,
310 SMB2_CLIENT_GUID_SIZE);
311 ctx.use_client_guid = true;
313 chan_server = cifs_get_tcp_session(&ctx, ses->server);
315 spin_lock(&ses->chan_lock);
316 chan = &ses->chans[ses->chan_count];
317 chan->server = chan_server;
318 if (IS_ERR(chan->server)) {
319 rc = PTR_ERR(chan->server);
321 spin_unlock(&ses->chan_lock);
325 atomic_set(&ses->chan_seq, 0);
327 /* Mark this channel as needing connect/setup */
328 cifs_chan_set_need_reconnect(ses, chan->server);
330 spin_unlock(&ses->chan_lock);
332 mutex_lock(&ses->session_mutex);
334 * We need to allocate the server crypto now as we will need
335 * to sign packets before we generate the channel signing key
336 * (we sign with the session key)
338 rc = smb311_crypto_shash_allocate(chan->server);
340 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
341 mutex_unlock(&ses->session_mutex);
345 rc = cifs_negotiate_protocol(xid, ses, chan->server);
347 rc = cifs_setup_session(xid, ses, chan->server, cifs_sb->local_nls);
349 mutex_unlock(&ses->session_mutex);
352 if (rc && chan->server) {
353 spin_lock(&ses->chan_lock);
354 /* we rely on all bits beyond chan_count to be clear */
355 cifs_chan_clear_need_reconnect(ses, chan->server);
358 * chan_count should never reach 0 as at least the primary
359 * channel is always allocated
361 WARN_ON(ses->chan_count < 1);
362 spin_unlock(&ses->chan_lock);
365 if (rc && chan->server)
366 cifs_put_tcp_session(chan->server, 0);
371 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses,
372 struct TCP_Server_Info *server,
373 SESSION_SETUP_ANDX *pSMB)
375 __u32 capabilities = 0;
377 /* init fields common to all four types of SessSetup */
378 /* Note that offsets for first seven fields in req struct are same */
379 /* in CIFS Specs so does not matter which of 3 forms of struct */
380 /* that we use in next few lines */
381 /* Note that header is initialized to zero in header_assemble */
382 pSMB->req.AndXCommand = 0xFF;
383 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
384 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
386 pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq);
387 pSMB->req.VcNumber = cpu_to_le16(1);
389 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
391 /* BB verify whether signing required on neg or just on auth frame
394 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
395 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
398 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
400 if (ses->capabilities & CAP_UNICODE) {
401 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
402 capabilities |= CAP_UNICODE;
404 if (ses->capabilities & CAP_STATUS32) {
405 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
406 capabilities |= CAP_STATUS32;
408 if (ses->capabilities & CAP_DFS) {
409 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
410 capabilities |= CAP_DFS;
412 if (ses->capabilities & CAP_UNIX)
413 capabilities |= CAP_UNIX;
419 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
421 char *bcc_ptr = *pbcc_area;
424 /* Copy OS version */
425 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
427 bcc_ptr += 2 * bytes_ret;
428 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
430 bcc_ptr += 2 * bytes_ret;
431 bcc_ptr += 2; /* trailing null */
433 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
435 bcc_ptr += 2 * bytes_ret;
436 bcc_ptr += 2; /* trailing null */
438 *pbcc_area = bcc_ptr;
441 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
442 const struct nls_table *nls_cp)
444 char *bcc_ptr = *pbcc_area;
448 if (ses->domainName == NULL) {
449 /* Sending null domain better than using a bogus domain name (as
450 we did briefly in 2.6.18) since server will use its default */
455 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
456 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
457 bcc_ptr += 2 * bytes_ret;
458 bcc_ptr += 2; /* account for null terminator */
460 *pbcc_area = bcc_ptr;
464 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
465 const struct nls_table *nls_cp)
467 char *bcc_ptr = *pbcc_area;
470 /* BB FIXME add check that strings total less
471 than 335 or will need to send them as arrays */
473 /* unicode strings, must be word aligned before the call */
474 /* if ((long) bcc_ptr % 2) {
479 if (ses->user_name == NULL) {
480 /* null user mount */
484 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
485 CIFS_MAX_USERNAME_LEN, nls_cp);
487 bcc_ptr += 2 * bytes_ret;
488 bcc_ptr += 2; /* account for null termination */
490 unicode_domain_string(&bcc_ptr, ses, nls_cp);
491 unicode_oslm_strings(&bcc_ptr, nls_cp);
493 *pbcc_area = bcc_ptr;
496 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
497 const struct nls_table *nls_cp)
499 char *bcc_ptr = *pbcc_area;
503 /* BB what about null user mounts - check that we do this BB */
505 if (ses->user_name != NULL) {
506 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
507 if (WARN_ON_ONCE(len < 0))
508 len = CIFS_MAX_USERNAME_LEN - 1;
511 /* else null user mount */
513 bcc_ptr++; /* account for null termination */
516 if (ses->domainName != NULL) {
517 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
518 if (WARN_ON_ONCE(len < 0))
519 len = CIFS_MAX_DOMAINNAME_LEN - 1;
521 } /* else we will send a null domain name
522 so the server will default to its own domain */
526 /* BB check for overflow here */
528 strcpy(bcc_ptr, "Linux version ");
529 bcc_ptr += strlen("Linux version ");
530 strcpy(bcc_ptr, init_utsname()->release);
531 bcc_ptr += strlen(init_utsname()->release) + 1;
533 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
534 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
536 *pbcc_area = bcc_ptr;
540 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
541 const struct nls_table *nls_cp)
544 char *data = *pbcc_area;
546 cifs_dbg(FYI, "bleft %d\n", bleft);
548 kfree(ses->serverOS);
549 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
550 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
551 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
557 kfree(ses->serverNOS);
558 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
559 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
560 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
566 kfree(ses->serverDomain);
567 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
568 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
573 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
574 struct cifs_ses *ses,
575 const struct nls_table *nls_cp)
578 char *bcc_ptr = *pbcc_area;
580 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
582 len = strnlen(bcc_ptr, bleft);
586 kfree(ses->serverOS);
588 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
590 memcpy(ses->serverOS, bcc_ptr, len);
591 ses->serverOS[len] = 0;
592 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
593 cifs_dbg(FYI, "OS/2 server\n");
599 len = strnlen(bcc_ptr, bleft);
603 kfree(ses->serverNOS);
605 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
606 if (ses->serverNOS) {
607 memcpy(ses->serverNOS, bcc_ptr, len);
608 ses->serverNOS[len] = 0;
614 len = strnlen(bcc_ptr, bleft);
618 /* No domain field in LANMAN case. Domain is
619 returned by old servers in the SMB negprot response */
620 /* BB For newer servers which do not support Unicode,
621 but thus do return domain here we could add parsing
622 for it later, but it is not very important */
623 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
626 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
627 struct cifs_ses *ses)
629 unsigned int tioffset; /* challenge message target info area */
630 unsigned int tilen; /* challenge message target info area length */
631 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
634 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
635 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
639 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
640 cifs_dbg(VFS, "blob signature incorrect %s\n",
644 if (pblob->MessageType != NtLmChallenge) {
645 cifs_dbg(VFS, "Incorrect message type %d\n",
650 server_flags = le32_to_cpu(pblob->NegotiateFlags);
651 cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__,
652 ses->ntlmssp->client_flags, server_flags);
654 if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) &&
655 (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) {
656 cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n",
660 if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) {
661 cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__);
664 if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) {
665 cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n",
669 if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
670 !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH))
671 pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n",
674 ses->ntlmssp->server_flags = server_flags;
676 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
677 /* In particular we can examine sign flags */
678 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
679 we must set the MIC field of the AUTHENTICATE_MESSAGE */
681 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
682 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
683 if (tioffset > blob_len || tioffset + tilen > blob_len) {
684 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
689 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
691 if (!ses->auth_key.response) {
692 cifs_dbg(VFS, "Challenge target info alloc failure\n");
695 ses->auth_key.len = tilen;
701 static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
703 int sz = base_size + ses->auth_key.len
704 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
707 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
709 sz += sizeof(__le16);
712 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
714 sz += sizeof(__le16);
716 sz += sizeof(__le16) * strnlen(ses->workstation_name, CIFS_MAX_WORKSTATION_LEN);
721 static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
724 unsigned char *pstart,
725 unsigned char **pcur,
726 const struct nls_table *nls_cp)
728 unsigned char *tmp = pstart;
738 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
740 pbuf->MaximumLength = 0;
741 *pcur += sizeof(__le16);
743 len = cifs_strtoUTF16((__le16 *)*pcur,
747 len *= sizeof(__le16);
748 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
749 pbuf->Length = cpu_to_le16(len);
750 pbuf->MaximumLength = cpu_to_le16(len);
755 /* BB Move to ntlmssp.c eventually */
757 int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
759 struct cifs_ses *ses,
760 struct TCP_Server_Info *server,
761 const struct nls_table *nls_cp)
764 NEGOTIATE_MESSAGE *sec_blob;
769 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
770 *pbuffer = kmalloc(len, GFP_KERNEL);
773 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
775 goto setup_ntlm_neg_ret;
777 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
779 memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
780 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
781 sec_blob->MessageType = NtLmNegotiate;
783 /* BB is NTLMV2 session security format easier to use here? */
784 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
785 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
786 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
787 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
788 NTLMSSP_NEGOTIATE_SIGN;
789 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
790 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
792 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
793 ses->ntlmssp->client_flags = flags;
794 sec_blob->NegotiateFlags = cpu_to_le32(flags);
796 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
797 cifs_security_buffer_from_str(&sec_blob->DomainName,
799 CIFS_MAX_DOMAINNAME_LEN,
803 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
805 CIFS_MAX_WORKSTATION_LEN,
809 *buflen = tmp - *pbuffer;
815 * Build ntlmssp blob with additional fields, such as version,
816 * supported by modern servers. For safety limit to SMB3 or later
817 * See notes in MS-NLMP Section 2.2.2.1 e.g.
819 int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer,
821 struct cifs_ses *ses,
822 struct TCP_Server_Info *server,
823 const struct nls_table *nls_cp)
826 struct negotiate_message *sec_blob;
831 len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message));
832 *pbuffer = kmalloc(len, GFP_KERNEL);
835 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
837 goto setup_ntlm_smb3_neg_ret;
839 sec_blob = (struct negotiate_message *)*pbuffer;
841 memset(*pbuffer, 0, sizeof(struct negotiate_message));
842 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
843 sec_blob->MessageType = NtLmNegotiate;
845 /* BB is NTLMV2 session security format easier to use here? */
846 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
847 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
848 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
849 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
850 NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION;
851 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
852 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
854 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
855 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
856 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
857 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
859 tmp = *pbuffer + sizeof(struct negotiate_message);
860 ses->ntlmssp->client_flags = flags;
861 sec_blob->NegotiateFlags = cpu_to_le32(flags);
863 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
864 cifs_security_buffer_from_str(&sec_blob->DomainName,
866 CIFS_MAX_DOMAINNAME_LEN,
870 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
872 CIFS_MAX_WORKSTATION_LEN,
876 *buflen = tmp - *pbuffer;
877 setup_ntlm_smb3_neg_ret:
882 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
884 struct cifs_ses *ses,
885 struct TCP_Server_Info *server,
886 const struct nls_table *nls_cp)
889 AUTHENTICATE_MESSAGE *sec_blob;
894 rc = setup_ntlmv2_rsp(ses, nls_cp);
896 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
898 goto setup_ntlmv2_ret;
901 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
902 *pbuffer = kmalloc(len, GFP_KERNEL);
905 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
907 goto setup_ntlmv2_ret;
909 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
911 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
912 sec_blob->MessageType = NtLmAuthenticate;
914 flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET |
915 NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
917 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
918 sec_blob->NegotiateFlags = cpu_to_le32(flags);
920 sec_blob->LmChallengeResponse.BufferOffset =
921 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
922 sec_blob->LmChallengeResponse.Length = 0;
923 sec_blob->LmChallengeResponse.MaximumLength = 0;
925 sec_blob->NtChallengeResponse.BufferOffset =
926 cpu_to_le32(tmp - *pbuffer);
927 if (ses->user_name != NULL) {
928 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
929 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
930 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
932 sec_blob->NtChallengeResponse.Length =
933 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
934 sec_blob->NtChallengeResponse.MaximumLength =
935 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
938 * don't send an NT Response for anonymous access
940 sec_blob->NtChallengeResponse.Length = 0;
941 sec_blob->NtChallengeResponse.MaximumLength = 0;
944 cifs_security_buffer_from_str(&sec_blob->DomainName,
946 CIFS_MAX_DOMAINNAME_LEN,
950 cifs_security_buffer_from_str(&sec_blob->UserName,
952 CIFS_MAX_USERNAME_LEN,
956 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
957 ses->workstation_name,
958 CIFS_MAX_WORKSTATION_LEN,
962 if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
963 (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) &&
965 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
966 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
967 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
968 sec_blob->SessionKey.MaximumLength =
969 cpu_to_le16(CIFS_CPHTXT_SIZE);
970 tmp += CIFS_CPHTXT_SIZE;
972 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
973 sec_blob->SessionKey.Length = 0;
974 sec_blob->SessionKey.MaximumLength = 0;
977 *buflen = tmp - *pbuffer;
983 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
985 switch (server->negflavor) {
986 case CIFS_NEGFLAVOR_EXTENDED:
992 if (server->sec_ntlmssp &&
993 (global_secflags & CIFSSEC_MAY_NTLMSSP))
995 if ((server->sec_kerberos || server->sec_mskerberos) &&
996 (global_secflags & CIFSSEC_MAY_KRB5))
1002 case CIFS_NEGFLAVOR_UNENCAP:
1003 switch (requested) {
1007 if (global_secflags & CIFSSEC_MAY_NTLMV2)
1021 struct cifs_ses *ses;
1022 struct TCP_Server_Info *server;
1023 struct nls_table *nls_cp;
1024 void (*func)(struct sess_data *);
1027 /* we will send the SMB in three pieces:
1028 * a fixed length beginning part, an optional
1029 * SPNEGO blob (which can be zero length), and a
1030 * last part which will include the strings
1031 * and rest of bcc area. This allows us to avoid
1032 * a large buffer 17K allocation
1039 sess_alloc_buffer(struct sess_data *sess_data, int wct)
1042 struct cifs_ses *ses = sess_data->ses;
1043 struct smb_hdr *smb_buf;
1045 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
1051 sess_data->iov[0].iov_base = (char *)smb_buf;
1052 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
1054 * This variable will be used to clear the buffer
1055 * allocated above in case of any error in the calling function.
1057 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1059 /* 2000 big enough to fit max user, domain, NOS name etc. */
1060 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
1061 if (!sess_data->iov[2].iov_base) {
1063 goto out_free_smb_buf;
1069 cifs_small_buf_release(smb_buf);
1070 sess_data->iov[0].iov_base = NULL;
1071 sess_data->iov[0].iov_len = 0;
1072 sess_data->buf0_type = CIFS_NO_BUFFER;
1077 sess_free_buffer(struct sess_data *sess_data)
1080 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1081 sess_data->buf0_type = CIFS_NO_BUFFER;
1082 kfree(sess_data->iov[2].iov_base);
1086 sess_establish_session(struct sess_data *sess_data)
1088 struct cifs_ses *ses = sess_data->ses;
1089 struct TCP_Server_Info *server = sess_data->server;
1091 mutex_lock(&server->srv_mutex);
1092 if (!server->session_estab) {
1094 server->session_key.response =
1095 kmemdup(ses->auth_key.response,
1096 ses->auth_key.len, GFP_KERNEL);
1097 if (!server->session_key.response) {
1098 mutex_unlock(&server->srv_mutex);
1101 server->session_key.len =
1104 server->sequence_number = 0x2;
1105 server->session_estab = true;
1107 mutex_unlock(&server->srv_mutex);
1109 cifs_dbg(FYI, "CIFS session established successfully\n");
1114 sess_sendreceive(struct sess_data *sess_data)
1117 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
1119 struct kvec rsp_iov = { NULL, 0 };
1121 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
1122 be32_add_cpu(&smb_buf->smb_buf_length, count);
1123 put_bcc(count, smb_buf);
1125 rc = SendReceive2(sess_data->xid, sess_data->ses,
1126 sess_data->iov, 3 /* num_iovecs */,
1127 &sess_data->buf0_type,
1128 CIFS_LOG_ERROR, &rsp_iov);
1129 cifs_small_buf_release(sess_data->iov[0].iov_base);
1130 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1136 sess_auth_ntlmv2(struct sess_data *sess_data)
1139 struct smb_hdr *smb_buf;
1140 SESSION_SETUP_ANDX *pSMB;
1142 struct cifs_ses *ses = sess_data->ses;
1143 struct TCP_Server_Info *server = sess_data->server;
1145 __u16 bytes_remaining;
1147 /* old style NTLM sessionsetup */
1149 rc = sess_alloc_buffer(sess_data, 13);
1153 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1154 bcc_ptr = sess_data->iov[2].iov_base;
1155 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1157 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1159 /* LM2 password would be here if we supported it */
1160 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1162 if (ses->user_name != NULL) {
1163 /* calculate nlmv2 response and session key */
1164 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1166 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1170 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1171 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1172 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1174 /* set case sensitive password length after tilen may get
1175 * assigned, tilen is 0 otherwise.
1177 pSMB->req_no_secext.CaseSensitivePasswordLength =
1178 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1180 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1183 if (ses->capabilities & CAP_UNICODE) {
1184 if (sess_data->iov[0].iov_len % 2) {
1188 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1190 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1194 sess_data->iov[2].iov_len = (long) bcc_ptr -
1195 (long) sess_data->iov[2].iov_base;
1197 rc = sess_sendreceive(sess_data);
1201 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1202 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1204 if (smb_buf->WordCount != 3) {
1206 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1210 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1211 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1213 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1214 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1216 bytes_remaining = get_bcc(smb_buf);
1217 bcc_ptr = pByteArea(smb_buf);
1219 /* BB check if Unicode and decode strings */
1220 if (bytes_remaining == 0) {
1221 /* no string area to decode, do nothing */
1222 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1223 /* unicode string area must be word-aligned */
1224 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1228 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1231 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1235 rc = sess_establish_session(sess_data);
1237 sess_data->result = rc;
1238 sess_data->func = NULL;
1239 sess_free_buffer(sess_data);
1240 kfree(ses->auth_key.response);
1241 ses->auth_key.response = NULL;
1244 #ifdef CONFIG_CIFS_UPCALL
1246 sess_auth_kerberos(struct sess_data *sess_data)
1249 struct smb_hdr *smb_buf;
1250 SESSION_SETUP_ANDX *pSMB;
1252 struct cifs_ses *ses = sess_data->ses;
1253 struct TCP_Server_Info *server = sess_data->server;
1255 __u16 bytes_remaining;
1256 struct key *spnego_key = NULL;
1257 struct cifs_spnego_msg *msg;
1260 /* extended security */
1262 rc = sess_alloc_buffer(sess_data, 12);
1266 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1267 bcc_ptr = sess_data->iov[2].iov_base;
1268 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1270 spnego_key = cifs_get_spnego_key(ses, server);
1271 if (IS_ERR(spnego_key)) {
1272 rc = PTR_ERR(spnego_key);
1277 msg = spnego_key->payload.data[0];
1279 * check version field to make sure that cifs.upcall is
1280 * sending us a response in an expected form
1282 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1283 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1284 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1286 goto out_put_spnego_key;
1289 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1291 if (!ses->auth_key.response) {
1292 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1295 goto out_put_spnego_key;
1297 ses->auth_key.len = msg->sesskey_len;
1299 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1300 capabilities |= CAP_EXTENDED_SECURITY;
1301 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1302 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1303 sess_data->iov[1].iov_len = msg->secblob_len;
1304 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1306 if (ses->capabilities & CAP_UNICODE) {
1307 /* unicode strings must be word aligned */
1308 if ((sess_data->iov[0].iov_len
1309 + sess_data->iov[1].iov_len) % 2) {
1313 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1314 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1316 /* BB: is this right? */
1317 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1320 sess_data->iov[2].iov_len = (long) bcc_ptr -
1321 (long) sess_data->iov[2].iov_base;
1323 rc = sess_sendreceive(sess_data);
1325 goto out_put_spnego_key;
1327 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1328 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1330 if (smb_buf->WordCount != 4) {
1332 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1333 goto out_put_spnego_key;
1336 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1337 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1339 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1340 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1342 bytes_remaining = get_bcc(smb_buf);
1343 bcc_ptr = pByteArea(smb_buf);
1345 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1346 if (blob_len > bytes_remaining) {
1347 cifs_dbg(VFS, "bad security blob length %d\n",
1350 goto out_put_spnego_key;
1352 bcc_ptr += blob_len;
1353 bytes_remaining -= blob_len;
1355 /* BB check if Unicode and decode strings */
1356 if (bytes_remaining == 0) {
1357 /* no string area to decode, do nothing */
1358 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1359 /* unicode string area must be word-aligned */
1360 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1364 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1367 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1371 rc = sess_establish_session(sess_data);
1373 key_invalidate(spnego_key);
1374 key_put(spnego_key);
1376 sess_data->result = rc;
1377 sess_data->func = NULL;
1378 sess_free_buffer(sess_data);
1379 kfree(ses->auth_key.response);
1380 ses->auth_key.response = NULL;
1383 #endif /* ! CONFIG_CIFS_UPCALL */
1386 * The required kvec buffers have to be allocated before calling this
1390 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1392 SESSION_SETUP_ANDX *pSMB;
1393 struct cifs_ses *ses = sess_data->ses;
1394 struct TCP_Server_Info *server = sess_data->server;
1398 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1400 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1401 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1402 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1406 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1407 capabilities |= CAP_EXTENDED_SECURITY;
1408 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1410 bcc_ptr = sess_data->iov[2].iov_base;
1411 /* unicode strings must be word aligned */
1412 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1416 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1418 sess_data->iov[2].iov_len = (long) bcc_ptr -
1419 (long) sess_data->iov[2].iov_base;
1425 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1428 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1431 struct smb_hdr *smb_buf;
1432 SESSION_SETUP_ANDX *pSMB;
1433 struct cifs_ses *ses = sess_data->ses;
1434 struct TCP_Server_Info *server = sess_data->server;
1435 __u16 bytes_remaining;
1437 unsigned char *ntlmsspblob = NULL;
1440 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1443 * if memory allocation is successful, caller of this function
1446 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1447 if (!ses->ntlmssp) {
1451 ses->ntlmssp->sesskey_per_smbsess = false;
1454 rc = sess_alloc_buffer(sess_data, 12);
1458 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1460 /* Build security blob before we assemble the request */
1461 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
1462 &blob_len, ses, server,
1465 goto out_free_ntlmsspblob;
1467 sess_data->iov[1].iov_len = blob_len;
1468 sess_data->iov[1].iov_base = ntlmsspblob;
1469 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1471 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1473 goto out_free_ntlmsspblob;
1475 rc = sess_sendreceive(sess_data);
1477 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1478 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1480 /* If true, rc here is expected and not an error */
1481 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1482 smb_buf->Status.CifsError ==
1483 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1487 goto out_free_ntlmsspblob;
1489 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1491 if (smb_buf->WordCount != 4) {
1493 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1494 goto out_free_ntlmsspblob;
1497 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1498 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1500 bytes_remaining = get_bcc(smb_buf);
1501 bcc_ptr = pByteArea(smb_buf);
1503 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1504 if (blob_len > bytes_remaining) {
1505 cifs_dbg(VFS, "bad security blob length %d\n",
1508 goto out_free_ntlmsspblob;
1511 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1513 out_free_ntlmsspblob:
1516 sess_free_buffer(sess_data);
1519 sess_data->func = sess_auth_rawntlmssp_authenticate;
1523 /* Else error. Cleanup */
1524 kfree(ses->auth_key.response);
1525 ses->auth_key.response = NULL;
1526 kfree(ses->ntlmssp);
1527 ses->ntlmssp = NULL;
1529 sess_data->func = NULL;
1530 sess_data->result = rc;
1534 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1537 struct smb_hdr *smb_buf;
1538 SESSION_SETUP_ANDX *pSMB;
1539 struct cifs_ses *ses = sess_data->ses;
1540 struct TCP_Server_Info *server = sess_data->server;
1541 __u16 bytes_remaining;
1543 unsigned char *ntlmsspblob = NULL;
1546 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1549 rc = sess_alloc_buffer(sess_data, 12);
1553 /* Build security blob before we assemble the request */
1554 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1555 smb_buf = (struct smb_hdr *)pSMB;
1556 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1557 &blob_len, ses, server,
1560 goto out_free_ntlmsspblob;
1561 sess_data->iov[1].iov_len = blob_len;
1562 sess_data->iov[1].iov_base = ntlmsspblob;
1563 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1565 * Make sure that we tell the server that we are using
1566 * the uid that it just gave us back on the response
1569 smb_buf->Uid = ses->Suid;
1571 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1573 goto out_free_ntlmsspblob;
1575 rc = sess_sendreceive(sess_data);
1577 goto out_free_ntlmsspblob;
1579 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1580 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1581 if (smb_buf->WordCount != 4) {
1583 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1584 goto out_free_ntlmsspblob;
1587 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1588 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1590 if (ses->Suid != smb_buf->Uid) {
1591 ses->Suid = smb_buf->Uid;
1592 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1595 bytes_remaining = get_bcc(smb_buf);
1596 bcc_ptr = pByteArea(smb_buf);
1597 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1598 if (blob_len > bytes_remaining) {
1599 cifs_dbg(VFS, "bad security blob length %d\n",
1602 goto out_free_ntlmsspblob;
1604 bcc_ptr += blob_len;
1605 bytes_remaining -= blob_len;
1608 /* BB check if Unicode and decode strings */
1609 if (bytes_remaining == 0) {
1610 /* no string area to decode, do nothing */
1611 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1612 /* unicode string area must be word-aligned */
1613 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1617 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1620 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1624 out_free_ntlmsspblob:
1627 sess_free_buffer(sess_data);
1630 rc = sess_establish_session(sess_data);
1633 kfree(ses->auth_key.response);
1634 ses->auth_key.response = NULL;
1635 kfree(ses->ntlmssp);
1636 ses->ntlmssp = NULL;
1638 sess_data->func = NULL;
1639 sess_data->result = rc;
1642 static int select_sec(struct sess_data *sess_data)
1645 struct cifs_ses *ses = sess_data->ses;
1646 struct TCP_Server_Info *server = sess_data->server;
1648 type = cifs_select_sectype(server, ses->sectype);
1649 cifs_dbg(FYI, "sess setup type %d\n", type);
1650 if (type == Unspecified) {
1651 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1657 sess_data->func = sess_auth_ntlmv2;
1660 #ifdef CONFIG_CIFS_UPCALL
1661 sess_data->func = sess_auth_kerberos;
1664 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1666 #endif /* CONFIG_CIFS_UPCALL */
1668 sess_data->func = sess_auth_rawntlmssp_negotiate;
1671 cifs_dbg(VFS, "secType %d not supported!\n", type);
1678 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1679 struct TCP_Server_Info *server,
1680 const struct nls_table *nls_cp)
1683 struct sess_data *sess_data;
1686 WARN(1, "%s: ses == NULL!", __func__);
1690 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1694 sess_data->xid = xid;
1695 sess_data->ses = ses;
1696 sess_data->server = server;
1697 sess_data->buf0_type = CIFS_NO_BUFFER;
1698 sess_data->nls_cp = (struct nls_table *) nls_cp;
1700 rc = select_sec(sess_data);
1704 while (sess_data->func)
1705 sess_data->func(sess_data);
1707 /* Store result before we free sess_data */
1708 rc = sess_data->result;