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 (ses->chans[i].iface == 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);
150 cifs_chan_is_iface_active(struct cifs_ses *ses,
151 struct TCP_Server_Info *server)
153 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
155 return ses->chans[chan_index].iface &&
156 ses->chans[chan_index].iface->is_active;
159 /* returns number of channels added */
160 int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
162 int old_chan_count, new_chan_count;
166 struct cifs_server_iface *iface = NULL, *niface = NULL;
168 spin_lock(&ses->chan_lock);
170 new_chan_count = old_chan_count = ses->chan_count;
171 left = ses->chan_max - ses->chan_count;
174 spin_unlock(&ses->chan_lock);
176 "ses already at max_channels (%zu), nothing to open\n",
181 if (ses->server->dialect < SMB30_PROT_ID) {
182 spin_unlock(&ses->chan_lock);
183 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
187 if (!(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
189 spin_unlock(&ses->chan_lock);
190 cifs_dbg(VFS, "server %s does not support multichannel\n", ses->server->hostname);
193 spin_unlock(&ses->chan_lock);
196 * Keep connecting to same, fastest, iface for all channels as
197 * long as its RSS. Try next fastest one if not RSS or channel
200 spin_lock(&ses->iface_lock);
201 iface = list_first_entry(&ses->iface_list, struct cifs_server_iface,
203 spin_unlock(&ses->iface_lock);
208 if (tries > 3*ses->chan_max) {
209 cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
214 spin_lock(&ses->iface_lock);
215 if (!ses->iface_count) {
216 spin_unlock(&ses->iface_lock);
220 list_for_each_entry_safe_from(iface, niface, &ses->iface_list,
222 /* skip ifaces that are unusable */
223 if (!iface->is_active ||
224 (is_ses_using_iface(ses, iface) &&
225 !iface->rss_capable)) {
229 /* take ref before unlock */
230 kref_get(&iface->refcount);
232 spin_unlock(&ses->iface_lock);
233 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
234 spin_lock(&ses->iface_lock);
237 cifs_dbg(VFS, "failed to open extra channel on iface:%pIS rc=%d\n",
240 kref_put(&iface->refcount, release_iface);
244 cifs_dbg(FYI, "successfully opened new channel on iface:%pIS\n",
248 spin_unlock(&ses->iface_lock);
254 return new_chan_count - old_chan_count;
258 * update the iface for the channel if necessary.
259 * will return 0 when iface is updated, 1 if removed, 2 otherwise
260 * Must be called with chan_lock held.
263 cifs_chan_update_iface(struct cifs_ses *ses, struct TCP_Server_Info *server)
265 unsigned int chan_index;
266 struct cifs_server_iface *iface = NULL;
267 struct cifs_server_iface *old_iface = NULL;
270 spin_lock(&ses->chan_lock);
271 chan_index = cifs_ses_get_chan_index(ses, server);
273 spin_unlock(&ses->chan_lock);
277 if (ses->chans[chan_index].iface) {
278 old_iface = ses->chans[chan_index].iface;
279 if (old_iface->is_active) {
280 spin_unlock(&ses->chan_lock);
284 spin_unlock(&ses->chan_lock);
286 spin_lock(&ses->iface_lock);
287 /* then look for a new one */
288 list_for_each_entry(iface, &ses->iface_list, iface_head) {
289 if (!iface->is_active ||
290 (is_ses_using_iface(ses, iface) &&
291 !iface->rss_capable)) {
294 kref_get(&iface->refcount);
297 if (!list_entry_is_head(iface, &ses->iface_list, iface_head)) {
300 cifs_dbg(FYI, "unable to find a suitable iface\n");
303 /* now drop the ref to the current iface */
304 if (old_iface && iface) {
305 kref_put(&old_iface->refcount, release_iface);
306 cifs_dbg(FYI, "replacing iface: %pIS with %pIS\n",
307 &old_iface->sockaddr,
309 } else if (old_iface) {
310 kref_put(&old_iface->refcount, release_iface);
311 cifs_dbg(FYI, "releasing ref to iface: %pIS\n",
312 &old_iface->sockaddr);
315 cifs_dbg(FYI, "adding new iface: %pIS\n", &iface->sockaddr);
317 spin_unlock(&ses->iface_lock);
319 spin_lock(&ses->chan_lock);
320 chan_index = cifs_ses_get_chan_index(ses, server);
321 ses->chans[chan_index].iface = iface;
323 /* No iface is found. if secondary chan, drop connection */
324 if (!iface && CIFS_SERVER_IS_CHAN(server))
325 ses->chans[chan_index].server = NULL;
327 spin_unlock(&ses->chan_lock);
329 if (!iface && CIFS_SERVER_IS_CHAN(server))
330 cifs_put_tcp_session(server, false);
336 * If server is a channel of ses, return the corresponding enclosing
337 * cifs_chan otherwise return NULL.
340 cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
344 spin_lock(&ses->chan_lock);
345 for (i = 0; i < ses->chan_count; i++) {
346 if (ses->chans[i].server == server) {
347 spin_unlock(&ses->chan_lock);
348 return &ses->chans[i];
351 spin_unlock(&ses->chan_lock);
356 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
357 struct cifs_server_iface *iface)
359 struct TCP_Server_Info *chan_server;
360 struct cifs_chan *chan;
361 struct smb3_fs_context ctx = {NULL};
362 static const char unc_fmt[] = "\\%s\\foo";
363 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
364 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
365 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
367 unsigned int xid = get_xid();
369 if (iface->sockaddr.ss_family == AF_INET)
370 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
371 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
374 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
375 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
379 * Setup a ctx with mostly the same info as the existing
380 * session and overwrite it with the requested iface data.
382 * We need to setup at least the fields used for negprot and
385 * We only need the ctx here, so we can reuse memory from
386 * the session and server without caring about memory
390 /* Always make new connection for now (TODO?) */
391 ctx.nosharesock = true;
394 ctx.domainauto = ses->domainAuto;
395 ctx.domainname = ses->domainName;
397 /* no hostname for extra channels */
398 ctx.server_hostname = "";
400 ctx.username = ses->user_name;
401 ctx.password = ses->password;
402 ctx.sectype = ses->sectype;
403 ctx.sign = ses->sign;
406 /* XXX: Use ses->server->hostname? */
407 sprintf(unc, unc_fmt, ses->ip_addr);
411 /* Reuse same version as master connection */
412 ctx.vals = ses->server->vals;
413 ctx.ops = ses->server->ops;
415 ctx.noblocksnd = ses->server->noblocksnd;
416 ctx.noautotune = ses->server->noautotune;
417 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
418 ctx.echo_interval = ses->server->echo_interval / HZ;
419 ctx.max_credits = ses->server->max_credits;
422 * This will be used for encoding/decoding user/domain/pw
423 * during sess setup auth.
425 ctx.local_nls = cifs_sb->local_nls;
427 /* Use RDMA if possible */
428 ctx.rdma = iface->rdma_capable;
429 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
431 /* reuse master con client guid */
432 memcpy(&ctx.client_guid, ses->server->client_guid,
433 SMB2_CLIENT_GUID_SIZE);
434 ctx.use_client_guid = true;
436 chan_server = cifs_get_tcp_session(&ctx, ses->server);
438 spin_lock(&ses->chan_lock);
439 chan = &ses->chans[ses->chan_count];
440 chan->server = chan_server;
441 if (IS_ERR(chan->server)) {
442 rc = PTR_ERR(chan->server);
444 spin_unlock(&ses->chan_lock);
449 atomic_set(&ses->chan_seq, 0);
451 /* Mark this channel as needing connect/setup */
452 cifs_chan_set_need_reconnect(ses, chan->server);
454 spin_unlock(&ses->chan_lock);
456 mutex_lock(&ses->session_mutex);
458 * We need to allocate the server crypto now as we will need
459 * to sign packets before we generate the channel signing key
460 * (we sign with the session key)
462 rc = smb311_crypto_shash_allocate(chan->server);
464 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
465 mutex_unlock(&ses->session_mutex);
469 rc = cifs_negotiate_protocol(xid, ses, chan->server);
471 rc = cifs_setup_session(xid, ses, chan->server, cifs_sb->local_nls);
473 mutex_unlock(&ses->session_mutex);
476 if (rc && chan->server) {
477 spin_lock(&ses->chan_lock);
478 /* we rely on all bits beyond chan_count to be clear */
479 cifs_chan_clear_need_reconnect(ses, chan->server);
482 * chan_count should never reach 0 as at least the primary
483 * channel is always allocated
485 WARN_ON(ses->chan_count < 1);
486 spin_unlock(&ses->chan_lock);
489 if (rc && chan->server)
490 cifs_put_tcp_session(chan->server, 0);
495 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses,
496 struct TCP_Server_Info *server,
497 SESSION_SETUP_ANDX *pSMB)
499 __u32 capabilities = 0;
501 /* init fields common to all four types of SessSetup */
502 /* Note that offsets for first seven fields in req struct are same */
503 /* in CIFS Specs so does not matter which of 3 forms of struct */
504 /* that we use in next few lines */
505 /* Note that header is initialized to zero in header_assemble */
506 pSMB->req.AndXCommand = 0xFF;
507 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
508 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
510 pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq);
511 pSMB->req.VcNumber = cpu_to_le16(1);
513 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
515 /* BB verify whether signing required on neg or just on auth frame
518 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
519 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
522 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
524 if (ses->capabilities & CAP_UNICODE) {
525 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
526 capabilities |= CAP_UNICODE;
528 if (ses->capabilities & CAP_STATUS32) {
529 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
530 capabilities |= CAP_STATUS32;
532 if (ses->capabilities & CAP_DFS) {
533 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
534 capabilities |= CAP_DFS;
536 if (ses->capabilities & CAP_UNIX)
537 capabilities |= CAP_UNIX;
543 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
545 char *bcc_ptr = *pbcc_area;
548 /* Copy OS version */
549 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
551 bcc_ptr += 2 * bytes_ret;
552 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
554 bcc_ptr += 2 * bytes_ret;
555 bcc_ptr += 2; /* trailing null */
557 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
559 bcc_ptr += 2 * bytes_ret;
560 bcc_ptr += 2; /* trailing null */
562 *pbcc_area = bcc_ptr;
565 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
566 const struct nls_table *nls_cp)
568 char *bcc_ptr = *pbcc_area;
572 if (ses->domainName == NULL) {
573 /* Sending null domain better than using a bogus domain name (as
574 we did briefly in 2.6.18) since server will use its default */
579 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
580 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
581 bcc_ptr += 2 * bytes_ret;
582 bcc_ptr += 2; /* account for null terminator */
584 *pbcc_area = bcc_ptr;
588 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
589 const struct nls_table *nls_cp)
591 char *bcc_ptr = *pbcc_area;
594 /* BB FIXME add check that strings total less
595 than 335 or will need to send them as arrays */
597 /* unicode strings, must be word aligned before the call */
598 /* if ((long) bcc_ptr % 2) {
603 if (ses->user_name == NULL) {
604 /* null user mount */
608 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
609 CIFS_MAX_USERNAME_LEN, nls_cp);
611 bcc_ptr += 2 * bytes_ret;
612 bcc_ptr += 2; /* account for null termination */
614 unicode_domain_string(&bcc_ptr, ses, nls_cp);
615 unicode_oslm_strings(&bcc_ptr, nls_cp);
617 *pbcc_area = bcc_ptr;
620 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
621 const struct nls_table *nls_cp)
623 char *bcc_ptr = *pbcc_area;
627 /* BB what about null user mounts - check that we do this BB */
629 if (ses->user_name != NULL) {
630 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
631 if (WARN_ON_ONCE(len < 0))
632 len = CIFS_MAX_USERNAME_LEN - 1;
635 /* else null user mount */
637 bcc_ptr++; /* account for null termination */
640 if (ses->domainName != NULL) {
641 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
642 if (WARN_ON_ONCE(len < 0))
643 len = CIFS_MAX_DOMAINNAME_LEN - 1;
645 } /* else we will send a null domain name
646 so the server will default to its own domain */
650 /* BB check for overflow here */
652 strcpy(bcc_ptr, "Linux version ");
653 bcc_ptr += strlen("Linux version ");
654 strcpy(bcc_ptr, init_utsname()->release);
655 bcc_ptr += strlen(init_utsname()->release) + 1;
657 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
658 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
660 *pbcc_area = bcc_ptr;
664 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
665 const struct nls_table *nls_cp)
668 char *data = *pbcc_area;
670 cifs_dbg(FYI, "bleft %d\n", bleft);
672 kfree(ses->serverOS);
673 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
674 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
675 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
681 kfree(ses->serverNOS);
682 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
683 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
684 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
690 kfree(ses->serverDomain);
691 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
692 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
697 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
698 struct cifs_ses *ses,
699 const struct nls_table *nls_cp)
702 char *bcc_ptr = *pbcc_area;
704 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
706 len = strnlen(bcc_ptr, bleft);
710 kfree(ses->serverOS);
712 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
714 memcpy(ses->serverOS, bcc_ptr, len);
715 ses->serverOS[len] = 0;
716 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
717 cifs_dbg(FYI, "OS/2 server\n");
723 len = strnlen(bcc_ptr, bleft);
727 kfree(ses->serverNOS);
729 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
730 if (ses->serverNOS) {
731 memcpy(ses->serverNOS, bcc_ptr, len);
732 ses->serverNOS[len] = 0;
738 len = strnlen(bcc_ptr, bleft);
742 /* No domain field in LANMAN case. Domain is
743 returned by old servers in the SMB negprot response */
744 /* BB For newer servers which do not support Unicode,
745 but thus do return domain here we could add parsing
746 for it later, but it is not very important */
747 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
750 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
751 struct cifs_ses *ses)
753 unsigned int tioffset; /* challenge message target info area */
754 unsigned int tilen; /* challenge message target info area length */
755 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
758 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
759 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
763 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
764 cifs_dbg(VFS, "blob signature incorrect %s\n",
768 if (pblob->MessageType != NtLmChallenge) {
769 cifs_dbg(VFS, "Incorrect message type %d\n",
774 server_flags = le32_to_cpu(pblob->NegotiateFlags);
775 cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__,
776 ses->ntlmssp->client_flags, server_flags);
778 if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) &&
779 (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) {
780 cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n",
784 if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) {
785 cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__);
788 if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) {
789 cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n",
793 if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
794 !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH))
795 pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n",
798 ses->ntlmssp->server_flags = server_flags;
800 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
801 /* In particular we can examine sign flags */
802 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
803 we must set the MIC field of the AUTHENTICATE_MESSAGE */
805 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
806 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
807 if (tioffset > blob_len || tioffset + tilen > blob_len) {
808 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
813 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
815 if (!ses->auth_key.response) {
816 cifs_dbg(VFS, "Challenge target info alloc failure\n");
819 ses->auth_key.len = tilen;
825 static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
827 int sz = base_size + ses->auth_key.len
828 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
831 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
833 sz += sizeof(__le16);
836 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
838 sz += sizeof(__le16);
840 if (ses->workstation_name[0])
841 sz += sizeof(__le16) * strnlen(ses->workstation_name,
842 ntlmssp_workstation_name_size(ses));
844 sz += sizeof(__le16);
849 static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
852 unsigned char *pstart,
853 unsigned char **pcur,
854 const struct nls_table *nls_cp)
856 unsigned char *tmp = pstart;
866 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
868 pbuf->MaximumLength = 0;
869 *pcur += sizeof(__le16);
871 len = cifs_strtoUTF16((__le16 *)*pcur,
875 len *= sizeof(__le16);
876 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
877 pbuf->Length = cpu_to_le16(len);
878 pbuf->MaximumLength = cpu_to_le16(len);
883 /* BB Move to ntlmssp.c eventually */
885 int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
887 struct cifs_ses *ses,
888 struct TCP_Server_Info *server,
889 const struct nls_table *nls_cp)
892 NEGOTIATE_MESSAGE *sec_blob;
897 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
898 *pbuffer = kmalloc(len, GFP_KERNEL);
901 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
903 goto setup_ntlm_neg_ret;
905 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
907 memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
908 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
909 sec_blob->MessageType = NtLmNegotiate;
911 /* BB is NTLMV2 session security format easier to use here? */
912 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
913 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
914 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
915 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
916 NTLMSSP_NEGOTIATE_SIGN;
917 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
918 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
920 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
921 ses->ntlmssp->client_flags = flags;
922 sec_blob->NegotiateFlags = cpu_to_le32(flags);
924 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
925 cifs_security_buffer_from_str(&sec_blob->DomainName,
927 CIFS_MAX_DOMAINNAME_LEN,
931 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
933 CIFS_MAX_WORKSTATION_LEN,
937 *buflen = tmp - *pbuffer;
943 * Build ntlmssp blob with additional fields, such as version,
944 * supported by modern servers. For safety limit to SMB3 or later
945 * See notes in MS-NLMP Section 2.2.2.1 e.g.
947 int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer,
949 struct cifs_ses *ses,
950 struct TCP_Server_Info *server,
951 const struct nls_table *nls_cp)
954 struct negotiate_message *sec_blob;
959 len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message));
960 *pbuffer = kmalloc(len, GFP_KERNEL);
963 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
965 goto setup_ntlm_smb3_neg_ret;
967 sec_blob = (struct negotiate_message *)*pbuffer;
969 memset(*pbuffer, 0, sizeof(struct negotiate_message));
970 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
971 sec_blob->MessageType = NtLmNegotiate;
973 /* BB is NTLMV2 session security format easier to use here? */
974 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
975 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
976 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
977 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
978 NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION;
979 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
980 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
982 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
983 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
984 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
985 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
987 tmp = *pbuffer + sizeof(struct negotiate_message);
988 ses->ntlmssp->client_flags = flags;
989 sec_blob->NegotiateFlags = cpu_to_le32(flags);
991 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
992 cifs_security_buffer_from_str(&sec_blob->DomainName,
994 CIFS_MAX_DOMAINNAME_LEN,
998 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1000 CIFS_MAX_WORKSTATION_LEN,
1004 *buflen = tmp - *pbuffer;
1005 setup_ntlm_smb3_neg_ret:
1010 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
1012 struct cifs_ses *ses,
1013 struct TCP_Server_Info *server,
1014 const struct nls_table *nls_cp)
1017 AUTHENTICATE_MESSAGE *sec_blob;
1022 rc = setup_ntlmv2_rsp(ses, nls_cp);
1024 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
1026 goto setup_ntlmv2_ret;
1029 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
1030 *pbuffer = kmalloc(len, GFP_KERNEL);
1033 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
1035 goto setup_ntlmv2_ret;
1037 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
1039 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1040 sec_blob->MessageType = NtLmAuthenticate;
1042 flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET |
1043 NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
1045 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
1046 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1048 sec_blob->LmChallengeResponse.BufferOffset =
1049 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
1050 sec_blob->LmChallengeResponse.Length = 0;
1051 sec_blob->LmChallengeResponse.MaximumLength = 0;
1053 sec_blob->NtChallengeResponse.BufferOffset =
1054 cpu_to_le32(tmp - *pbuffer);
1055 if (ses->user_name != NULL) {
1056 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1057 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1058 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1060 sec_blob->NtChallengeResponse.Length =
1061 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1062 sec_blob->NtChallengeResponse.MaximumLength =
1063 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1066 * don't send an NT Response for anonymous access
1068 sec_blob->NtChallengeResponse.Length = 0;
1069 sec_blob->NtChallengeResponse.MaximumLength = 0;
1072 cifs_security_buffer_from_str(&sec_blob->DomainName,
1074 CIFS_MAX_DOMAINNAME_LEN,
1078 cifs_security_buffer_from_str(&sec_blob->UserName,
1080 CIFS_MAX_USERNAME_LEN,
1084 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1085 ses->workstation_name,
1086 ntlmssp_workstation_name_size(ses),
1090 if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
1091 (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) &&
1092 !calc_seckey(ses)) {
1093 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
1094 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1095 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
1096 sec_blob->SessionKey.MaximumLength =
1097 cpu_to_le16(CIFS_CPHTXT_SIZE);
1098 tmp += CIFS_CPHTXT_SIZE;
1100 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1101 sec_blob->SessionKey.Length = 0;
1102 sec_blob->SessionKey.MaximumLength = 0;
1105 *buflen = tmp - *pbuffer;
1111 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1113 switch (server->negflavor) {
1114 case CIFS_NEGFLAVOR_EXTENDED:
1115 switch (requested) {
1120 if (server->sec_ntlmssp &&
1121 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1123 if ((server->sec_kerberos || server->sec_mskerberos) &&
1124 (global_secflags & CIFSSEC_MAY_KRB5))
1130 case CIFS_NEGFLAVOR_UNENCAP:
1131 switch (requested) {
1135 if (global_secflags & CIFSSEC_MAY_NTLMV2)
1149 struct cifs_ses *ses;
1150 struct TCP_Server_Info *server;
1151 struct nls_table *nls_cp;
1152 void (*func)(struct sess_data *);
1155 /* we will send the SMB in three pieces:
1156 * a fixed length beginning part, an optional
1157 * SPNEGO blob (which can be zero length), and a
1158 * last part which will include the strings
1159 * and rest of bcc area. This allows us to avoid
1160 * a large buffer 17K allocation
1167 sess_alloc_buffer(struct sess_data *sess_data, int wct)
1170 struct cifs_ses *ses = sess_data->ses;
1171 struct smb_hdr *smb_buf;
1173 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
1179 sess_data->iov[0].iov_base = (char *)smb_buf;
1180 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
1182 * This variable will be used to clear the buffer
1183 * allocated above in case of any error in the calling function.
1185 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1187 /* 2000 big enough to fit max user, domain, NOS name etc. */
1188 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
1189 if (!sess_data->iov[2].iov_base) {
1191 goto out_free_smb_buf;
1197 cifs_small_buf_release(smb_buf);
1198 sess_data->iov[0].iov_base = NULL;
1199 sess_data->iov[0].iov_len = 0;
1200 sess_data->buf0_type = CIFS_NO_BUFFER;
1205 sess_free_buffer(struct sess_data *sess_data)
1208 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1209 sess_data->buf0_type = CIFS_NO_BUFFER;
1210 kfree(sess_data->iov[2].iov_base);
1214 sess_establish_session(struct sess_data *sess_data)
1216 struct cifs_ses *ses = sess_data->ses;
1217 struct TCP_Server_Info *server = sess_data->server;
1219 cifs_server_lock(server);
1220 if (!server->session_estab) {
1222 server->session_key.response =
1223 kmemdup(ses->auth_key.response,
1224 ses->auth_key.len, GFP_KERNEL);
1225 if (!server->session_key.response) {
1226 cifs_server_unlock(server);
1229 server->session_key.len =
1232 server->sequence_number = 0x2;
1233 server->session_estab = true;
1235 cifs_server_unlock(server);
1237 cifs_dbg(FYI, "CIFS session established successfully\n");
1242 sess_sendreceive(struct sess_data *sess_data)
1245 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
1247 struct kvec rsp_iov = { NULL, 0 };
1249 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
1250 be32_add_cpu(&smb_buf->smb_buf_length, count);
1251 put_bcc(count, smb_buf);
1253 rc = SendReceive2(sess_data->xid, sess_data->ses,
1254 sess_data->iov, 3 /* num_iovecs */,
1255 &sess_data->buf0_type,
1256 CIFS_LOG_ERROR, &rsp_iov);
1257 cifs_small_buf_release(sess_data->iov[0].iov_base);
1258 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1264 sess_auth_ntlmv2(struct sess_data *sess_data)
1267 struct smb_hdr *smb_buf;
1268 SESSION_SETUP_ANDX *pSMB;
1270 struct cifs_ses *ses = sess_data->ses;
1271 struct TCP_Server_Info *server = sess_data->server;
1273 __u16 bytes_remaining;
1275 /* old style NTLM sessionsetup */
1277 rc = sess_alloc_buffer(sess_data, 13);
1281 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1282 bcc_ptr = sess_data->iov[2].iov_base;
1283 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1285 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1287 /* LM2 password would be here if we supported it */
1288 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1290 if (ses->user_name != NULL) {
1291 /* calculate nlmv2 response and session key */
1292 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1294 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1298 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1299 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1300 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1302 /* set case sensitive password length after tilen may get
1303 * assigned, tilen is 0 otherwise.
1305 pSMB->req_no_secext.CaseSensitivePasswordLength =
1306 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1308 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1311 if (ses->capabilities & CAP_UNICODE) {
1312 if (sess_data->iov[0].iov_len % 2) {
1316 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1318 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1322 sess_data->iov[2].iov_len = (long) bcc_ptr -
1323 (long) sess_data->iov[2].iov_base;
1325 rc = sess_sendreceive(sess_data);
1329 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1330 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1332 if (smb_buf->WordCount != 3) {
1334 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1338 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1339 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1341 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1342 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1344 bytes_remaining = get_bcc(smb_buf);
1345 bcc_ptr = pByteArea(smb_buf);
1347 /* BB check if Unicode and decode strings */
1348 if (bytes_remaining == 0) {
1349 /* no string area to decode, do nothing */
1350 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1351 /* unicode string area must be word-aligned */
1352 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1356 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1359 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1363 rc = sess_establish_session(sess_data);
1365 sess_data->result = rc;
1366 sess_data->func = NULL;
1367 sess_free_buffer(sess_data);
1368 kfree(ses->auth_key.response);
1369 ses->auth_key.response = NULL;
1372 #ifdef CONFIG_CIFS_UPCALL
1374 sess_auth_kerberos(struct sess_data *sess_data)
1377 struct smb_hdr *smb_buf;
1378 SESSION_SETUP_ANDX *pSMB;
1380 struct cifs_ses *ses = sess_data->ses;
1381 struct TCP_Server_Info *server = sess_data->server;
1383 __u16 bytes_remaining;
1384 struct key *spnego_key = NULL;
1385 struct cifs_spnego_msg *msg;
1388 /* extended security */
1390 rc = sess_alloc_buffer(sess_data, 12);
1394 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1395 bcc_ptr = sess_data->iov[2].iov_base;
1396 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1398 spnego_key = cifs_get_spnego_key(ses, server);
1399 if (IS_ERR(spnego_key)) {
1400 rc = PTR_ERR(spnego_key);
1405 msg = spnego_key->payload.data[0];
1407 * check version field to make sure that cifs.upcall is
1408 * sending us a response in an expected form
1410 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1411 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1412 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1414 goto out_put_spnego_key;
1417 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1419 if (!ses->auth_key.response) {
1420 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1423 goto out_put_spnego_key;
1425 ses->auth_key.len = msg->sesskey_len;
1427 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1428 capabilities |= CAP_EXTENDED_SECURITY;
1429 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1430 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1431 sess_data->iov[1].iov_len = msg->secblob_len;
1432 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1434 if (ses->capabilities & CAP_UNICODE) {
1435 /* unicode strings must be word aligned */
1436 if ((sess_data->iov[0].iov_len
1437 + sess_data->iov[1].iov_len) % 2) {
1441 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1442 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1444 /* BB: is this right? */
1445 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1448 sess_data->iov[2].iov_len = (long) bcc_ptr -
1449 (long) sess_data->iov[2].iov_base;
1451 rc = sess_sendreceive(sess_data);
1453 goto out_put_spnego_key;
1455 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1456 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1458 if (smb_buf->WordCount != 4) {
1460 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1461 goto out_put_spnego_key;
1464 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1465 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1467 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1468 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1470 bytes_remaining = get_bcc(smb_buf);
1471 bcc_ptr = pByteArea(smb_buf);
1473 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1474 if (blob_len > bytes_remaining) {
1475 cifs_dbg(VFS, "bad security blob length %d\n",
1478 goto out_put_spnego_key;
1480 bcc_ptr += blob_len;
1481 bytes_remaining -= blob_len;
1483 /* BB check if Unicode and decode strings */
1484 if (bytes_remaining == 0) {
1485 /* no string area to decode, do nothing */
1486 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1487 /* unicode string area must be word-aligned */
1488 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1492 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1495 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1499 rc = sess_establish_session(sess_data);
1501 key_invalidate(spnego_key);
1502 key_put(spnego_key);
1504 sess_data->result = rc;
1505 sess_data->func = NULL;
1506 sess_free_buffer(sess_data);
1507 kfree(ses->auth_key.response);
1508 ses->auth_key.response = NULL;
1511 #endif /* ! CONFIG_CIFS_UPCALL */
1514 * The required kvec buffers have to be allocated before calling this
1518 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1520 SESSION_SETUP_ANDX *pSMB;
1521 struct cifs_ses *ses = sess_data->ses;
1522 struct TCP_Server_Info *server = sess_data->server;
1526 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1528 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1529 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1530 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1534 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1535 capabilities |= CAP_EXTENDED_SECURITY;
1536 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1538 bcc_ptr = sess_data->iov[2].iov_base;
1539 /* unicode strings must be word aligned */
1540 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1544 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1546 sess_data->iov[2].iov_len = (long) bcc_ptr -
1547 (long) sess_data->iov[2].iov_base;
1553 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1556 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1559 struct smb_hdr *smb_buf;
1560 SESSION_SETUP_ANDX *pSMB;
1561 struct cifs_ses *ses = sess_data->ses;
1562 struct TCP_Server_Info *server = sess_data->server;
1563 __u16 bytes_remaining;
1565 unsigned char *ntlmsspblob = NULL;
1568 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1571 * if memory allocation is successful, caller of this function
1574 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1575 if (!ses->ntlmssp) {
1579 ses->ntlmssp->sesskey_per_smbsess = false;
1582 rc = sess_alloc_buffer(sess_data, 12);
1586 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1588 /* Build security blob before we assemble the request */
1589 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
1590 &blob_len, ses, server,
1593 goto out_free_ntlmsspblob;
1595 sess_data->iov[1].iov_len = blob_len;
1596 sess_data->iov[1].iov_base = ntlmsspblob;
1597 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1599 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1601 goto out_free_ntlmsspblob;
1603 rc = sess_sendreceive(sess_data);
1605 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1606 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1608 /* If true, rc here is expected and not an error */
1609 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1610 smb_buf->Status.CifsError ==
1611 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1615 goto out_free_ntlmsspblob;
1617 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1619 if (smb_buf->WordCount != 4) {
1621 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1622 goto out_free_ntlmsspblob;
1625 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1626 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1628 bytes_remaining = get_bcc(smb_buf);
1629 bcc_ptr = pByteArea(smb_buf);
1631 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1632 if (blob_len > bytes_remaining) {
1633 cifs_dbg(VFS, "bad security blob length %d\n",
1636 goto out_free_ntlmsspblob;
1639 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1641 out_free_ntlmsspblob:
1644 sess_free_buffer(sess_data);
1647 sess_data->func = sess_auth_rawntlmssp_authenticate;
1651 /* Else error. Cleanup */
1652 kfree(ses->auth_key.response);
1653 ses->auth_key.response = NULL;
1654 kfree(ses->ntlmssp);
1655 ses->ntlmssp = NULL;
1657 sess_data->func = NULL;
1658 sess_data->result = rc;
1662 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1665 struct smb_hdr *smb_buf;
1666 SESSION_SETUP_ANDX *pSMB;
1667 struct cifs_ses *ses = sess_data->ses;
1668 struct TCP_Server_Info *server = sess_data->server;
1669 __u16 bytes_remaining;
1671 unsigned char *ntlmsspblob = NULL;
1674 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1677 rc = sess_alloc_buffer(sess_data, 12);
1681 /* Build security blob before we assemble the request */
1682 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1683 smb_buf = (struct smb_hdr *)pSMB;
1684 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1685 &blob_len, ses, server,
1688 goto out_free_ntlmsspblob;
1689 sess_data->iov[1].iov_len = blob_len;
1690 sess_data->iov[1].iov_base = ntlmsspblob;
1691 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1693 * Make sure that we tell the server that we are using
1694 * the uid that it just gave us back on the response
1697 smb_buf->Uid = ses->Suid;
1699 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1701 goto out_free_ntlmsspblob;
1703 rc = sess_sendreceive(sess_data);
1705 goto out_free_ntlmsspblob;
1707 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1708 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1709 if (smb_buf->WordCount != 4) {
1711 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1712 goto out_free_ntlmsspblob;
1715 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1716 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1718 if (ses->Suid != smb_buf->Uid) {
1719 ses->Suid = smb_buf->Uid;
1720 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1723 bytes_remaining = get_bcc(smb_buf);
1724 bcc_ptr = pByteArea(smb_buf);
1725 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1726 if (blob_len > bytes_remaining) {
1727 cifs_dbg(VFS, "bad security blob length %d\n",
1730 goto out_free_ntlmsspblob;
1732 bcc_ptr += blob_len;
1733 bytes_remaining -= blob_len;
1736 /* BB check if Unicode and decode strings */
1737 if (bytes_remaining == 0) {
1738 /* no string area to decode, do nothing */
1739 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1740 /* unicode string area must be word-aligned */
1741 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1745 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1748 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1752 out_free_ntlmsspblob:
1755 sess_free_buffer(sess_data);
1758 rc = sess_establish_session(sess_data);
1761 kfree(ses->auth_key.response);
1762 ses->auth_key.response = NULL;
1763 kfree(ses->ntlmssp);
1764 ses->ntlmssp = NULL;
1766 sess_data->func = NULL;
1767 sess_data->result = rc;
1770 static int select_sec(struct sess_data *sess_data)
1773 struct cifs_ses *ses = sess_data->ses;
1774 struct TCP_Server_Info *server = sess_data->server;
1776 type = cifs_select_sectype(server, ses->sectype);
1777 cifs_dbg(FYI, "sess setup type %d\n", type);
1778 if (type == Unspecified) {
1779 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1785 sess_data->func = sess_auth_ntlmv2;
1788 #ifdef CONFIG_CIFS_UPCALL
1789 sess_data->func = sess_auth_kerberos;
1792 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1794 #endif /* CONFIG_CIFS_UPCALL */
1796 sess_data->func = sess_auth_rawntlmssp_negotiate;
1799 cifs_dbg(VFS, "secType %d not supported!\n", type);
1806 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1807 struct TCP_Server_Info *server,
1808 const struct nls_table *nls_cp)
1811 struct sess_data *sess_data;
1814 WARN(1, "%s: ses == NULL!", __func__);
1818 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1822 sess_data->xid = xid;
1823 sess_data->ses = ses;
1824 sess_data->server = server;
1825 sess_data->buf0_type = CIFS_NO_BUFFER;
1826 sess_data->nls_cp = (struct nls_table *) nls_cp;
1828 rc = select_sec(sess_data);
1832 while (sess_data->func)
1833 sess_data->func(sess_data);
1835 /* Store result before we free sess_data */
1836 rc = sess_data->result;