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) {
478 * we should avoid race with these delayed works before we
479 * remove this channel
481 cancel_delayed_work_sync(&chan->server->echo);
482 cancel_delayed_work_sync(&chan->server->resolve);
483 cancel_delayed_work_sync(&chan->server->reconnect);
485 spin_lock(&ses->chan_lock);
486 /* we rely on all bits beyond chan_count to be clear */
487 cifs_chan_clear_need_reconnect(ses, chan->server);
490 * chan_count should never reach 0 as at least the primary
491 * channel is always allocated
493 WARN_ON(ses->chan_count < 1);
494 spin_unlock(&ses->chan_lock);
496 cifs_put_tcp_session(chan->server, 0);
502 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
503 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses,
504 struct TCP_Server_Info *server,
505 SESSION_SETUP_ANDX *pSMB)
507 __u32 capabilities = 0;
509 /* init fields common to all four types of SessSetup */
510 /* Note that offsets for first seven fields in req struct are same */
511 /* in CIFS Specs so does not matter which of 3 forms of struct */
512 /* that we use in next few lines */
513 /* Note that header is initialized to zero in header_assemble */
514 pSMB->req.AndXCommand = 0xFF;
515 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
516 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
518 pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq);
519 pSMB->req.VcNumber = cpu_to_le16(1);
521 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
523 /* BB verify whether signing required on neg or just on auth frame
526 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
527 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
530 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
532 if (ses->capabilities & CAP_UNICODE) {
533 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
534 capabilities |= CAP_UNICODE;
536 if (ses->capabilities & CAP_STATUS32) {
537 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
538 capabilities |= CAP_STATUS32;
540 if (ses->capabilities & CAP_DFS) {
541 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
542 capabilities |= CAP_DFS;
544 if (ses->capabilities & CAP_UNIX)
545 capabilities |= CAP_UNIX;
551 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
553 char *bcc_ptr = *pbcc_area;
556 /* Copy OS version */
557 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
559 bcc_ptr += 2 * bytes_ret;
560 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
562 bcc_ptr += 2 * bytes_ret;
563 bcc_ptr += 2; /* trailing null */
565 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
567 bcc_ptr += 2 * bytes_ret;
568 bcc_ptr += 2; /* trailing null */
570 *pbcc_area = bcc_ptr;
573 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
574 const struct nls_table *nls_cp)
576 char *bcc_ptr = *pbcc_area;
580 if (ses->domainName == NULL) {
581 /* Sending null domain better than using a bogus domain name (as
582 we did briefly in 2.6.18) since server will use its default */
587 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
588 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
589 bcc_ptr += 2 * bytes_ret;
590 bcc_ptr += 2; /* account for null terminator */
592 *pbcc_area = bcc_ptr;
595 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
596 const struct nls_table *nls_cp)
598 char *bcc_ptr = *pbcc_area;
601 /* BB FIXME add check that strings total less
602 than 335 or will need to send them as arrays */
604 /* unicode strings, must be word aligned before the call */
605 /* if ((long) bcc_ptr % 2) {
610 if (ses->user_name == NULL) {
611 /* null user mount */
615 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
616 CIFS_MAX_USERNAME_LEN, nls_cp);
618 bcc_ptr += 2 * bytes_ret;
619 bcc_ptr += 2; /* account for null termination */
621 unicode_domain_string(&bcc_ptr, ses, nls_cp);
622 unicode_oslm_strings(&bcc_ptr, nls_cp);
624 *pbcc_area = bcc_ptr;
627 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
628 const struct nls_table *nls_cp)
630 char *bcc_ptr = *pbcc_area;
634 /* BB what about null user mounts - check that we do this BB */
636 if (ses->user_name != NULL) {
637 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
638 if (WARN_ON_ONCE(len < 0))
639 len = CIFS_MAX_USERNAME_LEN - 1;
642 /* else null user mount */
644 bcc_ptr++; /* account for null termination */
647 if (ses->domainName != NULL) {
648 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
649 if (WARN_ON_ONCE(len < 0))
650 len = CIFS_MAX_DOMAINNAME_LEN - 1;
652 } /* else we will send a null domain name
653 so the server will default to its own domain */
657 /* BB check for overflow here */
659 strcpy(bcc_ptr, "Linux version ");
660 bcc_ptr += strlen("Linux version ");
661 strcpy(bcc_ptr, init_utsname()->release);
662 bcc_ptr += strlen(init_utsname()->release) + 1;
664 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
665 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
667 *pbcc_area = bcc_ptr;
671 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
672 const struct nls_table *nls_cp)
675 char *data = *pbcc_area;
677 cifs_dbg(FYI, "bleft %d\n", bleft);
679 kfree(ses->serverOS);
680 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
681 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
682 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
688 kfree(ses->serverNOS);
689 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
690 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
691 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
697 kfree(ses->serverDomain);
698 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
699 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
704 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
705 struct cifs_ses *ses,
706 const struct nls_table *nls_cp)
709 char *bcc_ptr = *pbcc_area;
711 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
713 len = strnlen(bcc_ptr, bleft);
717 kfree(ses->serverOS);
719 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
721 memcpy(ses->serverOS, bcc_ptr, len);
722 ses->serverOS[len] = 0;
723 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
724 cifs_dbg(FYI, "OS/2 server\n");
730 len = strnlen(bcc_ptr, bleft);
734 kfree(ses->serverNOS);
736 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
737 if (ses->serverNOS) {
738 memcpy(ses->serverNOS, bcc_ptr, len);
739 ses->serverNOS[len] = 0;
745 len = strnlen(bcc_ptr, bleft);
749 /* No domain field in LANMAN case. Domain is
750 returned by old servers in the SMB negprot response */
751 /* BB For newer servers which do not support Unicode,
752 but thus do return domain here we could add parsing
753 for it later, but it is not very important */
754 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
756 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
758 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
759 struct cifs_ses *ses)
761 unsigned int tioffset; /* challenge message target info area */
762 unsigned int tilen; /* challenge message target info area length */
763 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
766 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
767 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
771 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
772 cifs_dbg(VFS, "blob signature incorrect %s\n",
776 if (pblob->MessageType != NtLmChallenge) {
777 cifs_dbg(VFS, "Incorrect message type %d\n",
782 server_flags = le32_to_cpu(pblob->NegotiateFlags);
783 cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__,
784 ses->ntlmssp->client_flags, server_flags);
786 if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) &&
787 (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) {
788 cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n",
792 if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) {
793 cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__);
796 if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) {
797 cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n",
801 if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
802 !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH))
803 pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n",
806 ses->ntlmssp->server_flags = server_flags;
808 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
809 /* In particular we can examine sign flags */
810 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
811 we must set the MIC field of the AUTHENTICATE_MESSAGE */
813 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
814 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
815 if (tioffset > blob_len || tioffset + tilen > blob_len) {
816 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
821 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
823 if (!ses->auth_key.response) {
824 cifs_dbg(VFS, "Challenge target info alloc failure\n");
827 ses->auth_key.len = tilen;
833 static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
835 int sz = base_size + ses->auth_key.len
836 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
839 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
841 sz += sizeof(__le16);
844 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
846 sz += sizeof(__le16);
848 if (ses->workstation_name[0])
849 sz += sizeof(__le16) * strnlen(ses->workstation_name,
850 ntlmssp_workstation_name_size(ses));
852 sz += sizeof(__le16);
857 static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
860 unsigned char *pstart,
861 unsigned char **pcur,
862 const struct nls_table *nls_cp)
864 unsigned char *tmp = pstart;
874 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
876 pbuf->MaximumLength = 0;
877 *pcur += sizeof(__le16);
879 len = cifs_strtoUTF16((__le16 *)*pcur,
883 len *= sizeof(__le16);
884 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
885 pbuf->Length = cpu_to_le16(len);
886 pbuf->MaximumLength = cpu_to_le16(len);
891 /* BB Move to ntlmssp.c eventually */
893 int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
895 struct cifs_ses *ses,
896 struct TCP_Server_Info *server,
897 const struct nls_table *nls_cp)
900 NEGOTIATE_MESSAGE *sec_blob;
905 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
906 *pbuffer = kmalloc(len, GFP_KERNEL);
909 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
911 goto setup_ntlm_neg_ret;
913 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
915 memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
916 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
917 sec_blob->MessageType = NtLmNegotiate;
919 /* BB is NTLMV2 session security format easier to use here? */
920 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
921 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
922 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
923 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
924 NTLMSSP_NEGOTIATE_SIGN;
925 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
926 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
928 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
929 ses->ntlmssp->client_flags = flags;
930 sec_blob->NegotiateFlags = cpu_to_le32(flags);
932 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
933 cifs_security_buffer_from_str(&sec_blob->DomainName,
935 CIFS_MAX_DOMAINNAME_LEN,
939 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
941 CIFS_MAX_WORKSTATION_LEN,
945 *buflen = tmp - *pbuffer;
951 * Build ntlmssp blob with additional fields, such as version,
952 * supported by modern servers. For safety limit to SMB3 or later
953 * See notes in MS-NLMP Section 2.2.2.1 e.g.
955 int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer,
957 struct cifs_ses *ses,
958 struct TCP_Server_Info *server,
959 const struct nls_table *nls_cp)
962 struct negotiate_message *sec_blob;
967 len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message));
968 *pbuffer = kmalloc(len, GFP_KERNEL);
971 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
973 goto setup_ntlm_smb3_neg_ret;
975 sec_blob = (struct negotiate_message *)*pbuffer;
977 memset(*pbuffer, 0, sizeof(struct negotiate_message));
978 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
979 sec_blob->MessageType = NtLmNegotiate;
981 /* BB is NTLMV2 session security format easier to use here? */
982 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
983 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
984 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
985 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
986 NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION;
987 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
988 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
990 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
991 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
992 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
993 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
995 tmp = *pbuffer + sizeof(struct negotiate_message);
996 ses->ntlmssp->client_flags = flags;
997 sec_blob->NegotiateFlags = cpu_to_le32(flags);
999 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
1000 cifs_security_buffer_from_str(&sec_blob->DomainName,
1002 CIFS_MAX_DOMAINNAME_LEN,
1006 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1008 CIFS_MAX_WORKSTATION_LEN,
1012 *buflen = tmp - *pbuffer;
1013 setup_ntlm_smb3_neg_ret:
1018 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
1020 struct cifs_ses *ses,
1021 struct TCP_Server_Info *server,
1022 const struct nls_table *nls_cp)
1025 AUTHENTICATE_MESSAGE *sec_blob;
1030 rc = setup_ntlmv2_rsp(ses, nls_cp);
1032 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
1034 goto setup_ntlmv2_ret;
1037 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
1038 *pbuffer = kmalloc(len, GFP_KERNEL);
1041 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
1043 goto setup_ntlmv2_ret;
1045 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
1047 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1048 sec_blob->MessageType = NtLmAuthenticate;
1050 flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET |
1051 NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
1053 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
1054 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1056 sec_blob->LmChallengeResponse.BufferOffset =
1057 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
1058 sec_blob->LmChallengeResponse.Length = 0;
1059 sec_blob->LmChallengeResponse.MaximumLength = 0;
1061 sec_blob->NtChallengeResponse.BufferOffset =
1062 cpu_to_le32(tmp - *pbuffer);
1063 if (ses->user_name != NULL) {
1064 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1065 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1066 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1068 sec_blob->NtChallengeResponse.Length =
1069 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1070 sec_blob->NtChallengeResponse.MaximumLength =
1071 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1074 * don't send an NT Response for anonymous access
1076 sec_blob->NtChallengeResponse.Length = 0;
1077 sec_blob->NtChallengeResponse.MaximumLength = 0;
1080 cifs_security_buffer_from_str(&sec_blob->DomainName,
1082 CIFS_MAX_DOMAINNAME_LEN,
1086 cifs_security_buffer_from_str(&sec_blob->UserName,
1088 CIFS_MAX_USERNAME_LEN,
1092 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1093 ses->workstation_name,
1094 ntlmssp_workstation_name_size(ses),
1098 if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
1099 (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) &&
1100 !calc_seckey(ses)) {
1101 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
1102 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1103 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
1104 sec_blob->SessionKey.MaximumLength =
1105 cpu_to_le16(CIFS_CPHTXT_SIZE);
1106 tmp += CIFS_CPHTXT_SIZE;
1108 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1109 sec_blob->SessionKey.Length = 0;
1110 sec_blob->SessionKey.MaximumLength = 0;
1113 *buflen = tmp - *pbuffer;
1119 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1121 switch (server->negflavor) {
1122 case CIFS_NEGFLAVOR_EXTENDED:
1123 switch (requested) {
1128 if (server->sec_ntlmssp &&
1129 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1131 if ((server->sec_kerberos || server->sec_mskerberos) &&
1132 (global_secflags & CIFSSEC_MAY_KRB5))
1138 case CIFS_NEGFLAVOR_UNENCAP:
1139 switch (requested) {
1143 if (global_secflags & CIFSSEC_MAY_NTLMV2)
1157 struct cifs_ses *ses;
1158 struct TCP_Server_Info *server;
1159 struct nls_table *nls_cp;
1160 void (*func)(struct sess_data *);
1163 /* we will send the SMB in three pieces:
1164 * a fixed length beginning part, an optional
1165 * SPNEGO blob (which can be zero length), and a
1166 * last part which will include the strings
1167 * and rest of bcc area. This allows us to avoid
1168 * a large buffer 17K allocation
1174 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1176 sess_alloc_buffer(struct sess_data *sess_data, int wct)
1179 struct cifs_ses *ses = sess_data->ses;
1180 struct smb_hdr *smb_buf;
1182 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
1188 sess_data->iov[0].iov_base = (char *)smb_buf;
1189 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
1191 * This variable will be used to clear the buffer
1192 * allocated above in case of any error in the calling function.
1194 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1196 /* 2000 big enough to fit max user, domain, NOS name etc. */
1197 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
1198 if (!sess_data->iov[2].iov_base) {
1200 goto out_free_smb_buf;
1206 cifs_small_buf_release(smb_buf);
1207 sess_data->iov[0].iov_base = NULL;
1208 sess_data->iov[0].iov_len = 0;
1209 sess_data->buf0_type = CIFS_NO_BUFFER;
1214 sess_free_buffer(struct sess_data *sess_data)
1217 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1218 sess_data->buf0_type = CIFS_NO_BUFFER;
1219 kfree(sess_data->iov[2].iov_base);
1223 sess_establish_session(struct sess_data *sess_data)
1225 struct cifs_ses *ses = sess_data->ses;
1226 struct TCP_Server_Info *server = sess_data->server;
1228 cifs_server_lock(server);
1229 if (!server->session_estab) {
1231 server->session_key.response =
1232 kmemdup(ses->auth_key.response,
1233 ses->auth_key.len, GFP_KERNEL);
1234 if (!server->session_key.response) {
1235 cifs_server_unlock(server);
1238 server->session_key.len =
1241 server->sequence_number = 0x2;
1242 server->session_estab = true;
1244 cifs_server_unlock(server);
1246 cifs_dbg(FYI, "CIFS session established successfully\n");
1251 sess_sendreceive(struct sess_data *sess_data)
1254 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
1256 struct kvec rsp_iov = { NULL, 0 };
1258 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
1259 be32_add_cpu(&smb_buf->smb_buf_length, count);
1260 put_bcc(count, smb_buf);
1262 rc = SendReceive2(sess_data->xid, sess_data->ses,
1263 sess_data->iov, 3 /* num_iovecs */,
1264 &sess_data->buf0_type,
1265 CIFS_LOG_ERROR, &rsp_iov);
1266 cifs_small_buf_release(sess_data->iov[0].iov_base);
1267 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1273 sess_auth_ntlmv2(struct sess_data *sess_data)
1276 struct smb_hdr *smb_buf;
1277 SESSION_SETUP_ANDX *pSMB;
1279 struct cifs_ses *ses = sess_data->ses;
1280 struct TCP_Server_Info *server = sess_data->server;
1282 __u16 bytes_remaining;
1284 /* old style NTLM sessionsetup */
1286 rc = sess_alloc_buffer(sess_data, 13);
1290 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1291 bcc_ptr = sess_data->iov[2].iov_base;
1292 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1294 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1296 /* LM2 password would be here if we supported it */
1297 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1299 if (ses->user_name != NULL) {
1300 /* calculate nlmv2 response and session key */
1301 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1303 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1307 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1308 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1309 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1311 /* set case sensitive password length after tilen may get
1312 * assigned, tilen is 0 otherwise.
1314 pSMB->req_no_secext.CaseSensitivePasswordLength =
1315 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1317 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1320 if (ses->capabilities & CAP_UNICODE) {
1321 if (sess_data->iov[0].iov_len % 2) {
1325 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1327 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1331 sess_data->iov[2].iov_len = (long) bcc_ptr -
1332 (long) sess_data->iov[2].iov_base;
1334 rc = sess_sendreceive(sess_data);
1338 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1339 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1341 if (smb_buf->WordCount != 3) {
1343 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1347 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1348 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1350 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1351 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1353 bytes_remaining = get_bcc(smb_buf);
1354 bcc_ptr = pByteArea(smb_buf);
1356 /* BB check if Unicode and decode strings */
1357 if (bytes_remaining == 0) {
1358 /* no string area to decode, do nothing */
1359 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1360 /* unicode string area must be word-aligned */
1361 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1365 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1368 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1372 rc = sess_establish_session(sess_data);
1374 sess_data->result = rc;
1375 sess_data->func = NULL;
1376 sess_free_buffer(sess_data);
1377 kfree(ses->auth_key.response);
1378 ses->auth_key.response = NULL;
1381 #ifdef CONFIG_CIFS_UPCALL
1383 sess_auth_kerberos(struct sess_data *sess_data)
1386 struct smb_hdr *smb_buf;
1387 SESSION_SETUP_ANDX *pSMB;
1389 struct cifs_ses *ses = sess_data->ses;
1390 struct TCP_Server_Info *server = sess_data->server;
1392 __u16 bytes_remaining;
1393 struct key *spnego_key = NULL;
1394 struct cifs_spnego_msg *msg;
1397 /* extended security */
1399 rc = sess_alloc_buffer(sess_data, 12);
1403 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1404 bcc_ptr = sess_data->iov[2].iov_base;
1405 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1407 spnego_key = cifs_get_spnego_key(ses, server);
1408 if (IS_ERR(spnego_key)) {
1409 rc = PTR_ERR(spnego_key);
1414 msg = spnego_key->payload.data[0];
1416 * check version field to make sure that cifs.upcall is
1417 * sending us a response in an expected form
1419 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1420 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1421 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1423 goto out_put_spnego_key;
1426 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1428 if (!ses->auth_key.response) {
1429 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1432 goto out_put_spnego_key;
1434 ses->auth_key.len = msg->sesskey_len;
1436 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1437 capabilities |= CAP_EXTENDED_SECURITY;
1438 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1439 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1440 sess_data->iov[1].iov_len = msg->secblob_len;
1441 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1443 if (ses->capabilities & CAP_UNICODE) {
1444 /* unicode strings must be word aligned */
1445 if ((sess_data->iov[0].iov_len
1446 + sess_data->iov[1].iov_len) % 2) {
1450 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1451 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1453 /* BB: is this right? */
1454 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1457 sess_data->iov[2].iov_len = (long) bcc_ptr -
1458 (long) sess_data->iov[2].iov_base;
1460 rc = sess_sendreceive(sess_data);
1462 goto out_put_spnego_key;
1464 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1465 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1467 if (smb_buf->WordCount != 4) {
1469 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1470 goto out_put_spnego_key;
1473 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1474 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1476 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1477 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1479 bytes_remaining = get_bcc(smb_buf);
1480 bcc_ptr = pByteArea(smb_buf);
1482 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1483 if (blob_len > bytes_remaining) {
1484 cifs_dbg(VFS, "bad security blob length %d\n",
1487 goto out_put_spnego_key;
1489 bcc_ptr += blob_len;
1490 bytes_remaining -= blob_len;
1492 /* BB check if Unicode and decode strings */
1493 if (bytes_remaining == 0) {
1494 /* no string area to decode, do nothing */
1495 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1496 /* unicode string area must be word-aligned */
1497 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1501 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1504 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1508 rc = sess_establish_session(sess_data);
1510 key_invalidate(spnego_key);
1511 key_put(spnego_key);
1513 sess_data->result = rc;
1514 sess_data->func = NULL;
1515 sess_free_buffer(sess_data);
1516 kfree(ses->auth_key.response);
1517 ses->auth_key.response = NULL;
1520 #endif /* ! CONFIG_CIFS_UPCALL */
1523 * The required kvec buffers have to be allocated before calling this
1527 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1529 SESSION_SETUP_ANDX *pSMB;
1530 struct cifs_ses *ses = sess_data->ses;
1531 struct TCP_Server_Info *server = sess_data->server;
1535 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1537 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1538 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1539 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1543 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1544 capabilities |= CAP_EXTENDED_SECURITY;
1545 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1547 bcc_ptr = sess_data->iov[2].iov_base;
1548 /* unicode strings must be word aligned */
1549 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1553 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1555 sess_data->iov[2].iov_len = (long) bcc_ptr -
1556 (long) sess_data->iov[2].iov_base;
1562 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1565 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1568 struct smb_hdr *smb_buf;
1569 SESSION_SETUP_ANDX *pSMB;
1570 struct cifs_ses *ses = sess_data->ses;
1571 struct TCP_Server_Info *server = sess_data->server;
1572 __u16 bytes_remaining;
1574 unsigned char *ntlmsspblob = NULL;
1577 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1580 * if memory allocation is successful, caller of this function
1583 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1584 if (!ses->ntlmssp) {
1588 ses->ntlmssp->sesskey_per_smbsess = false;
1591 rc = sess_alloc_buffer(sess_data, 12);
1595 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1597 /* Build security blob before we assemble the request */
1598 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
1599 &blob_len, ses, server,
1602 goto out_free_ntlmsspblob;
1604 sess_data->iov[1].iov_len = blob_len;
1605 sess_data->iov[1].iov_base = ntlmsspblob;
1606 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1608 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1610 goto out_free_ntlmsspblob;
1612 rc = sess_sendreceive(sess_data);
1614 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1615 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1617 /* If true, rc here is expected and not an error */
1618 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1619 smb_buf->Status.CifsError ==
1620 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1624 goto out_free_ntlmsspblob;
1626 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1628 if (smb_buf->WordCount != 4) {
1630 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1631 goto out_free_ntlmsspblob;
1634 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1635 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1637 bytes_remaining = get_bcc(smb_buf);
1638 bcc_ptr = pByteArea(smb_buf);
1640 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1641 if (blob_len > bytes_remaining) {
1642 cifs_dbg(VFS, "bad security blob length %d\n",
1645 goto out_free_ntlmsspblob;
1648 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1650 out_free_ntlmsspblob:
1653 sess_free_buffer(sess_data);
1656 sess_data->func = sess_auth_rawntlmssp_authenticate;
1660 /* Else error. Cleanup */
1661 kfree(ses->auth_key.response);
1662 ses->auth_key.response = NULL;
1663 kfree(ses->ntlmssp);
1664 ses->ntlmssp = NULL;
1666 sess_data->func = NULL;
1667 sess_data->result = rc;
1671 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1674 struct smb_hdr *smb_buf;
1675 SESSION_SETUP_ANDX *pSMB;
1676 struct cifs_ses *ses = sess_data->ses;
1677 struct TCP_Server_Info *server = sess_data->server;
1678 __u16 bytes_remaining;
1680 unsigned char *ntlmsspblob = NULL;
1683 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1686 rc = sess_alloc_buffer(sess_data, 12);
1690 /* Build security blob before we assemble the request */
1691 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1692 smb_buf = (struct smb_hdr *)pSMB;
1693 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1694 &blob_len, ses, server,
1697 goto out_free_ntlmsspblob;
1698 sess_data->iov[1].iov_len = blob_len;
1699 sess_data->iov[1].iov_base = ntlmsspblob;
1700 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1702 * Make sure that we tell the server that we are using
1703 * the uid that it just gave us back on the response
1706 smb_buf->Uid = ses->Suid;
1708 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1710 goto out_free_ntlmsspblob;
1712 rc = sess_sendreceive(sess_data);
1714 goto out_free_ntlmsspblob;
1716 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1717 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1718 if (smb_buf->WordCount != 4) {
1720 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1721 goto out_free_ntlmsspblob;
1724 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1725 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1727 if (ses->Suid != smb_buf->Uid) {
1728 ses->Suid = smb_buf->Uid;
1729 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1732 bytes_remaining = get_bcc(smb_buf);
1733 bcc_ptr = pByteArea(smb_buf);
1734 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1735 if (blob_len > bytes_remaining) {
1736 cifs_dbg(VFS, "bad security blob length %d\n",
1739 goto out_free_ntlmsspblob;
1741 bcc_ptr += blob_len;
1742 bytes_remaining -= blob_len;
1745 /* BB check if Unicode and decode strings */
1746 if (bytes_remaining == 0) {
1747 /* no string area to decode, do nothing */
1748 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1749 /* unicode string area must be word-aligned */
1750 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1754 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1757 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1761 out_free_ntlmsspblob:
1764 sess_free_buffer(sess_data);
1767 rc = sess_establish_session(sess_data);
1770 kfree(ses->auth_key.response);
1771 ses->auth_key.response = NULL;
1772 kfree(ses->ntlmssp);
1773 ses->ntlmssp = NULL;
1775 sess_data->func = NULL;
1776 sess_data->result = rc;
1779 static int select_sec(struct sess_data *sess_data)
1782 struct cifs_ses *ses = sess_data->ses;
1783 struct TCP_Server_Info *server = sess_data->server;
1785 type = cifs_select_sectype(server, ses->sectype);
1786 cifs_dbg(FYI, "sess setup type %d\n", type);
1787 if (type == Unspecified) {
1788 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1794 sess_data->func = sess_auth_ntlmv2;
1797 #ifdef CONFIG_CIFS_UPCALL
1798 sess_data->func = sess_auth_kerberos;
1801 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1803 #endif /* CONFIG_CIFS_UPCALL */
1805 sess_data->func = sess_auth_rawntlmssp_negotiate;
1808 cifs_dbg(VFS, "secType %d not supported!\n", type);
1815 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1816 struct TCP_Server_Info *server,
1817 const struct nls_table *nls_cp)
1820 struct sess_data *sess_data;
1823 WARN(1, "%s: ses == NULL!", __func__);
1827 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1831 sess_data->xid = xid;
1832 sess_data->ses = ses;
1833 sess_data->server = server;
1834 sess_data->buf0_type = CIFS_NO_BUFFER;
1835 sess_data->nls_cp = (struct nls_table *) nls_cp;
1837 rc = select_sec(sess_data);
1841 while (sess_data->func)
1842 sess_data->func(sess_data);
1844 /* Store result before we free sess_data */
1845 rc = sess_data->result;
1851 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */