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 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses,
503 struct TCP_Server_Info *server,
504 SESSION_SETUP_ANDX *pSMB)
506 __u32 capabilities = 0;
508 /* init fields common to all four types of SessSetup */
509 /* Note that offsets for first seven fields in req struct are same */
510 /* in CIFS Specs so does not matter which of 3 forms of struct */
511 /* that we use in next few lines */
512 /* Note that header is initialized to zero in header_assemble */
513 pSMB->req.AndXCommand = 0xFF;
514 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
515 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
517 pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq);
518 pSMB->req.VcNumber = cpu_to_le16(1);
520 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
522 /* BB verify whether signing required on neg or just on auth frame
525 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
526 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
529 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
531 if (ses->capabilities & CAP_UNICODE) {
532 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
533 capabilities |= CAP_UNICODE;
535 if (ses->capabilities & CAP_STATUS32) {
536 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
537 capabilities |= CAP_STATUS32;
539 if (ses->capabilities & CAP_DFS) {
540 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
541 capabilities |= CAP_DFS;
543 if (ses->capabilities & CAP_UNIX)
544 capabilities |= CAP_UNIX;
550 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
552 char *bcc_ptr = *pbcc_area;
555 /* Copy OS version */
556 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
558 bcc_ptr += 2 * bytes_ret;
559 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
561 bcc_ptr += 2 * bytes_ret;
562 bcc_ptr += 2; /* trailing null */
564 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
566 bcc_ptr += 2 * bytes_ret;
567 bcc_ptr += 2; /* trailing null */
569 *pbcc_area = bcc_ptr;
572 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
573 const struct nls_table *nls_cp)
575 char *bcc_ptr = *pbcc_area;
579 if (ses->domainName == NULL) {
580 /* Sending null domain better than using a bogus domain name (as
581 we did briefly in 2.6.18) since server will use its default */
586 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
587 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
588 bcc_ptr += 2 * bytes_ret;
589 bcc_ptr += 2; /* account for null terminator */
591 *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);
757 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
758 struct cifs_ses *ses)
760 unsigned int tioffset; /* challenge message target info area */
761 unsigned int tilen; /* challenge message target info area length */
762 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
765 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
766 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
770 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
771 cifs_dbg(VFS, "blob signature incorrect %s\n",
775 if (pblob->MessageType != NtLmChallenge) {
776 cifs_dbg(VFS, "Incorrect message type %d\n",
781 server_flags = le32_to_cpu(pblob->NegotiateFlags);
782 cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__,
783 ses->ntlmssp->client_flags, server_flags);
785 if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) &&
786 (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) {
787 cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n",
791 if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) {
792 cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__);
795 if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) {
796 cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n",
800 if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
801 !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH))
802 pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n",
805 ses->ntlmssp->server_flags = server_flags;
807 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
808 /* In particular we can examine sign flags */
809 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
810 we must set the MIC field of the AUTHENTICATE_MESSAGE */
812 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
813 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
814 if (tioffset > blob_len || tioffset + tilen > blob_len) {
815 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
820 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
822 if (!ses->auth_key.response) {
823 cifs_dbg(VFS, "Challenge target info alloc failure\n");
826 ses->auth_key.len = tilen;
832 static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
834 int sz = base_size + ses->auth_key.len
835 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
838 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
840 sz += sizeof(__le16);
843 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
845 sz += sizeof(__le16);
847 if (ses->workstation_name[0])
848 sz += sizeof(__le16) * strnlen(ses->workstation_name,
849 ntlmssp_workstation_name_size(ses));
851 sz += sizeof(__le16);
856 static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
859 unsigned char *pstart,
860 unsigned char **pcur,
861 const struct nls_table *nls_cp)
863 unsigned char *tmp = pstart;
873 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
875 pbuf->MaximumLength = 0;
876 *pcur += sizeof(__le16);
878 len = cifs_strtoUTF16((__le16 *)*pcur,
882 len *= sizeof(__le16);
883 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
884 pbuf->Length = cpu_to_le16(len);
885 pbuf->MaximumLength = cpu_to_le16(len);
890 /* BB Move to ntlmssp.c eventually */
892 int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
894 struct cifs_ses *ses,
895 struct TCP_Server_Info *server,
896 const struct nls_table *nls_cp)
899 NEGOTIATE_MESSAGE *sec_blob;
904 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
905 *pbuffer = kmalloc(len, GFP_KERNEL);
908 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
910 goto setup_ntlm_neg_ret;
912 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
914 memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
915 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
916 sec_blob->MessageType = NtLmNegotiate;
918 /* BB is NTLMV2 session security format easier to use here? */
919 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
920 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
921 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
922 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
923 NTLMSSP_NEGOTIATE_SIGN;
924 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
925 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
927 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
928 ses->ntlmssp->client_flags = flags;
929 sec_blob->NegotiateFlags = cpu_to_le32(flags);
931 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
932 cifs_security_buffer_from_str(&sec_blob->DomainName,
934 CIFS_MAX_DOMAINNAME_LEN,
938 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
940 CIFS_MAX_WORKSTATION_LEN,
944 *buflen = tmp - *pbuffer;
950 * Build ntlmssp blob with additional fields, such as version,
951 * supported by modern servers. For safety limit to SMB3 or later
952 * See notes in MS-NLMP Section 2.2.2.1 e.g.
954 int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer,
956 struct cifs_ses *ses,
957 struct TCP_Server_Info *server,
958 const struct nls_table *nls_cp)
961 struct negotiate_message *sec_blob;
966 len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message));
967 *pbuffer = kmalloc(len, GFP_KERNEL);
970 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
972 goto setup_ntlm_smb3_neg_ret;
974 sec_blob = (struct negotiate_message *)*pbuffer;
976 memset(*pbuffer, 0, sizeof(struct negotiate_message));
977 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
978 sec_blob->MessageType = NtLmNegotiate;
980 /* BB is NTLMV2 session security format easier to use here? */
981 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
982 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
983 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
984 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
985 NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION;
986 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
987 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
989 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
990 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
991 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
992 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
994 tmp = *pbuffer + sizeof(struct negotiate_message);
995 ses->ntlmssp->client_flags = flags;
996 sec_blob->NegotiateFlags = cpu_to_le32(flags);
998 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
999 cifs_security_buffer_from_str(&sec_blob->DomainName,
1001 CIFS_MAX_DOMAINNAME_LEN,
1005 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1007 CIFS_MAX_WORKSTATION_LEN,
1011 *buflen = tmp - *pbuffer;
1012 setup_ntlm_smb3_neg_ret:
1017 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
1019 struct cifs_ses *ses,
1020 struct TCP_Server_Info *server,
1021 const struct nls_table *nls_cp)
1024 AUTHENTICATE_MESSAGE *sec_blob;
1029 rc = setup_ntlmv2_rsp(ses, nls_cp);
1031 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
1033 goto setup_ntlmv2_ret;
1036 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
1037 *pbuffer = kmalloc(len, GFP_KERNEL);
1040 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
1042 goto setup_ntlmv2_ret;
1044 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
1046 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1047 sec_blob->MessageType = NtLmAuthenticate;
1049 flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET |
1050 NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
1052 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
1053 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1055 sec_blob->LmChallengeResponse.BufferOffset =
1056 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
1057 sec_blob->LmChallengeResponse.Length = 0;
1058 sec_blob->LmChallengeResponse.MaximumLength = 0;
1060 sec_blob->NtChallengeResponse.BufferOffset =
1061 cpu_to_le32(tmp - *pbuffer);
1062 if (ses->user_name != NULL) {
1063 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1064 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1065 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1067 sec_blob->NtChallengeResponse.Length =
1068 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1069 sec_blob->NtChallengeResponse.MaximumLength =
1070 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1073 * don't send an NT Response for anonymous access
1075 sec_blob->NtChallengeResponse.Length = 0;
1076 sec_blob->NtChallengeResponse.MaximumLength = 0;
1079 cifs_security_buffer_from_str(&sec_blob->DomainName,
1081 CIFS_MAX_DOMAINNAME_LEN,
1085 cifs_security_buffer_from_str(&sec_blob->UserName,
1087 CIFS_MAX_USERNAME_LEN,
1091 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1092 ses->workstation_name,
1093 ntlmssp_workstation_name_size(ses),
1097 if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
1098 (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) &&
1099 !calc_seckey(ses)) {
1100 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
1101 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1102 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
1103 sec_blob->SessionKey.MaximumLength =
1104 cpu_to_le16(CIFS_CPHTXT_SIZE);
1105 tmp += CIFS_CPHTXT_SIZE;
1107 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1108 sec_blob->SessionKey.Length = 0;
1109 sec_blob->SessionKey.MaximumLength = 0;
1112 *buflen = tmp - *pbuffer;
1118 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1120 switch (server->negflavor) {
1121 case CIFS_NEGFLAVOR_EXTENDED:
1122 switch (requested) {
1127 if (server->sec_ntlmssp &&
1128 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1130 if ((server->sec_kerberos || server->sec_mskerberos) &&
1131 (global_secflags & CIFSSEC_MAY_KRB5))
1137 case CIFS_NEGFLAVOR_UNENCAP:
1138 switch (requested) {
1142 if (global_secflags & CIFSSEC_MAY_NTLMV2)
1156 struct cifs_ses *ses;
1157 struct TCP_Server_Info *server;
1158 struct nls_table *nls_cp;
1159 void (*func)(struct sess_data *);
1162 /* we will send the SMB in three pieces:
1163 * a fixed length beginning part, an optional
1164 * SPNEGO blob (which can be zero length), and a
1165 * last part which will include the strings
1166 * and rest of bcc area. This allows us to avoid
1167 * a large buffer 17K allocation
1174 sess_alloc_buffer(struct sess_data *sess_data, int wct)
1177 struct cifs_ses *ses = sess_data->ses;
1178 struct smb_hdr *smb_buf;
1180 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
1186 sess_data->iov[0].iov_base = (char *)smb_buf;
1187 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
1189 * This variable will be used to clear the buffer
1190 * allocated above in case of any error in the calling function.
1192 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1194 /* 2000 big enough to fit max user, domain, NOS name etc. */
1195 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
1196 if (!sess_data->iov[2].iov_base) {
1198 goto out_free_smb_buf;
1204 cifs_small_buf_release(smb_buf);
1205 sess_data->iov[0].iov_base = NULL;
1206 sess_data->iov[0].iov_len = 0;
1207 sess_data->buf0_type = CIFS_NO_BUFFER;
1212 sess_free_buffer(struct sess_data *sess_data)
1215 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
1216 sess_data->buf0_type = CIFS_NO_BUFFER;
1217 kfree(sess_data->iov[2].iov_base);
1221 sess_establish_session(struct sess_data *sess_data)
1223 struct cifs_ses *ses = sess_data->ses;
1224 struct TCP_Server_Info *server = sess_data->server;
1226 cifs_server_lock(server);
1227 if (!server->session_estab) {
1229 server->session_key.response =
1230 kmemdup(ses->auth_key.response,
1231 ses->auth_key.len, GFP_KERNEL);
1232 if (!server->session_key.response) {
1233 cifs_server_unlock(server);
1236 server->session_key.len =
1239 server->sequence_number = 0x2;
1240 server->session_estab = true;
1242 cifs_server_unlock(server);
1244 cifs_dbg(FYI, "CIFS session established successfully\n");
1249 sess_sendreceive(struct sess_data *sess_data)
1252 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
1254 struct kvec rsp_iov = { NULL, 0 };
1256 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
1257 be32_add_cpu(&smb_buf->smb_buf_length, count);
1258 put_bcc(count, smb_buf);
1260 rc = SendReceive2(sess_data->xid, sess_data->ses,
1261 sess_data->iov, 3 /* num_iovecs */,
1262 &sess_data->buf0_type,
1263 CIFS_LOG_ERROR, &rsp_iov);
1264 cifs_small_buf_release(sess_data->iov[0].iov_base);
1265 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1271 sess_auth_ntlmv2(struct sess_data *sess_data)
1274 struct smb_hdr *smb_buf;
1275 SESSION_SETUP_ANDX *pSMB;
1277 struct cifs_ses *ses = sess_data->ses;
1278 struct TCP_Server_Info *server = sess_data->server;
1280 __u16 bytes_remaining;
1282 /* old style NTLM sessionsetup */
1284 rc = sess_alloc_buffer(sess_data, 13);
1288 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1289 bcc_ptr = sess_data->iov[2].iov_base;
1290 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1292 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1294 /* LM2 password would be here if we supported it */
1295 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1297 if (ses->user_name != NULL) {
1298 /* calculate nlmv2 response and session key */
1299 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1301 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1305 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1306 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1307 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1309 /* set case sensitive password length after tilen may get
1310 * assigned, tilen is 0 otherwise.
1312 pSMB->req_no_secext.CaseSensitivePasswordLength =
1313 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1315 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1318 if (ses->capabilities & CAP_UNICODE) {
1319 if (sess_data->iov[0].iov_len % 2) {
1323 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1325 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1329 sess_data->iov[2].iov_len = (long) bcc_ptr -
1330 (long) sess_data->iov[2].iov_base;
1332 rc = sess_sendreceive(sess_data);
1336 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1337 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1339 if (smb_buf->WordCount != 3) {
1341 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1345 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1346 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1348 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1349 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1351 bytes_remaining = get_bcc(smb_buf);
1352 bcc_ptr = pByteArea(smb_buf);
1354 /* BB check if Unicode and decode strings */
1355 if (bytes_remaining == 0) {
1356 /* no string area to decode, do nothing */
1357 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1358 /* unicode string area must be word-aligned */
1359 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1363 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1366 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1370 rc = sess_establish_session(sess_data);
1372 sess_data->result = rc;
1373 sess_data->func = NULL;
1374 sess_free_buffer(sess_data);
1375 kfree(ses->auth_key.response);
1376 ses->auth_key.response = NULL;
1379 #ifdef CONFIG_CIFS_UPCALL
1381 sess_auth_kerberos(struct sess_data *sess_data)
1384 struct smb_hdr *smb_buf;
1385 SESSION_SETUP_ANDX *pSMB;
1387 struct cifs_ses *ses = sess_data->ses;
1388 struct TCP_Server_Info *server = sess_data->server;
1390 __u16 bytes_remaining;
1391 struct key *spnego_key = NULL;
1392 struct cifs_spnego_msg *msg;
1395 /* extended security */
1397 rc = sess_alloc_buffer(sess_data, 12);
1401 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1402 bcc_ptr = sess_data->iov[2].iov_base;
1403 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1405 spnego_key = cifs_get_spnego_key(ses, server);
1406 if (IS_ERR(spnego_key)) {
1407 rc = PTR_ERR(spnego_key);
1412 msg = spnego_key->payload.data[0];
1414 * check version field to make sure that cifs.upcall is
1415 * sending us a response in an expected form
1417 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1418 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1419 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1421 goto out_put_spnego_key;
1424 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1426 if (!ses->auth_key.response) {
1427 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1430 goto out_put_spnego_key;
1432 ses->auth_key.len = msg->sesskey_len;
1434 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1435 capabilities |= CAP_EXTENDED_SECURITY;
1436 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1437 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1438 sess_data->iov[1].iov_len = msg->secblob_len;
1439 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1441 if (ses->capabilities & CAP_UNICODE) {
1442 /* unicode strings must be word aligned */
1443 if ((sess_data->iov[0].iov_len
1444 + sess_data->iov[1].iov_len) % 2) {
1448 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1449 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1451 /* BB: is this right? */
1452 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1455 sess_data->iov[2].iov_len = (long) bcc_ptr -
1456 (long) sess_data->iov[2].iov_base;
1458 rc = sess_sendreceive(sess_data);
1460 goto out_put_spnego_key;
1462 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1463 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1465 if (smb_buf->WordCount != 4) {
1467 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1468 goto out_put_spnego_key;
1471 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1472 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1474 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1475 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1477 bytes_remaining = get_bcc(smb_buf);
1478 bcc_ptr = pByteArea(smb_buf);
1480 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1481 if (blob_len > bytes_remaining) {
1482 cifs_dbg(VFS, "bad security blob length %d\n",
1485 goto out_put_spnego_key;
1487 bcc_ptr += blob_len;
1488 bytes_remaining -= blob_len;
1490 /* BB check if Unicode and decode strings */
1491 if (bytes_remaining == 0) {
1492 /* no string area to decode, do nothing */
1493 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1494 /* unicode string area must be word-aligned */
1495 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1499 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1502 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1506 rc = sess_establish_session(sess_data);
1508 key_invalidate(spnego_key);
1509 key_put(spnego_key);
1511 sess_data->result = rc;
1512 sess_data->func = NULL;
1513 sess_free_buffer(sess_data);
1514 kfree(ses->auth_key.response);
1515 ses->auth_key.response = NULL;
1518 #endif /* ! CONFIG_CIFS_UPCALL */
1521 * The required kvec buffers have to be allocated before calling this
1525 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1527 SESSION_SETUP_ANDX *pSMB;
1528 struct cifs_ses *ses = sess_data->ses;
1529 struct TCP_Server_Info *server = sess_data->server;
1533 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1535 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1536 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1537 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1541 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1542 capabilities |= CAP_EXTENDED_SECURITY;
1543 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1545 bcc_ptr = sess_data->iov[2].iov_base;
1546 /* unicode strings must be word aligned */
1547 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1551 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1553 sess_data->iov[2].iov_len = (long) bcc_ptr -
1554 (long) sess_data->iov[2].iov_base;
1560 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1563 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1566 struct smb_hdr *smb_buf;
1567 SESSION_SETUP_ANDX *pSMB;
1568 struct cifs_ses *ses = sess_data->ses;
1569 struct TCP_Server_Info *server = sess_data->server;
1570 __u16 bytes_remaining;
1572 unsigned char *ntlmsspblob = NULL;
1575 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1578 * if memory allocation is successful, caller of this function
1581 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1582 if (!ses->ntlmssp) {
1586 ses->ntlmssp->sesskey_per_smbsess = false;
1589 rc = sess_alloc_buffer(sess_data, 12);
1593 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1595 /* Build security blob before we assemble the request */
1596 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
1597 &blob_len, ses, server,
1600 goto out_free_ntlmsspblob;
1602 sess_data->iov[1].iov_len = blob_len;
1603 sess_data->iov[1].iov_base = ntlmsspblob;
1604 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1606 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1608 goto out_free_ntlmsspblob;
1610 rc = sess_sendreceive(sess_data);
1612 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1613 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1615 /* If true, rc here is expected and not an error */
1616 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1617 smb_buf->Status.CifsError ==
1618 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1622 goto out_free_ntlmsspblob;
1624 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1626 if (smb_buf->WordCount != 4) {
1628 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1629 goto out_free_ntlmsspblob;
1632 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1633 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1635 bytes_remaining = get_bcc(smb_buf);
1636 bcc_ptr = pByteArea(smb_buf);
1638 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1639 if (blob_len > bytes_remaining) {
1640 cifs_dbg(VFS, "bad security blob length %d\n",
1643 goto out_free_ntlmsspblob;
1646 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1648 out_free_ntlmsspblob:
1651 sess_free_buffer(sess_data);
1654 sess_data->func = sess_auth_rawntlmssp_authenticate;
1658 /* Else error. Cleanup */
1659 kfree(ses->auth_key.response);
1660 ses->auth_key.response = NULL;
1661 kfree(ses->ntlmssp);
1662 ses->ntlmssp = NULL;
1664 sess_data->func = NULL;
1665 sess_data->result = rc;
1669 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1672 struct smb_hdr *smb_buf;
1673 SESSION_SETUP_ANDX *pSMB;
1674 struct cifs_ses *ses = sess_data->ses;
1675 struct TCP_Server_Info *server = sess_data->server;
1676 __u16 bytes_remaining;
1678 unsigned char *ntlmsspblob = NULL;
1681 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1684 rc = sess_alloc_buffer(sess_data, 12);
1688 /* Build security blob before we assemble the request */
1689 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1690 smb_buf = (struct smb_hdr *)pSMB;
1691 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1692 &blob_len, ses, server,
1695 goto out_free_ntlmsspblob;
1696 sess_data->iov[1].iov_len = blob_len;
1697 sess_data->iov[1].iov_base = ntlmsspblob;
1698 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1700 * Make sure that we tell the server that we are using
1701 * the uid that it just gave us back on the response
1704 smb_buf->Uid = ses->Suid;
1706 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1708 goto out_free_ntlmsspblob;
1710 rc = sess_sendreceive(sess_data);
1712 goto out_free_ntlmsspblob;
1714 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1715 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1716 if (smb_buf->WordCount != 4) {
1718 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1719 goto out_free_ntlmsspblob;
1722 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1723 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1725 if (ses->Suid != smb_buf->Uid) {
1726 ses->Suid = smb_buf->Uid;
1727 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1730 bytes_remaining = get_bcc(smb_buf);
1731 bcc_ptr = pByteArea(smb_buf);
1732 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1733 if (blob_len > bytes_remaining) {
1734 cifs_dbg(VFS, "bad security blob length %d\n",
1737 goto out_free_ntlmsspblob;
1739 bcc_ptr += blob_len;
1740 bytes_remaining -= blob_len;
1743 /* BB check if Unicode and decode strings */
1744 if (bytes_remaining == 0) {
1745 /* no string area to decode, do nothing */
1746 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1747 /* unicode string area must be word-aligned */
1748 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1752 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1755 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1759 out_free_ntlmsspblob:
1762 sess_free_buffer(sess_data);
1765 rc = sess_establish_session(sess_data);
1768 kfree(ses->auth_key.response);
1769 ses->auth_key.response = NULL;
1770 kfree(ses->ntlmssp);
1771 ses->ntlmssp = NULL;
1773 sess_data->func = NULL;
1774 sess_data->result = rc;
1777 static int select_sec(struct sess_data *sess_data)
1780 struct cifs_ses *ses = sess_data->ses;
1781 struct TCP_Server_Info *server = sess_data->server;
1783 type = cifs_select_sectype(server, ses->sectype);
1784 cifs_dbg(FYI, "sess setup type %d\n", type);
1785 if (type == Unspecified) {
1786 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1792 sess_data->func = sess_auth_ntlmv2;
1795 #ifdef CONFIG_CIFS_UPCALL
1796 sess_data->func = sess_auth_kerberos;
1799 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1801 #endif /* CONFIG_CIFS_UPCALL */
1803 sess_data->func = sess_auth_rawntlmssp_negotiate;
1806 cifs_dbg(VFS, "secType %d not supported!\n", type);
1813 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1814 struct TCP_Server_Info *server,
1815 const struct nls_table *nls_cp)
1818 struct sess_data *sess_data;
1821 WARN(1, "%s: ses == NULL!", __func__);
1825 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1829 sess_data->xid = xid;
1830 sess_data->ses = ses;
1831 sess_data->server = server;
1832 sess_data->buf0_type = CIFS_NO_BUFFER;
1833 sess_data->nls_cp = (struct nls_table *) nls_cp;
1835 rc = select_sec(sess_data);
1839 while (sess_data->func)
1840 sess_data->func(sess_data);
1842 /* Store result before we free sess_data */
1843 rc = sess_data->result;