1 // SPDX-License-Identifier: LGPL-2.1
4 * SMB/CIFS session setup handling routines
6 * Copyright (c) International Business Machines Corp., 2006, 2009
7 * Author(s): Steve French (sfrench@us.ibm.com)
13 #include "cifsproto.h"
14 #include "cifs_unicode.h"
15 #include "cifs_debug.h"
18 #include <linux/utsname.h>
19 #include <linux/slab.h>
20 #include <linux/version.h>
22 #include "cifs_spnego.h"
23 #include "smb2proto.h"
24 #include "fs_context.h"
27 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
28 struct cifs_server_iface *iface);
31 is_server_using_iface(struct TCP_Server_Info *server,
32 struct cifs_server_iface *iface)
34 struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr;
35 struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr;
36 struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr;
37 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr;
39 if (server->dstaddr.ss_family != iface->sockaddr.ss_family)
41 if (server->dstaddr.ss_family == AF_INET) {
42 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
44 } else if (server->dstaddr.ss_family == AF_INET6) {
45 if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
46 sizeof(i6->sin6_addr)) != 0)
49 /* unknown family.. */
55 bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
59 spin_lock(&ses->chan_lock);
60 for (i = 0; i < ses->chan_count; i++) {
61 if (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 struct TCP_Server_Info *server = ses->server;
163 int old_chan_count, new_chan_count;
167 size_t iface_weight = 0, iface_min_speed = 0;
168 struct cifs_server_iface *iface = NULL, *niface = NULL;
169 struct cifs_server_iface *last_iface = NULL;
171 spin_lock(&ses->chan_lock);
173 new_chan_count = old_chan_count = ses->chan_count;
174 left = ses->chan_max - ses->chan_count;
177 spin_unlock(&ses->chan_lock);
179 "ses already at max_channels (%zu), nothing to open\n",
184 if (server->dialect < SMB30_PROT_ID) {
185 spin_unlock(&ses->chan_lock);
186 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
190 if (!(server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
191 spin_unlock(&ses->chan_lock);
192 cifs_server_dbg(VFS, "no multichannel support\n");
195 spin_unlock(&ses->chan_lock);
200 if (tries > 3*ses->chan_max) {
201 cifs_dbg(VFS, "too many channel open attempts (%d channels left to open)\n",
206 spin_lock(&ses->iface_lock);
207 if (!ses->iface_count) {
208 spin_unlock(&ses->iface_lock);
209 cifs_dbg(VFS, "server %s does not advertise interfaces\n",
210 ses->server->hostname);
215 iface = list_first_entry(&ses->iface_list, struct cifs_server_iface,
217 last_iface = list_last_entry(&ses->iface_list, struct cifs_server_iface,
219 iface_min_speed = last_iface->speed;
221 list_for_each_entry_safe_from(iface, niface, &ses->iface_list,
223 /* do not mix rdma and non-rdma interfaces */
224 if (iface->rdma_capable != ses->server->rdma)
227 /* skip ifaces that are unusable */
228 if (!iface->is_active ||
229 (is_ses_using_iface(ses, iface) &&
230 !iface->rss_capable))
233 /* check if we already allocated enough channels */
234 iface_weight = iface->speed / iface_min_speed;
236 if (iface->weight_fulfilled >= iface_weight)
239 /* take ref before unlock */
240 kref_get(&iface->refcount);
242 spin_unlock(&ses->iface_lock);
243 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
244 spin_lock(&ses->iface_lock);
247 cifs_dbg(VFS, "failed to open extra channel on iface:%pIS rc=%d\n",
250 kref_put(&iface->refcount, release_iface);
254 iface->num_channels++;
255 iface->weight_fulfilled++;
256 cifs_dbg(VFS, "successfully opened new channel on iface:%pIS\n",
261 /* reached end of list. reset weight_fulfilled and start over */
262 if (list_entry_is_head(iface, &ses->iface_list, iface_head)) {
263 list_for_each_entry(iface, &ses->iface_list, iface_head)
264 iface->weight_fulfilled = 0;
265 spin_unlock(&ses->iface_lock);
269 spin_unlock(&ses->iface_lock);
275 return new_chan_count - old_chan_count;
279 * update the iface for the channel if necessary.
280 * will return 0 when iface is updated, 1 if removed, 2 otherwise
281 * Must be called with chan_lock held.
284 cifs_chan_update_iface(struct cifs_ses *ses, struct TCP_Server_Info *server)
286 unsigned int chan_index;
287 size_t iface_weight = 0, iface_min_speed = 0;
288 struct cifs_server_iface *iface = NULL;
289 struct cifs_server_iface *old_iface = NULL;
290 struct cifs_server_iface *last_iface = NULL;
293 spin_lock(&ses->chan_lock);
294 chan_index = cifs_ses_get_chan_index(ses, server);
296 spin_unlock(&ses->chan_lock);
300 if (ses->chans[chan_index].iface) {
301 old_iface = ses->chans[chan_index].iface;
302 if (old_iface->is_active) {
303 spin_unlock(&ses->chan_lock);
307 spin_unlock(&ses->chan_lock);
309 spin_lock(&ses->iface_lock);
310 if (!ses->iface_count) {
311 spin_unlock(&ses->iface_lock);
312 cifs_dbg(VFS, "server %s does not advertise interfaces\n", ses->server->hostname);
316 last_iface = list_last_entry(&ses->iface_list, struct cifs_server_iface,
318 iface_min_speed = last_iface->speed;
320 /* then look for a new one */
321 list_for_each_entry(iface, &ses->iface_list, iface_head) {
322 /* do not mix rdma and non-rdma interfaces */
323 if (iface->rdma_capable != server->rdma)
326 if (!iface->is_active ||
327 (is_ses_using_iface(ses, iface) &&
328 !iface->rss_capable)) {
332 /* check if we already allocated enough channels */
333 iface_weight = iface->speed / iface_min_speed;
335 if (iface->weight_fulfilled >= iface_weight)
338 kref_get(&iface->refcount);
342 if (list_entry_is_head(iface, &ses->iface_list, iface_head)) {
345 cifs_dbg(FYI, "unable to find a suitable iface\n");
348 /* now drop the ref to the current iface */
349 if (old_iface && iface) {
350 cifs_dbg(FYI, "replacing iface: %pIS with %pIS\n",
351 &old_iface->sockaddr,
354 old_iface->num_channels--;
355 if (old_iface->weight_fulfilled)
356 old_iface->weight_fulfilled--;
357 iface->num_channels++;
358 iface->weight_fulfilled++;
360 kref_put(&old_iface->refcount, release_iface);
361 } else if (old_iface) {
362 cifs_dbg(FYI, "releasing ref to iface: %pIS\n",
363 &old_iface->sockaddr);
365 old_iface->num_channels--;
366 if (old_iface->weight_fulfilled)
367 old_iface->weight_fulfilled--;
369 kref_put(&old_iface->refcount, release_iface);
372 cifs_dbg(FYI, "adding new iface: %pIS\n", &iface->sockaddr);
374 spin_unlock(&ses->iface_lock);
376 spin_lock(&ses->chan_lock);
377 chan_index = cifs_ses_get_chan_index(ses, server);
378 ses->chans[chan_index].iface = iface;
380 /* No iface is found. if secondary chan, drop connection */
381 if (!iface && CIFS_SERVER_IS_CHAN(server))
382 ses->chans[chan_index].server = NULL;
384 spin_unlock(&ses->chan_lock);
386 if (!iface && CIFS_SERVER_IS_CHAN(server))
387 cifs_put_tcp_session(server, false);
393 * If server is a channel of ses, return the corresponding enclosing
394 * cifs_chan otherwise return NULL.
397 cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
401 spin_lock(&ses->chan_lock);
402 for (i = 0; i < ses->chan_count; i++) {
403 if (ses->chans[i].server == server) {
404 spin_unlock(&ses->chan_lock);
405 return &ses->chans[i];
408 spin_unlock(&ses->chan_lock);
413 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
414 struct cifs_server_iface *iface)
416 struct TCP_Server_Info *chan_server;
417 struct cifs_chan *chan;
418 struct smb3_fs_context ctx = {NULL};
419 static const char unc_fmt[] = "\\%s\\foo";
420 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
421 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
422 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
424 unsigned int xid = get_xid();
426 if (iface->sockaddr.ss_family == AF_INET)
427 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
428 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
431 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
432 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
436 * Setup a ctx with mostly the same info as the existing
437 * session and overwrite it with the requested iface data.
439 * We need to setup at least the fields used for negprot and
442 * We only need the ctx here, so we can reuse memory from
443 * the session and server without caring about memory
447 /* Always make new connection for now (TODO?) */
448 ctx.nosharesock = true;
451 ctx.domainauto = ses->domainAuto;
452 ctx.domainname = ses->domainName;
454 /* no hostname for extra channels */
455 ctx.server_hostname = "";
457 ctx.username = ses->user_name;
458 ctx.password = ses->password;
459 ctx.sectype = ses->sectype;
460 ctx.sign = ses->sign;
463 /* XXX: Use ses->server->hostname? */
464 sprintf(unc, unc_fmt, ses->ip_addr);
468 /* Reuse same version as master connection */
469 ctx.vals = ses->server->vals;
470 ctx.ops = ses->server->ops;
472 ctx.noblocksnd = ses->server->noblocksnd;
473 ctx.noautotune = ses->server->noautotune;
474 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
475 ctx.echo_interval = ses->server->echo_interval / HZ;
476 ctx.max_credits = ses->server->max_credits;
479 * This will be used for encoding/decoding user/domain/pw
480 * during sess setup auth.
482 ctx.local_nls = cifs_sb->local_nls;
484 /* Use RDMA if possible */
485 ctx.rdma = iface->rdma_capable;
486 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
488 /* reuse master con client guid */
489 memcpy(&ctx.client_guid, ses->server->client_guid,
490 SMB2_CLIENT_GUID_SIZE);
491 ctx.use_client_guid = true;
493 chan_server = cifs_get_tcp_session(&ctx, ses->server);
495 spin_lock(&ses->chan_lock);
496 chan = &ses->chans[ses->chan_count];
497 chan->server = chan_server;
498 if (IS_ERR(chan->server)) {
499 rc = PTR_ERR(chan->server);
501 spin_unlock(&ses->chan_lock);
506 atomic_set(&ses->chan_seq, 0);
508 /* Mark this channel as needing connect/setup */
509 cifs_chan_set_need_reconnect(ses, chan->server);
511 spin_unlock(&ses->chan_lock);
513 mutex_lock(&ses->session_mutex);
515 * We need to allocate the server crypto now as we will need
516 * to sign packets before we generate the channel signing key
517 * (we sign with the session key)
519 rc = smb311_crypto_shash_allocate(chan->server);
521 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
522 mutex_unlock(&ses->session_mutex);
526 rc = cifs_negotiate_protocol(xid, ses, chan->server);
528 rc = cifs_setup_session(xid, ses, chan->server, cifs_sb->local_nls);
530 mutex_unlock(&ses->session_mutex);
533 if (rc && chan->server) {
535 * we should avoid race with these delayed works before we
536 * remove this channel
538 cancel_delayed_work_sync(&chan->server->echo);
539 cancel_delayed_work_sync(&chan->server->resolve);
540 cancel_delayed_work_sync(&chan->server->reconnect);
542 spin_lock(&ses->chan_lock);
543 /* we rely on all bits beyond chan_count to be clear */
544 cifs_chan_clear_need_reconnect(ses, chan->server);
547 * chan_count should never reach 0 as at least the primary
548 * channel is always allocated
550 WARN_ON(ses->chan_count < 1);
551 spin_unlock(&ses->chan_lock);
553 cifs_put_tcp_session(chan->server, 0);
560 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
561 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses,
562 struct TCP_Server_Info *server,
563 SESSION_SETUP_ANDX *pSMB)
565 __u32 capabilities = 0;
567 /* init fields common to all four types of SessSetup */
568 /* Note that offsets for first seven fields in req struct are same */
569 /* in CIFS Specs so does not matter which of 3 forms of struct */
570 /* that we use in next few lines */
571 /* Note that header is initialized to zero in header_assemble */
572 pSMB->req.AndXCommand = 0xFF;
573 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
574 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
576 pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq);
577 pSMB->req.VcNumber = cpu_to_le16(1);
579 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
581 /* BB verify whether signing required on neg or just on auth frame
584 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
585 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
588 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
590 if (ses->capabilities & CAP_UNICODE) {
591 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
592 capabilities |= CAP_UNICODE;
594 if (ses->capabilities & CAP_STATUS32) {
595 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
596 capabilities |= CAP_STATUS32;
598 if (ses->capabilities & CAP_DFS) {
599 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
600 capabilities |= CAP_DFS;
602 if (ses->capabilities & CAP_UNIX)
603 capabilities |= CAP_UNIX;
609 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
611 char *bcc_ptr = *pbcc_area;
614 /* Copy OS version */
615 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
617 bcc_ptr += 2 * bytes_ret;
618 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
620 bcc_ptr += 2 * bytes_ret;
621 bcc_ptr += 2; /* trailing null */
623 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
625 bcc_ptr += 2 * bytes_ret;
626 bcc_ptr += 2; /* trailing null */
628 *pbcc_area = bcc_ptr;
631 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
632 const struct nls_table *nls_cp)
634 char *bcc_ptr = *pbcc_area;
638 if (ses->domainName == NULL) {
639 /* Sending null domain better than using a bogus domain name (as
640 we did briefly in 2.6.18) since server will use its default */
645 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
646 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
647 bcc_ptr += 2 * bytes_ret;
648 bcc_ptr += 2; /* account for null terminator */
650 *pbcc_area = bcc_ptr;
653 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
654 const struct nls_table *nls_cp)
656 char *bcc_ptr = *pbcc_area;
659 /* BB FIXME add check that strings total less
660 than 335 or will need to send them as arrays */
663 if (ses->user_name == NULL) {
664 /* null user mount */
668 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
669 CIFS_MAX_USERNAME_LEN, nls_cp);
671 bcc_ptr += 2 * bytes_ret;
672 bcc_ptr += 2; /* account for null termination */
674 unicode_domain_string(&bcc_ptr, ses, nls_cp);
675 unicode_oslm_strings(&bcc_ptr, nls_cp);
677 *pbcc_area = bcc_ptr;
680 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
681 const struct nls_table *nls_cp)
683 char *bcc_ptr = *pbcc_area;
687 /* BB what about null user mounts - check that we do this BB */
689 if (ses->user_name != NULL) {
690 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
691 if (WARN_ON_ONCE(len < 0))
692 len = CIFS_MAX_USERNAME_LEN - 1;
695 /* else null user mount */
697 bcc_ptr++; /* account for null termination */
700 if (ses->domainName != NULL) {
701 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
702 if (WARN_ON_ONCE(len < 0))
703 len = CIFS_MAX_DOMAINNAME_LEN - 1;
705 } /* else we will send a null domain name
706 so the server will default to its own domain */
710 /* BB check for overflow here */
712 strcpy(bcc_ptr, "Linux version ");
713 bcc_ptr += strlen("Linux version ");
714 strcpy(bcc_ptr, init_utsname()->release);
715 bcc_ptr += strlen(init_utsname()->release) + 1;
717 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
718 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
720 *pbcc_area = bcc_ptr;
724 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
725 const struct nls_table *nls_cp)
728 char *data = *pbcc_area;
730 cifs_dbg(FYI, "bleft %d\n", bleft);
732 kfree(ses->serverOS);
733 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
734 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
735 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
741 kfree(ses->serverNOS);
742 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
743 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
744 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
750 kfree(ses->serverDomain);
751 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
752 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
757 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
758 struct cifs_ses *ses,
759 const struct nls_table *nls_cp)
762 char *bcc_ptr = *pbcc_area;
764 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
766 len = strnlen(bcc_ptr, bleft);
770 kfree(ses->serverOS);
772 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
774 memcpy(ses->serverOS, bcc_ptr, len);
775 ses->serverOS[len] = 0;
776 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
777 cifs_dbg(FYI, "OS/2 server\n");
783 len = strnlen(bcc_ptr, bleft);
787 kfree(ses->serverNOS);
789 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
790 if (ses->serverNOS) {
791 memcpy(ses->serverNOS, bcc_ptr, len);
792 ses->serverNOS[len] = 0;
798 len = strnlen(bcc_ptr, bleft);
802 /* No domain field in LANMAN case. Domain is
803 returned by old servers in the SMB negprot response */
804 /* BB For newer servers which do not support Unicode,
805 but thus do return domain here we could add parsing
806 for it later, but it is not very important */
807 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
809 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
811 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
812 struct cifs_ses *ses)
814 unsigned int tioffset; /* challenge message target info area */
815 unsigned int tilen; /* challenge message target info area length */
816 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
819 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
820 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
824 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
825 cifs_dbg(VFS, "blob signature incorrect %s\n",
829 if (pblob->MessageType != NtLmChallenge) {
830 cifs_dbg(VFS, "Incorrect message type %d\n",
835 server_flags = le32_to_cpu(pblob->NegotiateFlags);
836 cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__,
837 ses->ntlmssp->client_flags, server_flags);
839 if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) &&
840 (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) {
841 cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n",
845 if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) {
846 cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__);
849 if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) {
850 cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n",
854 if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
855 !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH))
856 pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n",
859 ses->ntlmssp->server_flags = server_flags;
861 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
862 /* In particular we can examine sign flags */
863 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
864 we must set the MIC field of the AUTHENTICATE_MESSAGE */
866 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
867 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
868 if (tioffset > blob_len || tioffset + tilen > blob_len) {
869 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
874 kfree_sensitive(ses->auth_key.response);
875 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
877 if (!ses->auth_key.response) {
878 cifs_dbg(VFS, "Challenge target info alloc failure\n");
881 ses->auth_key.len = tilen;
887 static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
889 int sz = base_size + ses->auth_key.len
890 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
893 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
895 sz += sizeof(__le16);
898 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
900 sz += sizeof(__le16);
902 if (ses->workstation_name[0])
903 sz += sizeof(__le16) * strnlen(ses->workstation_name,
904 ntlmssp_workstation_name_size(ses));
906 sz += sizeof(__le16);
911 static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
914 unsigned char *pstart,
915 unsigned char **pcur,
916 const struct nls_table *nls_cp)
918 unsigned char *tmp = pstart;
928 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
930 pbuf->MaximumLength = 0;
931 *pcur += sizeof(__le16);
933 len = cifs_strtoUTF16((__le16 *)*pcur,
937 len *= sizeof(__le16);
938 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
939 pbuf->Length = cpu_to_le16(len);
940 pbuf->MaximumLength = cpu_to_le16(len);
945 /* BB Move to ntlmssp.c eventually */
947 int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
949 struct cifs_ses *ses,
950 struct TCP_Server_Info *server,
951 const struct nls_table *nls_cp)
954 NEGOTIATE_MESSAGE *sec_blob;
959 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
960 *pbuffer = kmalloc(len, GFP_KERNEL);
963 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
965 goto setup_ntlm_neg_ret;
967 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
969 memset(*pbuffer, 0, sizeof(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;
979 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
980 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
982 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
983 ses->ntlmssp->client_flags = flags;
984 sec_blob->NegotiateFlags = cpu_to_le32(flags);
986 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
987 cifs_security_buffer_from_str(&sec_blob->DomainName,
989 CIFS_MAX_DOMAINNAME_LEN,
993 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
995 CIFS_MAX_WORKSTATION_LEN,
999 *buflen = tmp - *pbuffer;
1005 * Build ntlmssp blob with additional fields, such as version,
1006 * supported by modern servers. For safety limit to SMB3 or later
1007 * See notes in MS-NLMP Section 2.2.2.1 e.g.
1009 int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer,
1011 struct cifs_ses *ses,
1012 struct TCP_Server_Info *server,
1013 const struct nls_table *nls_cp)
1016 struct negotiate_message *sec_blob;
1021 len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message));
1022 *pbuffer = kmalloc(len, GFP_KERNEL);
1025 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
1027 goto setup_ntlm_smb3_neg_ret;
1029 sec_blob = (struct negotiate_message *)*pbuffer;
1031 memset(*pbuffer, 0, sizeof(struct negotiate_message));
1032 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1033 sec_blob->MessageType = NtLmNegotiate;
1035 /* BB is NTLMV2 session security format easier to use here? */
1036 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
1037 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
1038 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
1039 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
1040 NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION;
1041 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
1042 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
1044 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
1045 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
1046 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
1047 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
1049 tmp = *pbuffer + sizeof(struct negotiate_message);
1050 ses->ntlmssp->client_flags = flags;
1051 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1053 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
1054 cifs_security_buffer_from_str(&sec_blob->DomainName,
1056 CIFS_MAX_DOMAINNAME_LEN,
1060 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1062 CIFS_MAX_WORKSTATION_LEN,
1066 *buflen = tmp - *pbuffer;
1067 setup_ntlm_smb3_neg_ret:
1072 /* See MS-NLMP 2.2.1.3 */
1073 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
1075 struct cifs_ses *ses,
1076 struct TCP_Server_Info *server,
1077 const struct nls_table *nls_cp)
1080 AUTHENTICATE_MESSAGE *sec_blob;
1085 rc = setup_ntlmv2_rsp(ses, nls_cp);
1087 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
1089 goto setup_ntlmv2_ret;
1092 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
1093 *pbuffer = kmalloc(len, GFP_KERNEL);
1096 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
1098 goto setup_ntlmv2_ret;
1100 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
1102 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1103 sec_blob->MessageType = NtLmAuthenticate;
1105 flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET |
1106 NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
1107 /* we only send version information in ntlmssp negotiate, so do not set this flag */
1108 flags = flags & ~NTLMSSP_NEGOTIATE_VERSION;
1109 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
1110 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1112 sec_blob->LmChallengeResponse.BufferOffset =
1113 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
1114 sec_blob->LmChallengeResponse.Length = 0;
1115 sec_blob->LmChallengeResponse.MaximumLength = 0;
1117 sec_blob->NtChallengeResponse.BufferOffset =
1118 cpu_to_le32(tmp - *pbuffer);
1119 if (ses->user_name != NULL) {
1120 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1121 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1122 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1124 sec_blob->NtChallengeResponse.Length =
1125 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1126 sec_blob->NtChallengeResponse.MaximumLength =
1127 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1130 * don't send an NT Response for anonymous access
1132 sec_blob->NtChallengeResponse.Length = 0;
1133 sec_blob->NtChallengeResponse.MaximumLength = 0;
1136 cifs_security_buffer_from_str(&sec_blob->DomainName,
1138 CIFS_MAX_DOMAINNAME_LEN,
1142 cifs_security_buffer_from_str(&sec_blob->UserName,
1144 CIFS_MAX_USERNAME_LEN,
1148 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1149 ses->workstation_name,
1150 ntlmssp_workstation_name_size(ses),
1154 if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
1155 (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) &&
1156 !calc_seckey(ses)) {
1157 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
1158 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1159 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
1160 sec_blob->SessionKey.MaximumLength =
1161 cpu_to_le16(CIFS_CPHTXT_SIZE);
1162 tmp += CIFS_CPHTXT_SIZE;
1164 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1165 sec_blob->SessionKey.Length = 0;
1166 sec_blob->SessionKey.MaximumLength = 0;
1169 *buflen = tmp - *pbuffer;
1175 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1177 switch (server->negflavor) {
1178 case CIFS_NEGFLAVOR_EXTENDED:
1179 switch (requested) {
1184 if (server->sec_ntlmssp &&
1185 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1187 if ((server->sec_kerberos || server->sec_mskerberos) &&
1188 (global_secflags & CIFSSEC_MAY_KRB5))
1194 case CIFS_NEGFLAVOR_UNENCAP:
1195 switch (requested) {
1199 if (global_secflags & CIFSSEC_MAY_NTLMV2)
1213 struct cifs_ses *ses;
1214 struct TCP_Server_Info *server;
1215 struct nls_table *nls_cp;
1216 void (*func)(struct sess_data *);
1219 /* we will send the SMB in three pieces:
1220 * a fixed length beginning part, an optional
1221 * SPNEGO blob (which can be zero length), and a
1222 * last part which will include the strings
1223 * and rest of bcc area. This allows us to avoid
1224 * a large buffer 17K allocation
1230 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1232 sess_alloc_buffer(struct sess_data *sess_data, int wct)
1235 struct cifs_ses *ses = sess_data->ses;
1236 struct smb_hdr *smb_buf;
1238 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
1244 sess_data->iov[0].iov_base = (char *)smb_buf;
1245 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
1247 * This variable will be used to clear the buffer
1248 * allocated above in case of any error in the calling function.
1250 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1252 /* 2000 big enough to fit max user, domain, NOS name etc. */
1253 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
1254 if (!sess_data->iov[2].iov_base) {
1256 goto out_free_smb_buf;
1262 cifs_small_buf_release(smb_buf);
1263 sess_data->iov[0].iov_base = NULL;
1264 sess_data->iov[0].iov_len = 0;
1265 sess_data->buf0_type = CIFS_NO_BUFFER;
1270 sess_free_buffer(struct sess_data *sess_data)
1272 struct kvec *iov = sess_data->iov;
1275 * Zero the session data before freeing, as it might contain sensitive info (keys, etc).
1276 * Note that iov[1] is already freed by caller.
1278 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1279 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
1281 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
1282 sess_data->buf0_type = CIFS_NO_BUFFER;
1283 kfree_sensitive(iov[2].iov_base);
1287 sess_establish_session(struct sess_data *sess_data)
1289 struct cifs_ses *ses = sess_data->ses;
1290 struct TCP_Server_Info *server = sess_data->server;
1292 cifs_server_lock(server);
1293 if (!server->session_estab) {
1295 server->session_key.response =
1296 kmemdup(ses->auth_key.response,
1297 ses->auth_key.len, GFP_KERNEL);
1298 if (!server->session_key.response) {
1299 cifs_server_unlock(server);
1302 server->session_key.len =
1305 server->sequence_number = 0x2;
1306 server->session_estab = true;
1308 cifs_server_unlock(server);
1310 cifs_dbg(FYI, "CIFS session established successfully\n");
1315 sess_sendreceive(struct sess_data *sess_data)
1318 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
1320 struct kvec rsp_iov = { NULL, 0 };
1322 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
1323 be32_add_cpu(&smb_buf->smb_buf_length, count);
1324 put_bcc(count, smb_buf);
1326 rc = SendReceive2(sess_data->xid, sess_data->ses,
1327 sess_data->iov, 3 /* num_iovecs */,
1328 &sess_data->buf0_type,
1329 CIFS_LOG_ERROR, &rsp_iov);
1330 cifs_small_buf_release(sess_data->iov[0].iov_base);
1331 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1337 sess_auth_ntlmv2(struct sess_data *sess_data)
1340 struct smb_hdr *smb_buf;
1341 SESSION_SETUP_ANDX *pSMB;
1343 struct cifs_ses *ses = sess_data->ses;
1344 struct TCP_Server_Info *server = sess_data->server;
1346 __u16 bytes_remaining;
1348 /* old style NTLM sessionsetup */
1350 rc = sess_alloc_buffer(sess_data, 13);
1354 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1355 bcc_ptr = sess_data->iov[2].iov_base;
1356 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1358 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1360 /* LM2 password would be here if we supported it */
1361 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1363 if (ses->user_name != NULL) {
1364 /* calculate nlmv2 response and session key */
1365 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1367 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1371 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1372 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1373 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1375 /* set case sensitive password length after tilen may get
1376 * assigned, tilen is 0 otherwise.
1378 pSMB->req_no_secext.CaseSensitivePasswordLength =
1379 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1381 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1384 if (ses->capabilities & CAP_UNICODE) {
1385 if (!IS_ALIGNED(sess_data->iov[0].iov_len, 2)) {
1389 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1391 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1395 sess_data->iov[2].iov_len = (long) bcc_ptr -
1396 (long) sess_data->iov[2].iov_base;
1398 rc = sess_sendreceive(sess_data);
1402 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1403 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1405 if (smb_buf->WordCount != 3) {
1407 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1411 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1412 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1414 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1415 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1417 bytes_remaining = get_bcc(smb_buf);
1418 bcc_ptr = pByteArea(smb_buf);
1420 /* BB check if Unicode and decode strings */
1421 if (bytes_remaining == 0) {
1422 /* no string area to decode, do nothing */
1423 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1424 /* unicode string area must be word-aligned */
1425 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1429 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1432 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1436 rc = sess_establish_session(sess_data);
1438 sess_data->result = rc;
1439 sess_data->func = NULL;
1440 sess_free_buffer(sess_data);
1441 kfree_sensitive(ses->auth_key.response);
1442 ses->auth_key.response = NULL;
1445 #ifdef CONFIG_CIFS_UPCALL
1447 sess_auth_kerberos(struct sess_data *sess_data)
1450 struct smb_hdr *smb_buf;
1451 SESSION_SETUP_ANDX *pSMB;
1453 struct cifs_ses *ses = sess_data->ses;
1454 struct TCP_Server_Info *server = sess_data->server;
1456 __u16 bytes_remaining;
1457 struct key *spnego_key = NULL;
1458 struct cifs_spnego_msg *msg;
1461 /* extended security */
1463 rc = sess_alloc_buffer(sess_data, 12);
1467 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1468 bcc_ptr = sess_data->iov[2].iov_base;
1469 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1471 spnego_key = cifs_get_spnego_key(ses, server);
1472 if (IS_ERR(spnego_key)) {
1473 rc = PTR_ERR(spnego_key);
1478 msg = spnego_key->payload.data[0];
1480 * check version field to make sure that cifs.upcall is
1481 * sending us a response in an expected form
1483 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1484 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1485 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1487 goto out_put_spnego_key;
1490 kfree_sensitive(ses->auth_key.response);
1491 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1493 if (!ses->auth_key.response) {
1494 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1497 goto out_put_spnego_key;
1499 ses->auth_key.len = msg->sesskey_len;
1501 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1502 capabilities |= CAP_EXTENDED_SECURITY;
1503 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1504 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1505 sess_data->iov[1].iov_len = msg->secblob_len;
1506 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1508 if (ses->capabilities & CAP_UNICODE) {
1509 /* unicode strings must be word aligned */
1510 if (!IS_ALIGNED(sess_data->iov[0].iov_len + sess_data->iov[1].iov_len, 2)) {
1514 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1515 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1517 /* BB: is this right? */
1518 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1521 sess_data->iov[2].iov_len = (long) bcc_ptr -
1522 (long) sess_data->iov[2].iov_base;
1524 rc = sess_sendreceive(sess_data);
1526 goto out_put_spnego_key;
1528 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1529 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1531 if (smb_buf->WordCount != 4) {
1533 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1534 goto out_put_spnego_key;
1537 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1538 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1540 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1541 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1543 bytes_remaining = get_bcc(smb_buf);
1544 bcc_ptr = pByteArea(smb_buf);
1546 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1547 if (blob_len > bytes_remaining) {
1548 cifs_dbg(VFS, "bad security blob length %d\n",
1551 goto out_put_spnego_key;
1553 bcc_ptr += blob_len;
1554 bytes_remaining -= blob_len;
1556 /* BB check if Unicode and decode strings */
1557 if (bytes_remaining == 0) {
1558 /* no string area to decode, do nothing */
1559 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1560 /* unicode string area must be word-aligned */
1561 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1565 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1568 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1572 rc = sess_establish_session(sess_data);
1574 key_invalidate(spnego_key);
1575 key_put(spnego_key);
1577 sess_data->result = rc;
1578 sess_data->func = NULL;
1579 sess_free_buffer(sess_data);
1580 kfree_sensitive(ses->auth_key.response);
1581 ses->auth_key.response = NULL;
1584 #endif /* ! CONFIG_CIFS_UPCALL */
1587 * The required kvec buffers have to be allocated before calling this
1591 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1593 SESSION_SETUP_ANDX *pSMB;
1594 struct cifs_ses *ses = sess_data->ses;
1595 struct TCP_Server_Info *server = sess_data->server;
1599 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1601 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1602 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1603 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1607 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1608 capabilities |= CAP_EXTENDED_SECURITY;
1609 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1611 bcc_ptr = sess_data->iov[2].iov_base;
1612 /* unicode strings must be word aligned */
1613 if (!IS_ALIGNED(sess_data->iov[0].iov_len + sess_data->iov[1].iov_len, 2)) {
1617 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1619 sess_data->iov[2].iov_len = (long) bcc_ptr -
1620 (long) sess_data->iov[2].iov_base;
1626 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1629 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1632 struct smb_hdr *smb_buf;
1633 SESSION_SETUP_ANDX *pSMB;
1634 struct cifs_ses *ses = sess_data->ses;
1635 struct TCP_Server_Info *server = sess_data->server;
1636 __u16 bytes_remaining;
1638 unsigned char *ntlmsspblob = NULL;
1641 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1644 * if memory allocation is successful, caller of this function
1647 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1648 if (!ses->ntlmssp) {
1652 ses->ntlmssp->sesskey_per_smbsess = false;
1655 rc = sess_alloc_buffer(sess_data, 12);
1659 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1661 /* Build security blob before we assemble the request */
1662 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
1663 &blob_len, ses, server,
1666 goto out_free_ntlmsspblob;
1668 sess_data->iov[1].iov_len = blob_len;
1669 sess_data->iov[1].iov_base = ntlmsspblob;
1670 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1672 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1674 goto out_free_ntlmsspblob;
1676 rc = sess_sendreceive(sess_data);
1678 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1679 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1681 /* If true, rc here is expected and not an error */
1682 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1683 smb_buf->Status.CifsError ==
1684 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1688 goto out_free_ntlmsspblob;
1690 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1692 if (smb_buf->WordCount != 4) {
1694 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1695 goto out_free_ntlmsspblob;
1698 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1699 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1701 bytes_remaining = get_bcc(smb_buf);
1702 bcc_ptr = pByteArea(smb_buf);
1704 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1705 if (blob_len > bytes_remaining) {
1706 cifs_dbg(VFS, "bad security blob length %d\n",
1709 goto out_free_ntlmsspblob;
1712 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1714 out_free_ntlmsspblob:
1715 kfree_sensitive(ntlmsspblob);
1717 sess_free_buffer(sess_data);
1720 sess_data->func = sess_auth_rawntlmssp_authenticate;
1724 /* Else error. Cleanup */
1725 kfree_sensitive(ses->auth_key.response);
1726 ses->auth_key.response = NULL;
1727 kfree_sensitive(ses->ntlmssp);
1728 ses->ntlmssp = NULL;
1730 sess_data->func = NULL;
1731 sess_data->result = rc;
1735 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1738 struct smb_hdr *smb_buf;
1739 SESSION_SETUP_ANDX *pSMB;
1740 struct cifs_ses *ses = sess_data->ses;
1741 struct TCP_Server_Info *server = sess_data->server;
1742 __u16 bytes_remaining;
1744 unsigned char *ntlmsspblob = NULL;
1747 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1750 rc = sess_alloc_buffer(sess_data, 12);
1754 /* Build security blob before we assemble the request */
1755 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1756 smb_buf = (struct smb_hdr *)pSMB;
1757 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1758 &blob_len, ses, server,
1761 goto out_free_ntlmsspblob;
1762 sess_data->iov[1].iov_len = blob_len;
1763 sess_data->iov[1].iov_base = ntlmsspblob;
1764 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1766 * Make sure that we tell the server that we are using
1767 * the uid that it just gave us back on the response
1770 smb_buf->Uid = ses->Suid;
1772 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1774 goto out_free_ntlmsspblob;
1776 rc = sess_sendreceive(sess_data);
1778 goto out_free_ntlmsspblob;
1780 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1781 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1782 if (smb_buf->WordCount != 4) {
1784 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1785 goto out_free_ntlmsspblob;
1788 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1789 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1791 if (ses->Suid != smb_buf->Uid) {
1792 ses->Suid = smb_buf->Uid;
1793 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1796 bytes_remaining = get_bcc(smb_buf);
1797 bcc_ptr = pByteArea(smb_buf);
1798 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1799 if (blob_len > bytes_remaining) {
1800 cifs_dbg(VFS, "bad security blob length %d\n",
1803 goto out_free_ntlmsspblob;
1805 bcc_ptr += blob_len;
1806 bytes_remaining -= blob_len;
1809 /* BB check if Unicode and decode strings */
1810 if (bytes_remaining == 0) {
1811 /* no string area to decode, do nothing */
1812 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1813 /* unicode string area must be word-aligned */
1814 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1818 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1821 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1825 out_free_ntlmsspblob:
1826 kfree_sensitive(ntlmsspblob);
1828 sess_free_buffer(sess_data);
1831 rc = sess_establish_session(sess_data);
1834 kfree_sensitive(ses->auth_key.response);
1835 ses->auth_key.response = NULL;
1836 kfree_sensitive(ses->ntlmssp);
1837 ses->ntlmssp = NULL;
1839 sess_data->func = NULL;
1840 sess_data->result = rc;
1843 static int select_sec(struct sess_data *sess_data)
1846 struct cifs_ses *ses = sess_data->ses;
1847 struct TCP_Server_Info *server = sess_data->server;
1849 type = cifs_select_sectype(server, ses->sectype);
1850 cifs_dbg(FYI, "sess setup type %d\n", type);
1851 if (type == Unspecified) {
1852 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1858 sess_data->func = sess_auth_ntlmv2;
1861 #ifdef CONFIG_CIFS_UPCALL
1862 sess_data->func = sess_auth_kerberos;
1865 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1867 #endif /* CONFIG_CIFS_UPCALL */
1869 sess_data->func = sess_auth_rawntlmssp_negotiate;
1872 cifs_dbg(VFS, "secType %d not supported!\n", type);
1879 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1880 struct TCP_Server_Info *server,
1881 const struct nls_table *nls_cp)
1884 struct sess_data *sess_data;
1887 WARN(1, "%s: ses == NULL!", __func__);
1891 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1895 sess_data->xid = xid;
1896 sess_data->ses = ses;
1897 sess_data->server = server;
1898 sess_data->buf0_type = CIFS_NO_BUFFER;
1899 sess_data->nls_cp = (struct nls_table *) nls_cp;
1901 rc = select_sec(sess_data);
1905 while (sess_data->func)
1906 sess_data->func(sess_data);
1908 /* Store result before we free sess_data */
1909 rc = sess_data->result;
1912 kfree_sensitive(sess_data);
1915 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */