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_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",
88 return CIFS_INVAL_CHAN_INDEX;
92 cifs_chan_set_in_reconnect(struct cifs_ses *ses,
93 struct TCP_Server_Info *server)
95 int chan_index = cifs_ses_get_chan_index(ses, server);
97 if (chan_index == CIFS_INVAL_CHAN_INDEX)
100 ses->chans[chan_index].in_reconnect = true;
104 cifs_chan_clear_in_reconnect(struct cifs_ses *ses,
105 struct TCP_Server_Info *server)
107 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
109 if (chan_index == CIFS_INVAL_CHAN_INDEX)
112 ses->chans[chan_index].in_reconnect = false;
116 cifs_chan_in_reconnect(struct cifs_ses *ses,
117 struct TCP_Server_Info *server)
119 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
121 if (chan_index == CIFS_INVAL_CHAN_INDEX)
122 return true; /* err on the safer side */
124 return CIFS_CHAN_IN_RECONNECT(ses, chan_index);
128 cifs_chan_set_need_reconnect(struct cifs_ses *ses,
129 struct TCP_Server_Info *server)
131 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
133 if (chan_index == CIFS_INVAL_CHAN_INDEX)
136 set_bit(chan_index, &ses->chans_need_reconnect);
137 cifs_dbg(FYI, "Set reconnect bitmask for chan %u; now 0x%lx\n",
138 chan_index, ses->chans_need_reconnect);
142 cifs_chan_clear_need_reconnect(struct cifs_ses *ses,
143 struct TCP_Server_Info *server)
145 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
147 if (chan_index == CIFS_INVAL_CHAN_INDEX)
150 clear_bit(chan_index, &ses->chans_need_reconnect);
151 cifs_dbg(FYI, "Cleared reconnect bitmask for chan %u; now 0x%lx\n",
152 chan_index, ses->chans_need_reconnect);
156 cifs_chan_needs_reconnect(struct cifs_ses *ses,
157 struct TCP_Server_Info *server)
159 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
161 if (chan_index == CIFS_INVAL_CHAN_INDEX)
162 return true; /* err on the safer side */
164 return CIFS_CHAN_NEEDS_RECONNECT(ses, chan_index);
168 cifs_chan_is_iface_active(struct cifs_ses *ses,
169 struct TCP_Server_Info *server)
171 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
173 if (chan_index == CIFS_INVAL_CHAN_INDEX)
174 return true; /* err on the safer side */
176 return ses->chans[chan_index].iface &&
177 ses->chans[chan_index].iface->is_active;
180 /* returns number of channels added */
181 int cifs_try_adding_channels(struct cifs_ses *ses)
183 struct TCP_Server_Info *server = ses->server;
184 int old_chan_count, new_chan_count;
188 size_t iface_weight = 0, iface_min_speed = 0;
189 struct cifs_server_iface *iface = NULL, *niface = NULL;
190 struct cifs_server_iface *last_iface = NULL;
192 spin_lock(&ses->chan_lock);
194 new_chan_count = old_chan_count = ses->chan_count;
195 left = ses->chan_max - ses->chan_count;
198 spin_unlock(&ses->chan_lock);
200 "ses already at max_channels (%zu), nothing to open\n",
205 if (server->dialect < SMB30_PROT_ID) {
206 spin_unlock(&ses->chan_lock);
207 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
211 if (!(server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
212 spin_unlock(&ses->chan_lock);
213 cifs_server_dbg(VFS, "no multichannel support\n");
216 spin_unlock(&ses->chan_lock);
221 if (tries > 3*ses->chan_max) {
222 cifs_dbg(VFS, "too many channel open attempts (%d channels left to open)\n",
227 spin_lock(&ses->iface_lock);
228 if (!ses->iface_count) {
229 spin_unlock(&ses->iface_lock);
230 cifs_dbg(VFS, "server %s does not advertise interfaces\n",
231 ses->server->hostname);
236 iface = list_first_entry(&ses->iface_list, struct cifs_server_iface,
238 last_iface = list_last_entry(&ses->iface_list, struct cifs_server_iface,
240 iface_min_speed = last_iface->speed;
242 list_for_each_entry_safe_from(iface, niface, &ses->iface_list,
244 /* do not mix rdma and non-rdma interfaces */
245 if (iface->rdma_capable != ses->server->rdma)
248 /* skip ifaces that are unusable */
249 if (!iface->is_active ||
250 (is_ses_using_iface(ses, iface) &&
251 !iface->rss_capable))
254 /* check if we already allocated enough channels */
255 iface_weight = iface->speed / iface_min_speed;
257 if (iface->weight_fulfilled >= iface_weight)
260 /* take ref before unlock */
261 kref_get(&iface->refcount);
263 spin_unlock(&ses->iface_lock);
264 rc = cifs_ses_add_channel(ses, iface);
265 spin_lock(&ses->iface_lock);
268 cifs_dbg(VFS, "failed to open extra channel on iface:%pIS rc=%d\n",
271 kref_put(&iface->refcount, release_iface);
275 iface->num_channels++;
276 iface->weight_fulfilled++;
277 cifs_dbg(VFS, "successfully opened new channel on iface:%pIS\n",
282 /* reached end of list. reset weight_fulfilled and start over */
283 if (list_entry_is_head(iface, &ses->iface_list, iface_head)) {
284 list_for_each_entry(iface, &ses->iface_list, iface_head)
285 iface->weight_fulfilled = 0;
286 spin_unlock(&ses->iface_lock);
290 spin_unlock(&ses->iface_lock);
296 return new_chan_count - old_chan_count;
300 * called when multichannel is disabled by the server.
301 * this always gets called from smb2_reconnect
302 * and cannot get called in parallel threads.
305 cifs_disable_secondary_channels(struct cifs_ses *ses)
308 struct TCP_Server_Info *server;
309 struct cifs_server_iface *iface;
311 spin_lock(&ses->chan_lock);
312 chan_count = ses->chan_count;
318 /* for all secondary channels reset the need reconnect bit */
319 ses->chans_need_reconnect &= 1;
321 for (i = 1; i < chan_count; i++) {
322 iface = ses->chans[i].iface;
323 server = ses->chans[i].server;
326 * remove these references first, since we need to unlock
327 * the chan_lock here, since iface_lock is a higher lock
329 ses->chans[i].iface = NULL;
330 ses->chans[i].server = NULL;
331 spin_unlock(&ses->chan_lock);
334 spin_lock(&ses->iface_lock);
335 iface->num_channels--;
336 if (iface->weight_fulfilled)
337 iface->weight_fulfilled--;
338 kref_put(&iface->refcount, release_iface);
339 spin_unlock(&ses->iface_lock);
343 if (!server->terminate) {
344 server->terminate = true;
345 cifs_signal_cifsd_for_reconnect(server, false);
347 cifs_put_tcp_session(server, false);
350 spin_lock(&ses->chan_lock);
354 spin_unlock(&ses->chan_lock);
358 * update the iface for the channel if necessary.
359 * will return 0 when iface is updated, 1 if removed, 2 otherwise
360 * Must be called with chan_lock held.
363 cifs_chan_update_iface(struct cifs_ses *ses, struct TCP_Server_Info *server)
365 unsigned int chan_index;
366 size_t iface_weight = 0, iface_min_speed = 0;
367 struct cifs_server_iface *iface = NULL;
368 struct cifs_server_iface *old_iface = NULL;
369 struct cifs_server_iface *last_iface = NULL;
370 struct sockaddr_storage ss;
373 spin_lock(&ses->chan_lock);
374 chan_index = cifs_ses_get_chan_index(ses, server);
375 if (chan_index == CIFS_INVAL_CHAN_INDEX) {
376 spin_unlock(&ses->chan_lock);
380 if (ses->chans[chan_index].iface) {
381 old_iface = ses->chans[chan_index].iface;
382 if (old_iface->is_active) {
383 spin_unlock(&ses->chan_lock);
387 spin_unlock(&ses->chan_lock);
389 spin_lock(&server->srv_lock);
390 ss = server->dstaddr;
391 spin_unlock(&server->srv_lock);
393 spin_lock(&ses->iface_lock);
394 if (!ses->iface_count) {
395 spin_unlock(&ses->iface_lock);
396 cifs_dbg(VFS, "server %s does not advertise interfaces\n", ses->server->hostname);
400 last_iface = list_last_entry(&ses->iface_list, struct cifs_server_iface,
402 iface_min_speed = last_iface->speed;
404 /* then look for a new one */
405 list_for_each_entry(iface, &ses->iface_list, iface_head) {
407 /* if we're trying to get the updated iface for primary channel */
408 if (!cifs_match_ipaddr((struct sockaddr *) &ss,
409 (struct sockaddr *) &iface->sockaddr))
412 kref_get(&iface->refcount);
416 /* do not mix rdma and non-rdma interfaces */
417 if (iface->rdma_capable != server->rdma)
420 if (!iface->is_active ||
421 (is_ses_using_iface(ses, iface) &&
422 !iface->rss_capable)) {
426 /* check if we already allocated enough channels */
427 iface_weight = iface->speed / iface_min_speed;
429 if (iface->weight_fulfilled >= iface_weight)
432 kref_get(&iface->refcount);
436 if (list_entry_is_head(iface, &ses->iface_list, iface_head)) {
439 cifs_dbg(FYI, "unable to find a suitable iface\n");
444 cifs_dbg(FYI, "unable to get the interface matching: %pIS\n",
447 cifs_dbg(FYI, "unable to find another interface to replace: %pIS\n",
448 &old_iface->sockaddr);
451 spin_unlock(&ses->iface_lock);
455 /* now drop the ref to the current iface */
457 cifs_dbg(FYI, "replacing iface: %pIS with %pIS\n",
458 &old_iface->sockaddr,
461 old_iface->num_channels--;
462 if (old_iface->weight_fulfilled)
463 old_iface->weight_fulfilled--;
464 iface->num_channels++;
465 iface->weight_fulfilled++;
467 kref_put(&old_iface->refcount, release_iface);
468 } else if (!chan_index) {
469 /* special case: update interface for primary channel */
471 cifs_dbg(FYI, "referencing primary channel iface: %pIS\n",
473 iface->num_channels++;
474 iface->weight_fulfilled++;
477 spin_unlock(&ses->iface_lock);
480 spin_lock(&ses->chan_lock);
481 chan_index = cifs_ses_get_chan_index(ses, server);
482 if (chan_index == CIFS_INVAL_CHAN_INDEX) {
483 spin_unlock(&ses->chan_lock);
487 ses->chans[chan_index].iface = iface;
488 spin_unlock(&ses->chan_lock);
495 * If server is a channel of ses, return the corresponding enclosing
496 * cifs_chan otherwise return NULL.
499 cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
503 spin_lock(&ses->chan_lock);
504 for (i = 0; i < ses->chan_count; i++) {
505 if (ses->chans[i].server == server) {
506 spin_unlock(&ses->chan_lock);
507 return &ses->chans[i];
510 spin_unlock(&ses->chan_lock);
515 cifs_ses_add_channel(struct cifs_ses *ses,
516 struct cifs_server_iface *iface)
518 struct TCP_Server_Info *chan_server;
519 struct cifs_chan *chan;
520 struct smb3_fs_context *ctx;
521 static const char unc_fmt[] = "\\%s\\foo";
522 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
523 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
526 unsigned int xid = get_xid();
528 if (iface->sockaddr.ss_family == AF_INET)
529 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
530 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
533 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
534 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
538 * Setup a ctx with mostly the same info as the existing
539 * session and overwrite it with the requested iface data.
541 * We need to setup at least the fields used for negprot and
544 * We only need the ctx here, so we can reuse memory from
545 * the session and server without caring about memory
548 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
554 /* Always make new connection for now (TODO?) */
555 ctx->nosharesock = true;
558 ctx->domainauto = ses->domainAuto;
559 ctx->domainname = ses->domainName;
561 /* no hostname for extra channels */
562 ctx->server_hostname = "";
564 ctx->username = ses->user_name;
565 ctx->password = ses->password;
566 ctx->sectype = ses->sectype;
567 ctx->sign = ses->sign;
570 /* XXX: Use ses->server->hostname? */
571 len = sizeof(unc_fmt) + SERVER_NAME_LEN_WITH_NULL;
572 ctx->UNC = kzalloc(len, GFP_KERNEL);
577 scnprintf(ctx->UNC, len, unc_fmt, ses->ip_addr);
580 /* Reuse same version as master connection */
581 ctx->vals = ses->server->vals;
582 ctx->ops = ses->server->ops;
584 ctx->noblocksnd = ses->server->noblocksnd;
585 ctx->noautotune = ses->server->noautotune;
586 ctx->sockopt_tcp_nodelay = ses->server->tcp_nodelay;
587 ctx->echo_interval = ses->server->echo_interval / HZ;
588 ctx->max_credits = ses->server->max_credits;
591 * This will be used for encoding/decoding user/domain/pw
592 * during sess setup auth.
594 ctx->local_nls = ses->local_nls;
596 /* Use RDMA if possible */
597 ctx->rdma = iface->rdma_capable;
598 memcpy(&ctx->dstaddr, &iface->sockaddr, sizeof(ctx->dstaddr));
600 /* reuse master con client guid */
601 memcpy(&ctx->client_guid, ses->server->client_guid,
602 sizeof(ctx->client_guid));
603 ctx->use_client_guid = true;
605 chan_server = cifs_get_tcp_session(ctx, ses->server);
607 spin_lock(&ses->chan_lock);
608 chan = &ses->chans[ses->chan_count];
609 chan->server = chan_server;
610 if (IS_ERR(chan->server)) {
611 rc = PTR_ERR(chan->server);
613 spin_unlock(&ses->chan_lock);
618 atomic_set(&ses->chan_seq, 0);
620 /* Mark this channel as needing connect/setup */
621 cifs_chan_set_need_reconnect(ses, chan->server);
623 spin_unlock(&ses->chan_lock);
625 mutex_lock(&ses->session_mutex);
627 * We need to allocate the server crypto now as we will need
628 * to sign packets before we generate the channel signing key
629 * (we sign with the session key)
631 rc = smb311_crypto_shash_allocate(chan->server);
633 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
634 mutex_unlock(&ses->session_mutex);
638 rc = cifs_negotiate_protocol(xid, ses, chan->server);
640 rc = cifs_setup_session(xid, ses, chan->server, ses->local_nls);
642 mutex_unlock(&ses->session_mutex);
645 if (rc && chan->server) {
646 cifs_put_tcp_session(chan->server, 0);
648 spin_lock(&ses->chan_lock);
650 /* we rely on all bits beyond chan_count to be clear */
651 cifs_chan_clear_need_reconnect(ses, chan->server);
654 * chan_count should never reach 0 as at least the primary
655 * channel is always allocated
657 WARN_ON(ses->chan_count < 1);
658 spin_unlock(&ses->chan_lock);
669 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
670 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses,
671 struct TCP_Server_Info *server,
672 SESSION_SETUP_ANDX *pSMB)
674 __u32 capabilities = 0;
676 /* init fields common to all four types of SessSetup */
677 /* Note that offsets for first seven fields in req struct are same */
678 /* in CIFS Specs so does not matter which of 3 forms of struct */
679 /* that we use in next few lines */
680 /* Note that header is initialized to zero in header_assemble */
681 pSMB->req.AndXCommand = 0xFF;
682 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
683 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
685 pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq);
686 pSMB->req.VcNumber = cpu_to_le16(1);
688 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
690 /* BB verify whether signing required on neg or just auth frame (and NTLM case) */
692 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
693 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
696 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
698 if (ses->capabilities & CAP_UNICODE) {
699 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
700 capabilities |= CAP_UNICODE;
702 if (ses->capabilities & CAP_STATUS32) {
703 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
704 capabilities |= CAP_STATUS32;
706 if (ses->capabilities & CAP_DFS) {
707 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
708 capabilities |= CAP_DFS;
710 if (ses->capabilities & CAP_UNIX)
711 capabilities |= CAP_UNIX;
717 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
719 char *bcc_ptr = *pbcc_area;
722 /* Copy OS version */
723 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
725 bcc_ptr += 2 * bytes_ret;
726 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
728 bcc_ptr += 2 * bytes_ret;
729 bcc_ptr += 2; /* trailing null */
731 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
733 bcc_ptr += 2 * bytes_ret;
734 bcc_ptr += 2; /* trailing null */
736 *pbcc_area = bcc_ptr;
739 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
740 const struct nls_table *nls_cp)
742 char *bcc_ptr = *pbcc_area;
746 if (ses->domainName == NULL) {
748 * Sending null domain better than using a bogus domain name (as
749 * we did briefly in 2.6.18) since server will use its default
755 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
756 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
757 bcc_ptr += 2 * bytes_ret;
758 bcc_ptr += 2; /* account for null terminator */
760 *pbcc_area = bcc_ptr;
763 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
764 const struct nls_table *nls_cp)
766 char *bcc_ptr = *pbcc_area;
769 /* BB FIXME add check that strings less than 335 or will need to send as arrays */
772 if (ses->user_name == NULL) {
773 /* null user mount */
777 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
778 CIFS_MAX_USERNAME_LEN, nls_cp);
780 bcc_ptr += 2 * bytes_ret;
781 bcc_ptr += 2; /* account for null termination */
783 unicode_domain_string(&bcc_ptr, ses, nls_cp);
784 unicode_oslm_strings(&bcc_ptr, nls_cp);
786 *pbcc_area = bcc_ptr;
789 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
790 const struct nls_table *nls_cp)
792 char *bcc_ptr = *pbcc_area;
796 /* BB what about null user mounts - check that we do this BB */
798 if (ses->user_name != NULL) {
799 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
800 if (WARN_ON_ONCE(len < 0))
801 len = CIFS_MAX_USERNAME_LEN - 1;
804 /* else null user mount */
806 bcc_ptr++; /* account for null termination */
809 if (ses->domainName != NULL) {
810 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
811 if (WARN_ON_ONCE(len < 0))
812 len = CIFS_MAX_DOMAINNAME_LEN - 1;
814 } /* else we send a null domain name so server will default to its own domain */
818 /* BB check for overflow here */
820 strcpy(bcc_ptr, "Linux version ");
821 bcc_ptr += strlen("Linux version ");
822 strcpy(bcc_ptr, init_utsname()->release);
823 bcc_ptr += strlen(init_utsname()->release) + 1;
825 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
826 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
828 *pbcc_area = bcc_ptr;
832 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
833 const struct nls_table *nls_cp)
836 char *data = *pbcc_area;
838 cifs_dbg(FYI, "bleft %d\n", bleft);
840 kfree(ses->serverOS);
841 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
842 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
843 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
849 kfree(ses->serverNOS);
850 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
851 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
852 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
858 kfree(ses->serverDomain);
859 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
860 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
865 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
866 struct cifs_ses *ses,
867 const struct nls_table *nls_cp)
870 char *bcc_ptr = *pbcc_area;
872 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
874 len = strnlen(bcc_ptr, bleft);
878 kfree(ses->serverOS);
880 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
882 memcpy(ses->serverOS, bcc_ptr, len);
883 ses->serverOS[len] = 0;
884 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
885 cifs_dbg(FYI, "OS/2 server\n");
891 len = strnlen(bcc_ptr, bleft);
895 kfree(ses->serverNOS);
897 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
898 if (ses->serverNOS) {
899 memcpy(ses->serverNOS, bcc_ptr, len);
900 ses->serverNOS[len] = 0;
906 len = strnlen(bcc_ptr, bleft);
911 * No domain field in LANMAN case. Domain is
912 * returned by old servers in the SMB negprot response
914 * BB For newer servers which do not support Unicode,
915 * but thus do return domain here, we could add parsing
916 * for it later, but it is not very important
918 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
920 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
922 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
923 struct cifs_ses *ses)
925 unsigned int tioffset; /* challenge message target info area */
926 unsigned int tilen; /* challenge message target info area length */
927 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
930 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
931 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
935 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
936 cifs_dbg(VFS, "blob signature incorrect %s\n",
940 if (pblob->MessageType != NtLmChallenge) {
941 cifs_dbg(VFS, "Incorrect message type %d\n",
946 server_flags = le32_to_cpu(pblob->NegotiateFlags);
947 cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__,
948 ses->ntlmssp->client_flags, server_flags);
950 if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) &&
951 (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) {
952 cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n",
956 if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) {
957 cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__);
960 if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) {
961 cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n",
965 if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
966 !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH))
967 pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n",
970 ses->ntlmssp->server_flags = server_flags;
972 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
974 * In particular we can examine sign flags
976 * BB spec says that if AvId field of MsvAvTimestamp is populated then
977 * we must set the MIC field of the AUTHENTICATE_MESSAGE
980 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
981 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
982 if (tioffset > blob_len || tioffset + tilen > blob_len) {
983 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
988 kfree_sensitive(ses->auth_key.response);
989 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
991 if (!ses->auth_key.response) {
992 cifs_dbg(VFS, "Challenge target info alloc failure\n");
995 ses->auth_key.len = tilen;
1001 static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
1003 int sz = base_size + ses->auth_key.len
1004 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
1006 if (ses->domainName)
1007 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
1009 sz += sizeof(__le16);
1012 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
1014 sz += sizeof(__le16);
1016 if (ses->workstation_name[0])
1017 sz += sizeof(__le16) * strnlen(ses->workstation_name,
1018 ntlmssp_workstation_name_size(ses));
1020 sz += sizeof(__le16);
1025 static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
1028 unsigned char *pstart,
1029 unsigned char **pcur,
1030 const struct nls_table *nls_cp)
1032 unsigned char *tmp = pstart;
1042 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
1044 pbuf->MaximumLength = 0;
1045 *pcur += sizeof(__le16);
1047 len = cifs_strtoUTF16((__le16 *)*pcur,
1051 len *= sizeof(__le16);
1052 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
1053 pbuf->Length = cpu_to_le16(len);
1054 pbuf->MaximumLength = cpu_to_le16(len);
1059 /* BB Move to ntlmssp.c eventually */
1061 int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
1063 struct cifs_ses *ses,
1064 struct TCP_Server_Info *server,
1065 const struct nls_table *nls_cp)
1068 NEGOTIATE_MESSAGE *sec_blob;
1073 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
1074 *pbuffer = kmalloc(len, GFP_KERNEL);
1077 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
1079 goto setup_ntlm_neg_ret;
1081 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
1083 memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
1084 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1085 sec_blob->MessageType = NtLmNegotiate;
1087 /* BB is NTLMV2 session security format easier to use here? */
1088 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
1089 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
1090 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
1091 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
1092 NTLMSSP_NEGOTIATE_SIGN;
1093 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
1094 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
1096 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
1097 ses->ntlmssp->client_flags = flags;
1098 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1100 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
1101 cifs_security_buffer_from_str(&sec_blob->DomainName,
1103 CIFS_MAX_DOMAINNAME_LEN,
1107 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1109 CIFS_MAX_WORKSTATION_LEN,
1113 *buflen = tmp - *pbuffer;
1119 * Build ntlmssp blob with additional fields, such as version,
1120 * supported by modern servers. For safety limit to SMB3 or later
1121 * See notes in MS-NLMP Section 2.2.2.1 e.g.
1123 int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer,
1125 struct cifs_ses *ses,
1126 struct TCP_Server_Info *server,
1127 const struct nls_table *nls_cp)
1130 struct negotiate_message *sec_blob;
1135 len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message));
1136 *pbuffer = kmalloc(len, GFP_KERNEL);
1139 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
1141 goto setup_ntlm_smb3_neg_ret;
1143 sec_blob = (struct negotiate_message *)*pbuffer;
1145 memset(*pbuffer, 0, sizeof(struct negotiate_message));
1146 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1147 sec_blob->MessageType = NtLmNegotiate;
1149 /* BB is NTLMV2 session security format easier to use here? */
1150 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
1151 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
1152 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
1153 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
1154 NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION;
1155 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
1156 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
1158 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
1159 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
1160 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
1161 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
1163 tmp = *pbuffer + sizeof(struct negotiate_message);
1164 ses->ntlmssp->client_flags = flags;
1165 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1167 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
1168 cifs_security_buffer_from_str(&sec_blob->DomainName,
1170 CIFS_MAX_DOMAINNAME_LEN,
1174 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1176 CIFS_MAX_WORKSTATION_LEN,
1180 *buflen = tmp - *pbuffer;
1181 setup_ntlm_smb3_neg_ret:
1186 /* See MS-NLMP 2.2.1.3 */
1187 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
1189 struct cifs_ses *ses,
1190 struct TCP_Server_Info *server,
1191 const struct nls_table *nls_cp)
1194 AUTHENTICATE_MESSAGE *sec_blob;
1199 rc = setup_ntlmv2_rsp(ses, nls_cp);
1201 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
1203 goto setup_ntlmv2_ret;
1206 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
1207 *pbuffer = kmalloc(len, GFP_KERNEL);
1210 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
1212 goto setup_ntlmv2_ret;
1214 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
1216 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1217 sec_blob->MessageType = NtLmAuthenticate;
1219 /* send version information in ntlmssp authenticate also */
1220 flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET |
1221 NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_VERSION |
1222 NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
1224 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
1225 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
1226 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
1227 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
1229 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
1230 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1232 sec_blob->LmChallengeResponse.BufferOffset =
1233 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
1234 sec_blob->LmChallengeResponse.Length = 0;
1235 sec_blob->LmChallengeResponse.MaximumLength = 0;
1237 sec_blob->NtChallengeResponse.BufferOffset =
1238 cpu_to_le32(tmp - *pbuffer);
1239 if (ses->user_name != NULL) {
1240 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1241 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1242 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1244 sec_blob->NtChallengeResponse.Length =
1245 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1246 sec_blob->NtChallengeResponse.MaximumLength =
1247 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1250 * don't send an NT Response for anonymous access
1252 sec_blob->NtChallengeResponse.Length = 0;
1253 sec_blob->NtChallengeResponse.MaximumLength = 0;
1256 cifs_security_buffer_from_str(&sec_blob->DomainName,
1258 CIFS_MAX_DOMAINNAME_LEN,
1262 cifs_security_buffer_from_str(&sec_blob->UserName,
1264 CIFS_MAX_USERNAME_LEN,
1268 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1269 ses->workstation_name,
1270 ntlmssp_workstation_name_size(ses),
1274 if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
1275 (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) &&
1276 !calc_seckey(ses)) {
1277 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
1278 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1279 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
1280 sec_blob->SessionKey.MaximumLength =
1281 cpu_to_le16(CIFS_CPHTXT_SIZE);
1282 tmp += CIFS_CPHTXT_SIZE;
1284 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1285 sec_blob->SessionKey.Length = 0;
1286 sec_blob->SessionKey.MaximumLength = 0;
1289 *buflen = tmp - *pbuffer;
1295 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1297 switch (server->negflavor) {
1298 case CIFS_NEGFLAVOR_EXTENDED:
1299 switch (requested) {
1304 if (server->sec_ntlmssp &&
1305 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1307 if ((server->sec_kerberos || server->sec_mskerberos) &&
1308 (global_secflags & CIFSSEC_MAY_KRB5))
1314 case CIFS_NEGFLAVOR_UNENCAP:
1315 switch (requested) {
1319 if (global_secflags & CIFSSEC_MAY_NTLMV2)
1333 struct cifs_ses *ses;
1334 struct TCP_Server_Info *server;
1335 struct nls_table *nls_cp;
1336 void (*func)(struct sess_data *);
1339 /* we will send the SMB in three pieces:
1340 * a fixed length beginning part, an optional
1341 * SPNEGO blob (which can be zero length), and a
1342 * last part which will include the strings
1343 * and rest of bcc area. This allows us to avoid
1344 * a large buffer 17K allocation
1350 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1352 sess_alloc_buffer(struct sess_data *sess_data, int wct)
1355 struct cifs_ses *ses = sess_data->ses;
1356 struct smb_hdr *smb_buf;
1358 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
1364 sess_data->iov[0].iov_base = (char *)smb_buf;
1365 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
1367 * This variable will be used to clear the buffer
1368 * allocated above in case of any error in the calling function.
1370 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1372 /* 2000 big enough to fit max user, domain, NOS name etc. */
1373 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
1374 if (!sess_data->iov[2].iov_base) {
1376 goto out_free_smb_buf;
1382 cifs_small_buf_release(smb_buf);
1383 sess_data->iov[0].iov_base = NULL;
1384 sess_data->iov[0].iov_len = 0;
1385 sess_data->buf0_type = CIFS_NO_BUFFER;
1390 sess_free_buffer(struct sess_data *sess_data)
1392 struct kvec *iov = sess_data->iov;
1395 * Zero the session data before freeing, as it might contain sensitive info (keys, etc).
1396 * Note that iov[1] is already freed by caller.
1398 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1399 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
1401 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
1402 sess_data->buf0_type = CIFS_NO_BUFFER;
1403 kfree_sensitive(iov[2].iov_base);
1407 sess_establish_session(struct sess_data *sess_data)
1409 struct cifs_ses *ses = sess_data->ses;
1410 struct TCP_Server_Info *server = sess_data->server;
1412 cifs_server_lock(server);
1413 if (!server->session_estab) {
1415 server->session_key.response =
1416 kmemdup(ses->auth_key.response,
1417 ses->auth_key.len, GFP_KERNEL);
1418 if (!server->session_key.response) {
1419 cifs_server_unlock(server);
1422 server->session_key.len =
1425 server->sequence_number = 0x2;
1426 server->session_estab = true;
1428 cifs_server_unlock(server);
1430 cifs_dbg(FYI, "CIFS session established successfully\n");
1435 sess_sendreceive(struct sess_data *sess_data)
1438 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
1440 struct kvec rsp_iov = { NULL, 0 };
1442 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
1443 be32_add_cpu(&smb_buf->smb_buf_length, count);
1444 put_bcc(count, smb_buf);
1446 rc = SendReceive2(sess_data->xid, sess_data->ses,
1447 sess_data->iov, 3 /* num_iovecs */,
1448 &sess_data->buf0_type,
1449 CIFS_LOG_ERROR, &rsp_iov);
1450 cifs_small_buf_release(sess_data->iov[0].iov_base);
1451 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1457 sess_auth_ntlmv2(struct sess_data *sess_data)
1460 struct smb_hdr *smb_buf;
1461 SESSION_SETUP_ANDX *pSMB;
1463 struct cifs_ses *ses = sess_data->ses;
1464 struct TCP_Server_Info *server = sess_data->server;
1466 __u16 bytes_remaining;
1468 /* old style NTLM sessionsetup */
1470 rc = sess_alloc_buffer(sess_data, 13);
1474 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1475 bcc_ptr = sess_data->iov[2].iov_base;
1476 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1478 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1480 /* LM2 password would be here if we supported it */
1481 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1483 if (ses->user_name != NULL) {
1484 /* calculate nlmv2 response and session key */
1485 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1487 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1491 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1492 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1493 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1495 /* set case sensitive password length after tilen may get
1496 * assigned, tilen is 0 otherwise.
1498 pSMB->req_no_secext.CaseSensitivePasswordLength =
1499 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1501 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1504 if (ses->capabilities & CAP_UNICODE) {
1505 if (!IS_ALIGNED(sess_data->iov[0].iov_len, 2)) {
1509 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1511 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1515 sess_data->iov[2].iov_len = (long) bcc_ptr -
1516 (long) sess_data->iov[2].iov_base;
1518 rc = sess_sendreceive(sess_data);
1522 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1523 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1525 if (smb_buf->WordCount != 3) {
1527 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1531 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1532 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1534 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1535 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1537 bytes_remaining = get_bcc(smb_buf);
1538 bcc_ptr = pByteArea(smb_buf);
1540 /* BB check if Unicode and decode strings */
1541 if (bytes_remaining == 0) {
1542 /* no string area to decode, do nothing */
1543 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1544 /* unicode string area must be word-aligned */
1545 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1549 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1552 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1556 rc = sess_establish_session(sess_data);
1558 sess_data->result = rc;
1559 sess_data->func = NULL;
1560 sess_free_buffer(sess_data);
1561 kfree_sensitive(ses->auth_key.response);
1562 ses->auth_key.response = NULL;
1565 #ifdef CONFIG_CIFS_UPCALL
1567 sess_auth_kerberos(struct sess_data *sess_data)
1570 struct smb_hdr *smb_buf;
1571 SESSION_SETUP_ANDX *pSMB;
1573 struct cifs_ses *ses = sess_data->ses;
1574 struct TCP_Server_Info *server = sess_data->server;
1576 __u16 bytes_remaining;
1577 struct key *spnego_key = NULL;
1578 struct cifs_spnego_msg *msg;
1581 /* extended security */
1583 rc = sess_alloc_buffer(sess_data, 12);
1587 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1588 bcc_ptr = sess_data->iov[2].iov_base;
1589 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1591 spnego_key = cifs_get_spnego_key(ses, server);
1592 if (IS_ERR(spnego_key)) {
1593 rc = PTR_ERR(spnego_key);
1598 msg = spnego_key->payload.data[0];
1600 * check version field to make sure that cifs.upcall is
1601 * sending us a response in an expected form
1603 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1604 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1605 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1607 goto out_put_spnego_key;
1610 kfree_sensitive(ses->auth_key.response);
1611 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1613 if (!ses->auth_key.response) {
1614 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1617 goto out_put_spnego_key;
1619 ses->auth_key.len = msg->sesskey_len;
1621 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1622 capabilities |= CAP_EXTENDED_SECURITY;
1623 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1624 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1625 sess_data->iov[1].iov_len = msg->secblob_len;
1626 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1628 if (ses->capabilities & CAP_UNICODE) {
1629 /* unicode strings must be word aligned */
1630 if (!IS_ALIGNED(sess_data->iov[0].iov_len + sess_data->iov[1].iov_len, 2)) {
1634 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1635 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1637 /* BB: is this right? */
1638 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1641 sess_data->iov[2].iov_len = (long) bcc_ptr -
1642 (long) sess_data->iov[2].iov_base;
1644 rc = sess_sendreceive(sess_data);
1646 goto out_put_spnego_key;
1648 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1649 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1651 if (smb_buf->WordCount != 4) {
1653 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1654 goto out_put_spnego_key;
1657 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1658 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1660 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1661 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1663 bytes_remaining = get_bcc(smb_buf);
1664 bcc_ptr = pByteArea(smb_buf);
1666 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1667 if (blob_len > bytes_remaining) {
1668 cifs_dbg(VFS, "bad security blob length %d\n",
1671 goto out_put_spnego_key;
1673 bcc_ptr += blob_len;
1674 bytes_remaining -= blob_len;
1676 /* BB check if Unicode and decode strings */
1677 if (bytes_remaining == 0) {
1678 /* no string area to decode, do nothing */
1679 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1680 /* unicode string area must be word-aligned */
1681 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1685 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1688 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1692 rc = sess_establish_session(sess_data);
1694 key_invalidate(spnego_key);
1695 key_put(spnego_key);
1697 sess_data->result = rc;
1698 sess_data->func = NULL;
1699 sess_free_buffer(sess_data);
1700 kfree_sensitive(ses->auth_key.response);
1701 ses->auth_key.response = NULL;
1704 #endif /* ! CONFIG_CIFS_UPCALL */
1707 * The required kvec buffers have to be allocated before calling this
1711 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1713 SESSION_SETUP_ANDX *pSMB;
1714 struct cifs_ses *ses = sess_data->ses;
1715 struct TCP_Server_Info *server = sess_data->server;
1719 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1721 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1722 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1723 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1727 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1728 capabilities |= CAP_EXTENDED_SECURITY;
1729 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1731 bcc_ptr = sess_data->iov[2].iov_base;
1732 /* unicode strings must be word aligned */
1733 if (!IS_ALIGNED(sess_data->iov[0].iov_len + sess_data->iov[1].iov_len, 2)) {
1737 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1739 sess_data->iov[2].iov_len = (long) bcc_ptr -
1740 (long) sess_data->iov[2].iov_base;
1746 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1749 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1752 struct smb_hdr *smb_buf;
1753 SESSION_SETUP_ANDX *pSMB;
1754 struct cifs_ses *ses = sess_data->ses;
1755 struct TCP_Server_Info *server = sess_data->server;
1756 __u16 bytes_remaining;
1758 unsigned char *ntlmsspblob = NULL;
1761 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1764 * if memory allocation is successful, caller of this function
1767 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1768 if (!ses->ntlmssp) {
1772 ses->ntlmssp->sesskey_per_smbsess = false;
1775 rc = sess_alloc_buffer(sess_data, 12);
1779 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1781 /* Build security blob before we assemble the request */
1782 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
1783 &blob_len, ses, server,
1786 goto out_free_ntlmsspblob;
1788 sess_data->iov[1].iov_len = blob_len;
1789 sess_data->iov[1].iov_base = ntlmsspblob;
1790 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1792 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1794 goto out_free_ntlmsspblob;
1796 rc = sess_sendreceive(sess_data);
1798 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1799 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1801 /* If true, rc here is expected and not an error */
1802 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1803 smb_buf->Status.CifsError ==
1804 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1808 goto out_free_ntlmsspblob;
1810 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1812 if (smb_buf->WordCount != 4) {
1814 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1815 goto out_free_ntlmsspblob;
1818 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1819 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1821 bytes_remaining = get_bcc(smb_buf);
1822 bcc_ptr = pByteArea(smb_buf);
1824 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1825 if (blob_len > bytes_remaining) {
1826 cifs_dbg(VFS, "bad security blob length %d\n",
1829 goto out_free_ntlmsspblob;
1832 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1834 out_free_ntlmsspblob:
1835 kfree_sensitive(ntlmsspblob);
1837 sess_free_buffer(sess_data);
1840 sess_data->func = sess_auth_rawntlmssp_authenticate;
1844 /* Else error. Cleanup */
1845 kfree_sensitive(ses->auth_key.response);
1846 ses->auth_key.response = NULL;
1847 kfree_sensitive(ses->ntlmssp);
1848 ses->ntlmssp = NULL;
1850 sess_data->func = NULL;
1851 sess_data->result = rc;
1855 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1858 struct smb_hdr *smb_buf;
1859 SESSION_SETUP_ANDX *pSMB;
1860 struct cifs_ses *ses = sess_data->ses;
1861 struct TCP_Server_Info *server = sess_data->server;
1862 __u16 bytes_remaining;
1864 unsigned char *ntlmsspblob = NULL;
1867 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1870 rc = sess_alloc_buffer(sess_data, 12);
1874 /* Build security blob before we assemble the request */
1875 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1876 smb_buf = (struct smb_hdr *)pSMB;
1877 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1878 &blob_len, ses, server,
1881 goto out_free_ntlmsspblob;
1882 sess_data->iov[1].iov_len = blob_len;
1883 sess_data->iov[1].iov_base = ntlmsspblob;
1884 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1886 * Make sure that we tell the server that we are using
1887 * the uid that it just gave us back on the response
1890 smb_buf->Uid = ses->Suid;
1892 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1894 goto out_free_ntlmsspblob;
1896 rc = sess_sendreceive(sess_data);
1898 goto out_free_ntlmsspblob;
1900 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1901 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1902 if (smb_buf->WordCount != 4) {
1904 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1905 goto out_free_ntlmsspblob;
1908 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1909 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1911 if (ses->Suid != smb_buf->Uid) {
1912 ses->Suid = smb_buf->Uid;
1913 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1916 bytes_remaining = get_bcc(smb_buf);
1917 bcc_ptr = pByteArea(smb_buf);
1918 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1919 if (blob_len > bytes_remaining) {
1920 cifs_dbg(VFS, "bad security blob length %d\n",
1923 goto out_free_ntlmsspblob;
1925 bcc_ptr += blob_len;
1926 bytes_remaining -= blob_len;
1929 /* BB check if Unicode and decode strings */
1930 if (bytes_remaining == 0) {
1931 /* no string area to decode, do nothing */
1932 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1933 /* unicode string area must be word-aligned */
1934 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1938 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1941 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1945 out_free_ntlmsspblob:
1946 kfree_sensitive(ntlmsspblob);
1948 sess_free_buffer(sess_data);
1951 rc = sess_establish_session(sess_data);
1954 kfree_sensitive(ses->auth_key.response);
1955 ses->auth_key.response = NULL;
1956 kfree_sensitive(ses->ntlmssp);
1957 ses->ntlmssp = NULL;
1959 sess_data->func = NULL;
1960 sess_data->result = rc;
1963 static int select_sec(struct sess_data *sess_data)
1966 struct cifs_ses *ses = sess_data->ses;
1967 struct TCP_Server_Info *server = sess_data->server;
1969 type = cifs_select_sectype(server, ses->sectype);
1970 cifs_dbg(FYI, "sess setup type %d\n", type);
1971 if (type == Unspecified) {
1972 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1978 sess_data->func = sess_auth_ntlmv2;
1981 #ifdef CONFIG_CIFS_UPCALL
1982 sess_data->func = sess_auth_kerberos;
1985 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1987 #endif /* CONFIG_CIFS_UPCALL */
1989 sess_data->func = sess_auth_rawntlmssp_negotiate;
1992 cifs_dbg(VFS, "secType %d not supported!\n", type);
1999 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
2000 struct TCP_Server_Info *server,
2001 const struct nls_table *nls_cp)
2004 struct sess_data *sess_data;
2007 WARN(1, "%s: ses == NULL!", __func__);
2011 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
2015 sess_data->xid = xid;
2016 sess_data->ses = ses;
2017 sess_data->server = server;
2018 sess_data->buf0_type = CIFS_NO_BUFFER;
2019 sess_data->nls_cp = (struct nls_table *) nls_cp;
2021 rc = select_sec(sess_data);
2025 while (sess_data->func)
2026 sess_data->func(sess_data);
2028 /* Store result before we free sess_data */
2029 rc = sess_data->result;
2032 kfree_sensitive(sess_data);
2035 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */