1 // SPDX-License-Identifier: LGPL-2.1
4 * SMB/CIFS session setup handling routines
6 * Copyright (c) International Business Machines Corp., 2006, 2009
7 * Author(s): Steve French (sfrench@us.ibm.com)
13 #include "cifsproto.h"
14 #include "cifs_unicode.h"
15 #include "cifs_debug.h"
18 #include <linux/utsname.h>
19 #include <linux/slab.h>
20 #include <linux/version.h>
22 #include "cifs_spnego.h"
23 #include "smb2proto.h"
24 #include "fs_context.h"
27 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
28 struct cifs_server_iface *iface);
31 is_server_using_iface(struct TCP_Server_Info *server,
32 struct cifs_server_iface *iface)
34 struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr;
35 struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr;
36 struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr;
37 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr;
39 if (server->dstaddr.ss_family != iface->sockaddr.ss_family)
41 if (server->dstaddr.ss_family == AF_INET) {
42 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
44 } else if (server->dstaddr.ss_family == AF_INET6) {
45 if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
46 sizeof(i6->sin6_addr)) != 0)
49 /* unknown family.. */
55 bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
59 spin_lock(&ses->chan_lock);
60 for (i = 0; i < ses->chan_count; i++) {
61 if (ses->chans[i].iface == iface) {
62 spin_unlock(&ses->chan_lock);
66 spin_unlock(&ses->chan_lock);
70 /* channel helper functions. assumed that chan_lock is held by caller. */
73 cifs_ses_get_chan_index(struct cifs_ses *ses,
74 struct TCP_Server_Info *server)
78 for (i = 0; i < ses->chan_count; i++) {
79 if (ses->chans[i].server == server)
83 /* If we didn't find the channel, it is likely a bug */
85 cifs_dbg(VFS, "unable to get chan index for server: 0x%llx",
92 cifs_chan_set_in_reconnect(struct cifs_ses *ses,
93 struct TCP_Server_Info *server)
95 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
97 ses->chans[chan_index].in_reconnect = true;
101 cifs_chan_clear_in_reconnect(struct cifs_ses *ses,
102 struct TCP_Server_Info *server)
104 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
106 ses->chans[chan_index].in_reconnect = false;
110 cifs_chan_in_reconnect(struct cifs_ses *ses,
111 struct TCP_Server_Info *server)
113 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
115 return CIFS_CHAN_IN_RECONNECT(ses, chan_index);
119 cifs_chan_set_need_reconnect(struct cifs_ses *ses,
120 struct TCP_Server_Info *server)
122 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
124 set_bit(chan_index, &ses->chans_need_reconnect);
125 cifs_dbg(FYI, "Set reconnect bitmask for chan %u; now 0x%lx\n",
126 chan_index, ses->chans_need_reconnect);
130 cifs_chan_clear_need_reconnect(struct cifs_ses *ses,
131 struct TCP_Server_Info *server)
133 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
135 clear_bit(chan_index, &ses->chans_need_reconnect);
136 cifs_dbg(FYI, "Cleared reconnect bitmask for chan %u; now 0x%lx\n",
137 chan_index, ses->chans_need_reconnect);
141 cifs_chan_needs_reconnect(struct cifs_ses *ses,
142 struct TCP_Server_Info *server)
144 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
146 return CIFS_CHAN_NEEDS_RECONNECT(ses, chan_index);
150 cifs_chan_is_iface_active(struct cifs_ses *ses,
151 struct TCP_Server_Info *server)
153 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
155 return ses->chans[chan_index].iface &&
156 ses->chans[chan_index].iface->is_active;
159 /* returns number of channels added */
160 int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
162 struct TCP_Server_Info *server = ses->server;
163 int old_chan_count, new_chan_count;
167 size_t iface_weight = 0, iface_min_speed = 0;
168 struct cifs_server_iface *iface = NULL, *niface = NULL;
169 struct cifs_server_iface *last_iface = NULL;
171 spin_lock(&ses->chan_lock);
173 new_chan_count = old_chan_count = ses->chan_count;
174 left = ses->chan_max - ses->chan_count;
177 spin_unlock(&ses->chan_lock);
179 "ses already at max_channels (%zu), nothing to open\n",
184 if (server->dialect < SMB30_PROT_ID) {
185 spin_unlock(&ses->chan_lock);
186 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
190 if (!(server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
191 spin_unlock(&ses->chan_lock);
192 cifs_server_dbg(VFS, "no multichannel support\n");
195 spin_unlock(&ses->chan_lock);
200 if (tries > 3*ses->chan_max) {
201 cifs_dbg(VFS, "too many channel open attempts (%d channels left to open)\n",
206 spin_lock(&ses->iface_lock);
207 if (!ses->iface_count) {
208 spin_unlock(&ses->iface_lock);
209 cifs_dbg(VFS, "server %s does not advertise interfaces\n",
210 ses->server->hostname);
215 iface = list_first_entry(&ses->iface_list, struct cifs_server_iface,
217 last_iface = list_last_entry(&ses->iface_list, struct cifs_server_iface,
219 iface_min_speed = last_iface->speed;
221 list_for_each_entry_safe_from(iface, niface, &ses->iface_list,
223 /* do not mix rdma and non-rdma interfaces */
224 if (iface->rdma_capable != ses->server->rdma)
227 /* skip ifaces that are unusable */
228 if (!iface->is_active ||
229 (is_ses_using_iface(ses, iface) &&
230 !iface->rss_capable))
233 /* check if we already allocated enough channels */
234 iface_weight = iface->speed / iface_min_speed;
236 if (iface->weight_fulfilled >= iface_weight)
239 /* take ref before unlock */
240 kref_get(&iface->refcount);
242 spin_unlock(&ses->iface_lock);
243 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
244 spin_lock(&ses->iface_lock);
247 cifs_dbg(VFS, "failed to open extra channel on iface:%pIS rc=%d\n",
250 kref_put(&iface->refcount, release_iface);
251 /* failure to add chan should increase weight */
252 iface->weight_fulfilled++;
256 iface->num_channels++;
257 iface->weight_fulfilled++;
258 cifs_dbg(VFS, "successfully opened new channel on iface:%pIS\n",
263 /* reached end of list. reset weight_fulfilled and start over */
264 if (list_entry_is_head(iface, &ses->iface_list, iface_head)) {
265 list_for_each_entry(iface, &ses->iface_list, iface_head)
266 iface->weight_fulfilled = 0;
267 spin_unlock(&ses->iface_lock);
271 spin_unlock(&ses->iface_lock);
277 return new_chan_count - old_chan_count;
281 * update the iface for the channel if necessary.
282 * will return 0 when iface is updated, 1 if removed, 2 otherwise
283 * Must be called with chan_lock held.
286 cifs_chan_update_iface(struct cifs_ses *ses, struct TCP_Server_Info *server)
288 unsigned int chan_index;
289 size_t iface_weight = 0, iface_min_speed = 0;
290 struct cifs_server_iface *iface = NULL;
291 struct cifs_server_iface *old_iface = NULL;
292 struct cifs_server_iface *last_iface = NULL;
293 struct sockaddr_storage ss;
296 spin_lock(&ses->chan_lock);
297 chan_index = cifs_ses_get_chan_index(ses, server);
299 spin_unlock(&ses->chan_lock);
303 if (ses->chans[chan_index].iface) {
304 old_iface = ses->chans[chan_index].iface;
305 if (old_iface->is_active) {
306 spin_unlock(&ses->chan_lock);
310 spin_unlock(&ses->chan_lock);
312 spin_lock(&server->srv_lock);
313 ss = server->dstaddr;
314 spin_unlock(&server->srv_lock);
316 spin_lock(&ses->iface_lock);
317 if (!ses->iface_count) {
318 spin_unlock(&ses->iface_lock);
319 cifs_dbg(VFS, "server %s does not advertise interfaces\n", ses->server->hostname);
323 last_iface = list_last_entry(&ses->iface_list, struct cifs_server_iface,
325 iface_min_speed = last_iface->speed;
327 /* then look for a new one */
328 list_for_each_entry(iface, &ses->iface_list, iface_head) {
330 /* if we're trying to get the updated iface for primary channel */
331 if (!cifs_match_ipaddr((struct sockaddr *) &ss,
332 (struct sockaddr *) &iface->sockaddr))
335 kref_get(&iface->refcount);
339 /* do not mix rdma and non-rdma interfaces */
340 if (iface->rdma_capable != server->rdma)
343 if (!iface->is_active ||
344 (is_ses_using_iface(ses, iface) &&
345 !iface->rss_capable)) {
349 /* check if we already allocated enough channels */
350 iface_weight = iface->speed / iface_min_speed;
352 if (iface->weight_fulfilled >= iface_weight)
355 kref_get(&iface->refcount);
359 if (list_entry_is_head(iface, &ses->iface_list, iface_head)) {
362 cifs_dbg(FYI, "unable to find a suitable iface\n");
365 if (!chan_index && !iface) {
366 cifs_dbg(FYI, "unable to get the interface matching: %pIS\n",
368 spin_unlock(&ses->iface_lock);
372 /* now drop the ref to the current iface */
373 if (old_iface && iface) {
374 cifs_dbg(FYI, "replacing iface: %pIS with %pIS\n",
375 &old_iface->sockaddr,
378 old_iface->num_channels--;
379 if (old_iface->weight_fulfilled)
380 old_iface->weight_fulfilled--;
381 iface->num_channels++;
382 iface->weight_fulfilled++;
384 kref_put(&old_iface->refcount, release_iface);
385 } else if (old_iface) {
386 cifs_dbg(FYI, "releasing ref to iface: %pIS\n",
387 &old_iface->sockaddr);
389 old_iface->num_channels--;
390 if (old_iface->weight_fulfilled)
391 old_iface->weight_fulfilled--;
393 kref_put(&old_iface->refcount, release_iface);
394 } else if (!chan_index) {
395 /* special case: update interface for primary channel */
396 cifs_dbg(FYI, "referencing primary channel iface: %pIS\n",
398 iface->num_channels++;
399 iface->weight_fulfilled++;
402 cifs_dbg(FYI, "adding new iface: %pIS\n", &iface->sockaddr);
404 spin_unlock(&ses->iface_lock);
406 spin_lock(&ses->chan_lock);
407 chan_index = cifs_ses_get_chan_index(ses, server);
408 ses->chans[chan_index].iface = iface;
410 /* No iface is found. if secondary chan, drop connection */
411 if (!iface && CIFS_SERVER_IS_CHAN(server))
412 ses->chans[chan_index].server = NULL;
414 spin_unlock(&ses->chan_lock);
416 if (!iface && CIFS_SERVER_IS_CHAN(server))
417 cifs_put_tcp_session(server, false);
423 * If server is a channel of ses, return the corresponding enclosing
424 * cifs_chan otherwise return NULL.
427 cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
431 spin_lock(&ses->chan_lock);
432 for (i = 0; i < ses->chan_count; i++) {
433 if (ses->chans[i].server == server) {
434 spin_unlock(&ses->chan_lock);
435 return &ses->chans[i];
438 spin_unlock(&ses->chan_lock);
443 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
444 struct cifs_server_iface *iface)
446 struct TCP_Server_Info *chan_server;
447 struct cifs_chan *chan;
448 struct smb3_fs_context ctx = {NULL};
449 static const char unc_fmt[] = "\\%s\\foo";
450 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
451 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
452 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
454 unsigned int xid = get_xid();
456 if (iface->sockaddr.ss_family == AF_INET)
457 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
458 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
461 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
462 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
466 * Setup a ctx with mostly the same info as the existing
467 * session and overwrite it with the requested iface data.
469 * We need to setup at least the fields used for negprot and
472 * We only need the ctx here, so we can reuse memory from
473 * the session and server without caring about memory
477 /* Always make new connection for now (TODO?) */
478 ctx.nosharesock = true;
481 ctx.domainauto = ses->domainAuto;
482 ctx.domainname = ses->domainName;
484 /* no hostname for extra channels */
485 ctx.server_hostname = "";
487 ctx.username = ses->user_name;
488 ctx.password = ses->password;
489 ctx.sectype = ses->sectype;
490 ctx.sign = ses->sign;
493 /* XXX: Use ses->server->hostname? */
494 sprintf(unc, unc_fmt, ses->ip_addr);
498 /* Reuse same version as master connection */
499 ctx.vals = ses->server->vals;
500 ctx.ops = ses->server->ops;
502 ctx.noblocksnd = ses->server->noblocksnd;
503 ctx.noautotune = ses->server->noautotune;
504 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
505 ctx.echo_interval = ses->server->echo_interval / HZ;
506 ctx.max_credits = ses->server->max_credits;
509 * This will be used for encoding/decoding user/domain/pw
510 * during sess setup auth.
512 ctx.local_nls = cifs_sb->local_nls;
514 /* Use RDMA if possible */
515 ctx.rdma = iface->rdma_capable;
516 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
518 /* reuse master con client guid */
519 memcpy(&ctx.client_guid, ses->server->client_guid,
520 SMB2_CLIENT_GUID_SIZE);
521 ctx.use_client_guid = true;
523 chan_server = cifs_get_tcp_session(&ctx, ses->server);
525 spin_lock(&ses->chan_lock);
526 chan = &ses->chans[ses->chan_count];
527 chan->server = chan_server;
528 if (IS_ERR(chan->server)) {
529 rc = PTR_ERR(chan->server);
531 spin_unlock(&ses->chan_lock);
536 atomic_set(&ses->chan_seq, 0);
538 /* Mark this channel as needing connect/setup */
539 cifs_chan_set_need_reconnect(ses, chan->server);
541 spin_unlock(&ses->chan_lock);
543 mutex_lock(&ses->session_mutex);
545 * We need to allocate the server crypto now as we will need
546 * to sign packets before we generate the channel signing key
547 * (we sign with the session key)
549 rc = smb311_crypto_shash_allocate(chan->server);
551 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
552 mutex_unlock(&ses->session_mutex);
556 rc = cifs_negotiate_protocol(xid, ses, chan->server);
558 rc = cifs_setup_session(xid, ses, chan->server, cifs_sb->local_nls);
560 mutex_unlock(&ses->session_mutex);
563 if (rc && chan->server) {
565 * we should avoid race with these delayed works before we
566 * remove this channel
568 cancel_delayed_work_sync(&chan->server->echo);
569 cancel_delayed_work_sync(&chan->server->resolve);
570 cancel_delayed_work_sync(&chan->server->reconnect);
572 spin_lock(&ses->chan_lock);
573 /* we rely on all bits beyond chan_count to be clear */
574 cifs_chan_clear_need_reconnect(ses, chan->server);
577 * chan_count should never reach 0 as at least the primary
578 * channel is always allocated
580 WARN_ON(ses->chan_count < 1);
581 spin_unlock(&ses->chan_lock);
583 cifs_put_tcp_session(chan->server, 0);
590 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
591 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses,
592 struct TCP_Server_Info *server,
593 SESSION_SETUP_ANDX *pSMB)
595 __u32 capabilities = 0;
597 /* init fields common to all four types of SessSetup */
598 /* Note that offsets for first seven fields in req struct are same */
599 /* in CIFS Specs so does not matter which of 3 forms of struct */
600 /* that we use in next few lines */
601 /* Note that header is initialized to zero in header_assemble */
602 pSMB->req.AndXCommand = 0xFF;
603 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
604 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
606 pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq);
607 pSMB->req.VcNumber = cpu_to_le16(1);
609 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
611 /* BB verify whether signing required on neg or just on auth frame
614 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
615 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
618 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
620 if (ses->capabilities & CAP_UNICODE) {
621 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
622 capabilities |= CAP_UNICODE;
624 if (ses->capabilities & CAP_STATUS32) {
625 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
626 capabilities |= CAP_STATUS32;
628 if (ses->capabilities & CAP_DFS) {
629 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
630 capabilities |= CAP_DFS;
632 if (ses->capabilities & CAP_UNIX)
633 capabilities |= CAP_UNIX;
639 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
641 char *bcc_ptr = *pbcc_area;
644 /* Copy OS version */
645 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
647 bcc_ptr += 2 * bytes_ret;
648 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
650 bcc_ptr += 2 * bytes_ret;
651 bcc_ptr += 2; /* trailing null */
653 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
655 bcc_ptr += 2 * bytes_ret;
656 bcc_ptr += 2; /* trailing null */
658 *pbcc_area = bcc_ptr;
661 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
662 const struct nls_table *nls_cp)
664 char *bcc_ptr = *pbcc_area;
668 if (ses->domainName == NULL) {
669 /* Sending null domain better than using a bogus domain name (as
670 we did briefly in 2.6.18) since server will use its default */
675 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
676 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
677 bcc_ptr += 2 * bytes_ret;
678 bcc_ptr += 2; /* account for null terminator */
680 *pbcc_area = bcc_ptr;
683 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
684 const struct nls_table *nls_cp)
686 char *bcc_ptr = *pbcc_area;
689 /* BB FIXME add check that strings total less
690 than 335 or will need to send them as arrays */
693 if (ses->user_name == NULL) {
694 /* null user mount */
698 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
699 CIFS_MAX_USERNAME_LEN, nls_cp);
701 bcc_ptr += 2 * bytes_ret;
702 bcc_ptr += 2; /* account for null termination */
704 unicode_domain_string(&bcc_ptr, ses, nls_cp);
705 unicode_oslm_strings(&bcc_ptr, nls_cp);
707 *pbcc_area = bcc_ptr;
710 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
711 const struct nls_table *nls_cp)
713 char *bcc_ptr = *pbcc_area;
717 /* BB what about null user mounts - check that we do this BB */
719 if (ses->user_name != NULL) {
720 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
721 if (WARN_ON_ONCE(len < 0))
722 len = CIFS_MAX_USERNAME_LEN - 1;
725 /* else null user mount */
727 bcc_ptr++; /* account for null termination */
730 if (ses->domainName != NULL) {
731 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
732 if (WARN_ON_ONCE(len < 0))
733 len = CIFS_MAX_DOMAINNAME_LEN - 1;
735 } /* else we will send a null domain name
736 so the server will default to its own domain */
740 /* BB check for overflow here */
742 strcpy(bcc_ptr, "Linux version ");
743 bcc_ptr += strlen("Linux version ");
744 strcpy(bcc_ptr, init_utsname()->release);
745 bcc_ptr += strlen(init_utsname()->release) + 1;
747 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
748 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
750 *pbcc_area = bcc_ptr;
754 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
755 const struct nls_table *nls_cp)
758 char *data = *pbcc_area;
760 cifs_dbg(FYI, "bleft %d\n", bleft);
762 kfree(ses->serverOS);
763 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
764 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
765 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
771 kfree(ses->serverNOS);
772 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
773 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
774 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
780 kfree(ses->serverDomain);
781 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
782 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
787 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
788 struct cifs_ses *ses,
789 const struct nls_table *nls_cp)
792 char *bcc_ptr = *pbcc_area;
794 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
796 len = strnlen(bcc_ptr, bleft);
800 kfree(ses->serverOS);
802 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
804 memcpy(ses->serverOS, bcc_ptr, len);
805 ses->serverOS[len] = 0;
806 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
807 cifs_dbg(FYI, "OS/2 server\n");
813 len = strnlen(bcc_ptr, bleft);
817 kfree(ses->serverNOS);
819 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
820 if (ses->serverNOS) {
821 memcpy(ses->serverNOS, bcc_ptr, len);
822 ses->serverNOS[len] = 0;
828 len = strnlen(bcc_ptr, bleft);
832 /* No domain field in LANMAN case. Domain is
833 returned by old servers in the SMB negprot response */
834 /* BB For newer servers which do not support Unicode,
835 but thus do return domain here we could add parsing
836 for it later, but it is not very important */
837 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
839 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
841 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
842 struct cifs_ses *ses)
844 unsigned int tioffset; /* challenge message target info area */
845 unsigned int tilen; /* challenge message target info area length */
846 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
849 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
850 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
854 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
855 cifs_dbg(VFS, "blob signature incorrect %s\n",
859 if (pblob->MessageType != NtLmChallenge) {
860 cifs_dbg(VFS, "Incorrect message type %d\n",
865 server_flags = le32_to_cpu(pblob->NegotiateFlags);
866 cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__,
867 ses->ntlmssp->client_flags, server_flags);
869 if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) &&
870 (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) {
871 cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n",
875 if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) {
876 cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__);
879 if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) {
880 cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n",
884 if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
885 !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH))
886 pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n",
889 ses->ntlmssp->server_flags = server_flags;
891 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
892 /* In particular we can examine sign flags */
893 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
894 we must set the MIC field of the AUTHENTICATE_MESSAGE */
896 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
897 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
898 if (tioffset > blob_len || tioffset + tilen > blob_len) {
899 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
904 kfree_sensitive(ses->auth_key.response);
905 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
907 if (!ses->auth_key.response) {
908 cifs_dbg(VFS, "Challenge target info alloc failure\n");
911 ses->auth_key.len = tilen;
917 static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
919 int sz = base_size + ses->auth_key.len
920 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
923 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
925 sz += sizeof(__le16);
928 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
930 sz += sizeof(__le16);
932 if (ses->workstation_name[0])
933 sz += sizeof(__le16) * strnlen(ses->workstation_name,
934 ntlmssp_workstation_name_size(ses));
936 sz += sizeof(__le16);
941 static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
944 unsigned char *pstart,
945 unsigned char **pcur,
946 const struct nls_table *nls_cp)
948 unsigned char *tmp = pstart;
958 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
960 pbuf->MaximumLength = 0;
961 *pcur += sizeof(__le16);
963 len = cifs_strtoUTF16((__le16 *)*pcur,
967 len *= sizeof(__le16);
968 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
969 pbuf->Length = cpu_to_le16(len);
970 pbuf->MaximumLength = cpu_to_le16(len);
975 /* BB Move to ntlmssp.c eventually */
977 int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
979 struct cifs_ses *ses,
980 struct TCP_Server_Info *server,
981 const struct nls_table *nls_cp)
984 NEGOTIATE_MESSAGE *sec_blob;
989 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
990 *pbuffer = kmalloc(len, GFP_KERNEL);
993 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
995 goto setup_ntlm_neg_ret;
997 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
999 memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
1000 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1001 sec_blob->MessageType = NtLmNegotiate;
1003 /* BB is NTLMV2 session security format easier to use here? */
1004 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
1005 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
1006 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
1007 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
1008 NTLMSSP_NEGOTIATE_SIGN;
1009 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
1010 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
1012 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
1013 ses->ntlmssp->client_flags = flags;
1014 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1016 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
1017 cifs_security_buffer_from_str(&sec_blob->DomainName,
1019 CIFS_MAX_DOMAINNAME_LEN,
1023 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1025 CIFS_MAX_WORKSTATION_LEN,
1029 *buflen = tmp - *pbuffer;
1035 * Build ntlmssp blob with additional fields, such as version,
1036 * supported by modern servers. For safety limit to SMB3 or later
1037 * See notes in MS-NLMP Section 2.2.2.1 e.g.
1039 int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer,
1041 struct cifs_ses *ses,
1042 struct TCP_Server_Info *server,
1043 const struct nls_table *nls_cp)
1046 struct negotiate_message *sec_blob;
1051 len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message));
1052 *pbuffer = kmalloc(len, GFP_KERNEL);
1055 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
1057 goto setup_ntlm_smb3_neg_ret;
1059 sec_blob = (struct negotiate_message *)*pbuffer;
1061 memset(*pbuffer, 0, sizeof(struct negotiate_message));
1062 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1063 sec_blob->MessageType = NtLmNegotiate;
1065 /* BB is NTLMV2 session security format easier to use here? */
1066 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
1067 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
1068 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
1069 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
1070 NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION;
1071 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
1072 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
1074 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
1075 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
1076 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
1077 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
1079 tmp = *pbuffer + sizeof(struct negotiate_message);
1080 ses->ntlmssp->client_flags = flags;
1081 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1083 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
1084 cifs_security_buffer_from_str(&sec_blob->DomainName,
1086 CIFS_MAX_DOMAINNAME_LEN,
1090 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1092 CIFS_MAX_WORKSTATION_LEN,
1096 *buflen = tmp - *pbuffer;
1097 setup_ntlm_smb3_neg_ret:
1102 /* See MS-NLMP 2.2.1.3 */
1103 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
1105 struct cifs_ses *ses,
1106 struct TCP_Server_Info *server,
1107 const struct nls_table *nls_cp)
1110 AUTHENTICATE_MESSAGE *sec_blob;
1115 rc = setup_ntlmv2_rsp(ses, nls_cp);
1117 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
1119 goto setup_ntlmv2_ret;
1122 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
1123 *pbuffer = kmalloc(len, GFP_KERNEL);
1126 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
1128 goto setup_ntlmv2_ret;
1130 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
1132 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1133 sec_blob->MessageType = NtLmAuthenticate;
1135 flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET |
1136 NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
1137 /* we only send version information in ntlmssp negotiate, so do not set this flag */
1138 flags = flags & ~NTLMSSP_NEGOTIATE_VERSION;
1139 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
1140 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1142 sec_blob->LmChallengeResponse.BufferOffset =
1143 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
1144 sec_blob->LmChallengeResponse.Length = 0;
1145 sec_blob->LmChallengeResponse.MaximumLength = 0;
1147 sec_blob->NtChallengeResponse.BufferOffset =
1148 cpu_to_le32(tmp - *pbuffer);
1149 if (ses->user_name != NULL) {
1150 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1151 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1152 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1154 sec_blob->NtChallengeResponse.Length =
1155 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1156 sec_blob->NtChallengeResponse.MaximumLength =
1157 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1160 * don't send an NT Response for anonymous access
1162 sec_blob->NtChallengeResponse.Length = 0;
1163 sec_blob->NtChallengeResponse.MaximumLength = 0;
1166 cifs_security_buffer_from_str(&sec_blob->DomainName,
1168 CIFS_MAX_DOMAINNAME_LEN,
1172 cifs_security_buffer_from_str(&sec_blob->UserName,
1174 CIFS_MAX_USERNAME_LEN,
1178 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1179 ses->workstation_name,
1180 ntlmssp_workstation_name_size(ses),
1184 if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
1185 (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) &&
1186 !calc_seckey(ses)) {
1187 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
1188 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1189 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
1190 sec_blob->SessionKey.MaximumLength =
1191 cpu_to_le16(CIFS_CPHTXT_SIZE);
1192 tmp += CIFS_CPHTXT_SIZE;
1194 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1195 sec_blob->SessionKey.Length = 0;
1196 sec_blob->SessionKey.MaximumLength = 0;
1199 *buflen = tmp - *pbuffer;
1205 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1207 switch (server->negflavor) {
1208 case CIFS_NEGFLAVOR_EXTENDED:
1209 switch (requested) {
1214 if (server->sec_ntlmssp &&
1215 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1217 if ((server->sec_kerberos || server->sec_mskerberos) &&
1218 (global_secflags & CIFSSEC_MAY_KRB5))
1224 case CIFS_NEGFLAVOR_UNENCAP:
1225 switch (requested) {
1229 if (global_secflags & CIFSSEC_MAY_NTLMV2)
1243 struct cifs_ses *ses;
1244 struct TCP_Server_Info *server;
1245 struct nls_table *nls_cp;
1246 void (*func)(struct sess_data *);
1249 /* we will send the SMB in three pieces:
1250 * a fixed length beginning part, an optional
1251 * SPNEGO blob (which can be zero length), and a
1252 * last part which will include the strings
1253 * and rest of bcc area. This allows us to avoid
1254 * a large buffer 17K allocation
1260 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1262 sess_alloc_buffer(struct sess_data *sess_data, int wct)
1265 struct cifs_ses *ses = sess_data->ses;
1266 struct smb_hdr *smb_buf;
1268 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
1274 sess_data->iov[0].iov_base = (char *)smb_buf;
1275 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
1277 * This variable will be used to clear the buffer
1278 * allocated above in case of any error in the calling function.
1280 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1282 /* 2000 big enough to fit max user, domain, NOS name etc. */
1283 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
1284 if (!sess_data->iov[2].iov_base) {
1286 goto out_free_smb_buf;
1292 cifs_small_buf_release(smb_buf);
1293 sess_data->iov[0].iov_base = NULL;
1294 sess_data->iov[0].iov_len = 0;
1295 sess_data->buf0_type = CIFS_NO_BUFFER;
1300 sess_free_buffer(struct sess_data *sess_data)
1302 struct kvec *iov = sess_data->iov;
1305 * Zero the session data before freeing, as it might contain sensitive info (keys, etc).
1306 * Note that iov[1] is already freed by caller.
1308 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1309 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
1311 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
1312 sess_data->buf0_type = CIFS_NO_BUFFER;
1313 kfree_sensitive(iov[2].iov_base);
1317 sess_establish_session(struct sess_data *sess_data)
1319 struct cifs_ses *ses = sess_data->ses;
1320 struct TCP_Server_Info *server = sess_data->server;
1322 cifs_server_lock(server);
1323 if (!server->session_estab) {
1325 server->session_key.response =
1326 kmemdup(ses->auth_key.response,
1327 ses->auth_key.len, GFP_KERNEL);
1328 if (!server->session_key.response) {
1329 cifs_server_unlock(server);
1332 server->session_key.len =
1335 server->sequence_number = 0x2;
1336 server->session_estab = true;
1338 cifs_server_unlock(server);
1340 cifs_dbg(FYI, "CIFS session established successfully\n");
1345 sess_sendreceive(struct sess_data *sess_data)
1348 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
1350 struct kvec rsp_iov = { NULL, 0 };
1352 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
1353 be32_add_cpu(&smb_buf->smb_buf_length, count);
1354 put_bcc(count, smb_buf);
1356 rc = SendReceive2(sess_data->xid, sess_data->ses,
1357 sess_data->iov, 3 /* num_iovecs */,
1358 &sess_data->buf0_type,
1359 CIFS_LOG_ERROR, &rsp_iov);
1360 cifs_small_buf_release(sess_data->iov[0].iov_base);
1361 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1367 sess_auth_ntlmv2(struct sess_data *sess_data)
1370 struct smb_hdr *smb_buf;
1371 SESSION_SETUP_ANDX *pSMB;
1373 struct cifs_ses *ses = sess_data->ses;
1374 struct TCP_Server_Info *server = sess_data->server;
1376 __u16 bytes_remaining;
1378 /* old style NTLM sessionsetup */
1380 rc = sess_alloc_buffer(sess_data, 13);
1384 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1385 bcc_ptr = sess_data->iov[2].iov_base;
1386 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1388 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1390 /* LM2 password would be here if we supported it */
1391 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1393 if (ses->user_name != NULL) {
1394 /* calculate nlmv2 response and session key */
1395 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1397 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1401 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1402 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1403 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1405 /* set case sensitive password length after tilen may get
1406 * assigned, tilen is 0 otherwise.
1408 pSMB->req_no_secext.CaseSensitivePasswordLength =
1409 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1411 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1414 if (ses->capabilities & CAP_UNICODE) {
1415 if (!IS_ALIGNED(sess_data->iov[0].iov_len, 2)) {
1419 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1421 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1425 sess_data->iov[2].iov_len = (long) bcc_ptr -
1426 (long) sess_data->iov[2].iov_base;
1428 rc = sess_sendreceive(sess_data);
1432 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1433 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1435 if (smb_buf->WordCount != 3) {
1437 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1441 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1442 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1444 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1445 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1447 bytes_remaining = get_bcc(smb_buf);
1448 bcc_ptr = pByteArea(smb_buf);
1450 /* BB check if Unicode and decode strings */
1451 if (bytes_remaining == 0) {
1452 /* no string area to decode, do nothing */
1453 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1454 /* unicode string area must be word-aligned */
1455 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1459 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1462 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1466 rc = sess_establish_session(sess_data);
1468 sess_data->result = rc;
1469 sess_data->func = NULL;
1470 sess_free_buffer(sess_data);
1471 kfree_sensitive(ses->auth_key.response);
1472 ses->auth_key.response = NULL;
1475 #ifdef CONFIG_CIFS_UPCALL
1477 sess_auth_kerberos(struct sess_data *sess_data)
1480 struct smb_hdr *smb_buf;
1481 SESSION_SETUP_ANDX *pSMB;
1483 struct cifs_ses *ses = sess_data->ses;
1484 struct TCP_Server_Info *server = sess_data->server;
1486 __u16 bytes_remaining;
1487 struct key *spnego_key = NULL;
1488 struct cifs_spnego_msg *msg;
1491 /* extended security */
1493 rc = sess_alloc_buffer(sess_data, 12);
1497 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1498 bcc_ptr = sess_data->iov[2].iov_base;
1499 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1501 spnego_key = cifs_get_spnego_key(ses, server);
1502 if (IS_ERR(spnego_key)) {
1503 rc = PTR_ERR(spnego_key);
1508 msg = spnego_key->payload.data[0];
1510 * check version field to make sure that cifs.upcall is
1511 * sending us a response in an expected form
1513 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1514 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1515 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1517 goto out_put_spnego_key;
1520 kfree_sensitive(ses->auth_key.response);
1521 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1523 if (!ses->auth_key.response) {
1524 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1527 goto out_put_spnego_key;
1529 ses->auth_key.len = msg->sesskey_len;
1531 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1532 capabilities |= CAP_EXTENDED_SECURITY;
1533 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1534 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1535 sess_data->iov[1].iov_len = msg->secblob_len;
1536 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1538 if (ses->capabilities & CAP_UNICODE) {
1539 /* unicode strings must be word aligned */
1540 if (!IS_ALIGNED(sess_data->iov[0].iov_len + sess_data->iov[1].iov_len, 2)) {
1544 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1545 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1547 /* BB: is this right? */
1548 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1551 sess_data->iov[2].iov_len = (long) bcc_ptr -
1552 (long) sess_data->iov[2].iov_base;
1554 rc = sess_sendreceive(sess_data);
1556 goto out_put_spnego_key;
1558 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1559 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1561 if (smb_buf->WordCount != 4) {
1563 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1564 goto out_put_spnego_key;
1567 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1568 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1570 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1571 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1573 bytes_remaining = get_bcc(smb_buf);
1574 bcc_ptr = pByteArea(smb_buf);
1576 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1577 if (blob_len > bytes_remaining) {
1578 cifs_dbg(VFS, "bad security blob length %d\n",
1581 goto out_put_spnego_key;
1583 bcc_ptr += blob_len;
1584 bytes_remaining -= blob_len;
1586 /* BB check if Unicode and decode strings */
1587 if (bytes_remaining == 0) {
1588 /* no string area to decode, do nothing */
1589 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1590 /* unicode string area must be word-aligned */
1591 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1595 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1598 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1602 rc = sess_establish_session(sess_data);
1604 key_invalidate(spnego_key);
1605 key_put(spnego_key);
1607 sess_data->result = rc;
1608 sess_data->func = NULL;
1609 sess_free_buffer(sess_data);
1610 kfree_sensitive(ses->auth_key.response);
1611 ses->auth_key.response = NULL;
1614 #endif /* ! CONFIG_CIFS_UPCALL */
1617 * The required kvec buffers have to be allocated before calling this
1621 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1623 SESSION_SETUP_ANDX *pSMB;
1624 struct cifs_ses *ses = sess_data->ses;
1625 struct TCP_Server_Info *server = sess_data->server;
1629 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1631 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1632 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1633 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1637 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1638 capabilities |= CAP_EXTENDED_SECURITY;
1639 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1641 bcc_ptr = sess_data->iov[2].iov_base;
1642 /* unicode strings must be word aligned */
1643 if (!IS_ALIGNED(sess_data->iov[0].iov_len + sess_data->iov[1].iov_len, 2)) {
1647 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1649 sess_data->iov[2].iov_len = (long) bcc_ptr -
1650 (long) sess_data->iov[2].iov_base;
1656 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1659 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1662 struct smb_hdr *smb_buf;
1663 SESSION_SETUP_ANDX *pSMB;
1664 struct cifs_ses *ses = sess_data->ses;
1665 struct TCP_Server_Info *server = sess_data->server;
1666 __u16 bytes_remaining;
1668 unsigned char *ntlmsspblob = NULL;
1671 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1674 * if memory allocation is successful, caller of this function
1677 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1678 if (!ses->ntlmssp) {
1682 ses->ntlmssp->sesskey_per_smbsess = false;
1685 rc = sess_alloc_buffer(sess_data, 12);
1689 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1691 /* Build security blob before we assemble the request */
1692 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
1693 &blob_len, ses, server,
1696 goto out_free_ntlmsspblob;
1698 sess_data->iov[1].iov_len = blob_len;
1699 sess_data->iov[1].iov_base = ntlmsspblob;
1700 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1702 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1704 goto out_free_ntlmsspblob;
1706 rc = sess_sendreceive(sess_data);
1708 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1709 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1711 /* If true, rc here is expected and not an error */
1712 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1713 smb_buf->Status.CifsError ==
1714 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1718 goto out_free_ntlmsspblob;
1720 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1722 if (smb_buf->WordCount != 4) {
1724 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1725 goto out_free_ntlmsspblob;
1728 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1729 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1731 bytes_remaining = get_bcc(smb_buf);
1732 bcc_ptr = pByteArea(smb_buf);
1734 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1735 if (blob_len > bytes_remaining) {
1736 cifs_dbg(VFS, "bad security blob length %d\n",
1739 goto out_free_ntlmsspblob;
1742 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1744 out_free_ntlmsspblob:
1745 kfree_sensitive(ntlmsspblob);
1747 sess_free_buffer(sess_data);
1750 sess_data->func = sess_auth_rawntlmssp_authenticate;
1754 /* Else error. Cleanup */
1755 kfree_sensitive(ses->auth_key.response);
1756 ses->auth_key.response = NULL;
1757 kfree_sensitive(ses->ntlmssp);
1758 ses->ntlmssp = NULL;
1760 sess_data->func = NULL;
1761 sess_data->result = rc;
1765 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1768 struct smb_hdr *smb_buf;
1769 SESSION_SETUP_ANDX *pSMB;
1770 struct cifs_ses *ses = sess_data->ses;
1771 struct TCP_Server_Info *server = sess_data->server;
1772 __u16 bytes_remaining;
1774 unsigned char *ntlmsspblob = NULL;
1777 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1780 rc = sess_alloc_buffer(sess_data, 12);
1784 /* Build security blob before we assemble the request */
1785 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1786 smb_buf = (struct smb_hdr *)pSMB;
1787 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1788 &blob_len, ses, server,
1791 goto out_free_ntlmsspblob;
1792 sess_data->iov[1].iov_len = blob_len;
1793 sess_data->iov[1].iov_base = ntlmsspblob;
1794 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1796 * Make sure that we tell the server that we are using
1797 * the uid that it just gave us back on the response
1800 smb_buf->Uid = ses->Suid;
1802 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1804 goto out_free_ntlmsspblob;
1806 rc = sess_sendreceive(sess_data);
1808 goto out_free_ntlmsspblob;
1810 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1811 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1812 if (smb_buf->WordCount != 4) {
1814 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1815 goto out_free_ntlmsspblob;
1818 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1819 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1821 if (ses->Suid != smb_buf->Uid) {
1822 ses->Suid = smb_buf->Uid;
1823 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1826 bytes_remaining = get_bcc(smb_buf);
1827 bcc_ptr = pByteArea(smb_buf);
1828 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1829 if (blob_len > bytes_remaining) {
1830 cifs_dbg(VFS, "bad security blob length %d\n",
1833 goto out_free_ntlmsspblob;
1835 bcc_ptr += blob_len;
1836 bytes_remaining -= blob_len;
1839 /* BB check if Unicode and decode strings */
1840 if (bytes_remaining == 0) {
1841 /* no string area to decode, do nothing */
1842 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1843 /* unicode string area must be word-aligned */
1844 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1848 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1851 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1855 out_free_ntlmsspblob:
1856 kfree_sensitive(ntlmsspblob);
1858 sess_free_buffer(sess_data);
1861 rc = sess_establish_session(sess_data);
1864 kfree_sensitive(ses->auth_key.response);
1865 ses->auth_key.response = NULL;
1866 kfree_sensitive(ses->ntlmssp);
1867 ses->ntlmssp = NULL;
1869 sess_data->func = NULL;
1870 sess_data->result = rc;
1873 static int select_sec(struct sess_data *sess_data)
1876 struct cifs_ses *ses = sess_data->ses;
1877 struct TCP_Server_Info *server = sess_data->server;
1879 type = cifs_select_sectype(server, ses->sectype);
1880 cifs_dbg(FYI, "sess setup type %d\n", type);
1881 if (type == Unspecified) {
1882 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1888 sess_data->func = sess_auth_ntlmv2;
1891 #ifdef CONFIG_CIFS_UPCALL
1892 sess_data->func = sess_auth_kerberos;
1895 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1897 #endif /* CONFIG_CIFS_UPCALL */
1899 sess_data->func = sess_auth_rawntlmssp_negotiate;
1902 cifs_dbg(VFS, "secType %d not supported!\n", type);
1909 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1910 struct TCP_Server_Info *server,
1911 const struct nls_table *nls_cp)
1914 struct sess_data *sess_data;
1917 WARN(1, "%s: ses == NULL!", __func__);
1921 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1925 sess_data->xid = xid;
1926 sess_data->ses = ses;
1927 sess_data->server = server;
1928 sess_data->buf0_type = CIFS_NO_BUFFER;
1929 sess_data->nls_cp = (struct nls_table *) nls_cp;
1931 rc = select_sec(sess_data);
1935 while (sess_data->func)
1936 sess_data->func(sess_data);
1938 /* Store result before we free sess_data */
1939 rc = sess_data->result;
1942 kfree_sensitive(sess_data);
1945 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */