1 // SPDX-License-Identifier: LGPL-2.1
5 * SMB/CIFS session setup handling routines
7 * Copyright (c) International Business Machines Corp., 2006, 2009
14 #include "cifsproto.h"
15 #include "cifs_unicode.h"
16 #include "cifs_debug.h"
19 #include <linux/utsname.h>
20 #include <linux/slab.h>
21 #include "cifs_spnego.h"
22 #include "smb2proto.h"
23 #include "fs_context.h"
26 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
27 struct cifs_server_iface *iface);
30 is_server_using_iface(struct TCP_Server_Info *server,
31 struct cifs_server_iface *iface)
33 struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr;
34 struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr;
35 struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr;
36 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr;
38 if (server->dstaddr.ss_family != iface->sockaddr.ss_family)
40 if (server->dstaddr.ss_family == AF_INET) {
41 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
43 } else if (server->dstaddr.ss_family == AF_INET6) {
44 if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
45 sizeof(i6->sin6_addr)) != 0)
48 /* unknown family.. */
54 bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
58 for (i = 0; i < ses->chan_count; i++) {
59 if (is_server_using_iface(ses->chans[i].server, iface))
65 /* returns number of channels added */
66 int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
68 int old_chan_count = ses->chan_count;
69 int left = ses->chan_max - ses->chan_count;
73 struct cifs_server_iface *ifaces = NULL;
78 "ses already at max_channels (%zu), nothing to open\n",
83 if (ses->server->dialect < SMB30_PROT_ID) {
84 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
88 if (!(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
89 cifs_dbg(VFS, "server %s does not support multichannel\n", ses->server->hostname);
95 * Make a copy of the iface list at the time and use that
96 * instead so as to not hold the iface spinlock for opening
99 spin_lock(&ses->iface_lock);
100 iface_count = ses->iface_count;
101 if (iface_count <= 0) {
102 spin_unlock(&ses->iface_lock);
103 cifs_dbg(VFS, "no iface list available to open channels\n");
106 ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces),
109 spin_unlock(&ses->iface_lock);
112 spin_unlock(&ses->iface_lock);
115 * Keep connecting to same, fastest, iface for all channels as
116 * long as its RSS. Try next fastest one if not RSS or channel
120 struct cifs_server_iface *iface;
123 if (tries > 3*ses->chan_max) {
124 cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
130 if (is_ses_using_iface(ses, iface) && !iface->rss_capable) {
131 i = (i+1) % iface_count;
135 rc = cifs_ses_add_channel(cifs_sb, ses, iface);
137 cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n",
139 i = (i+1) % iface_count;
143 cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
149 return ses->chan_count - old_chan_count;
153 * If server is a channel of ses, return the corresponding enclosing
154 * cifs_chan otherwise return NULL.
157 cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server)
161 for (i = 0; i < ses->chan_count; i++) {
162 if (ses->chans[i].server == server)
163 return &ses->chans[i];
169 cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
170 struct cifs_server_iface *iface)
172 struct cifs_chan *chan;
173 struct smb3_fs_context ctx = {NULL};
174 static const char unc_fmt[] = "\\%s\\foo";
175 char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
176 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
177 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
179 unsigned int xid = get_xid();
181 if (iface->sockaddr.ss_family == AF_INET)
182 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
183 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
186 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
187 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
191 * Setup a ctx with mostly the same info as the existing
192 * session and overwrite it with the requested iface data.
194 * We need to setup at least the fields used for negprot and
197 * We only need the ctx here, so we can reuse memory from
198 * the session and server without caring about memory
202 /* Always make new connection for now (TODO?) */
203 ctx.nosharesock = true;
206 ctx.domainauto = ses->domainAuto;
207 ctx.domainname = ses->domainName;
208 ctx.username = ses->user_name;
209 ctx.password = ses->password;
210 ctx.sectype = ses->sectype;
211 ctx.sign = ses->sign;
214 /* XXX: Use ses->server->hostname? */
215 sprintf(unc, unc_fmt, ses->ip_addr);
219 /* Reuse same version as master connection */
220 ctx.vals = ses->server->vals;
221 ctx.ops = ses->server->ops;
223 ctx.noblocksnd = ses->server->noblocksnd;
224 ctx.noautotune = ses->server->noautotune;
225 ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
226 ctx.echo_interval = ses->server->echo_interval / HZ;
227 ctx.max_credits = ses->server->max_credits;
230 * This will be used for encoding/decoding user/domain/pw
231 * during sess setup auth.
233 ctx.local_nls = cifs_sb->local_nls;
235 /* Use RDMA if possible */
236 ctx.rdma = iface->rdma_capable;
237 memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
239 /* reuse master con client guid */
240 memcpy(&ctx.client_guid, ses->server->client_guid,
241 SMB2_CLIENT_GUID_SIZE);
242 ctx.use_client_guid = true;
244 mutex_lock(&ses->session_mutex);
246 chan = ses->binding_chan = &ses->chans[ses->chan_count];
247 chan->server = cifs_get_tcp_session(&ctx);
248 if (IS_ERR(chan->server)) {
249 rc = PTR_ERR(chan->server);
253 spin_lock(&cifs_tcp_ses_lock);
254 chan->server->is_channel = true;
255 spin_unlock(&cifs_tcp_ses_lock);
258 * We need to allocate the server crypto now as we will need
259 * to sign packets before we generate the channel signing key
260 * (we sign with the session key)
262 rc = smb311_crypto_shash_allocate(chan->server);
264 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
269 rc = cifs_negotiate_protocol(xid, ses);
273 rc = cifs_setup_session(xid, ses, cifs_sb->local_nls);
277 /* success, put it on the list
278 * XXX: sharing ses between 2 tcp servers is not possible, the
279 * way "internal" linked lists works in linux makes element
280 * only able to belong to one list
282 * the binding session is already established so the rest of
283 * the code should be able to look it up, no need to add the
284 * ses to the new server.
288 atomic_set(&ses->chan_seq, 0);
290 ses->binding = false;
291 ses->binding_chan = NULL;
292 mutex_unlock(&ses->session_mutex);
294 if (rc && chan->server)
295 cifs_put_tcp_session(chan->server, 0);
300 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses, SESSION_SETUP_ANDX *pSMB)
302 __u32 capabilities = 0;
304 /* init fields common to all four types of SessSetup */
305 /* Note that offsets for first seven fields in req struct are same */
306 /* in CIFS Specs so does not matter which of 3 forms of struct */
307 /* that we use in next few lines */
308 /* Note that header is initialized to zero in header_assemble */
309 pSMB->req.AndXCommand = 0xFF;
310 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
311 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
313 pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
314 pSMB->req.VcNumber = cpu_to_le16(1);
316 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
318 /* BB verify whether signing required on neg or just on auth frame
321 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
322 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
324 if (ses->server->sign)
325 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
327 if (ses->capabilities & CAP_UNICODE) {
328 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
329 capabilities |= CAP_UNICODE;
331 if (ses->capabilities & CAP_STATUS32) {
332 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
333 capabilities |= CAP_STATUS32;
335 if (ses->capabilities & CAP_DFS) {
336 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
337 capabilities |= CAP_DFS;
339 if (ses->capabilities & CAP_UNIX)
340 capabilities |= CAP_UNIX;
346 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
348 char *bcc_ptr = *pbcc_area;
351 /* Copy OS version */
352 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
354 bcc_ptr += 2 * bytes_ret;
355 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
357 bcc_ptr += 2 * bytes_ret;
358 bcc_ptr += 2; /* trailing null */
360 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
362 bcc_ptr += 2 * bytes_ret;
363 bcc_ptr += 2; /* trailing null */
365 *pbcc_area = bcc_ptr;
368 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
369 const struct nls_table *nls_cp)
371 char *bcc_ptr = *pbcc_area;
375 if (ses->domainName == NULL) {
376 /* Sending null domain better than using a bogus domain name (as
377 we did briefly in 2.6.18) since server will use its default */
382 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
383 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
384 bcc_ptr += 2 * bytes_ret;
385 bcc_ptr += 2; /* account for null terminator */
387 *pbcc_area = bcc_ptr;
391 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
392 const struct nls_table *nls_cp)
394 char *bcc_ptr = *pbcc_area;
397 /* BB FIXME add check that strings total less
398 than 335 or will need to send them as arrays */
400 /* unicode strings, must be word aligned before the call */
401 /* if ((long) bcc_ptr % 2) {
406 if (ses->user_name == NULL) {
407 /* null user mount */
411 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
412 CIFS_MAX_USERNAME_LEN, nls_cp);
414 bcc_ptr += 2 * bytes_ret;
415 bcc_ptr += 2; /* account for null termination */
417 unicode_domain_string(&bcc_ptr, ses, nls_cp);
418 unicode_oslm_strings(&bcc_ptr, nls_cp);
420 *pbcc_area = bcc_ptr;
423 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
424 const struct nls_table *nls_cp)
426 char *bcc_ptr = *pbcc_area;
430 /* BB what about null user mounts - check that we do this BB */
432 if (ses->user_name != NULL) {
433 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
434 if (WARN_ON_ONCE(len < 0))
435 len = CIFS_MAX_USERNAME_LEN - 1;
438 /* else null user mount */
440 bcc_ptr++; /* account for null termination */
443 if (ses->domainName != NULL) {
444 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
445 if (WARN_ON_ONCE(len < 0))
446 len = CIFS_MAX_DOMAINNAME_LEN - 1;
448 } /* else we will send a null domain name
449 so the server will default to its own domain */
453 /* BB check for overflow here */
455 strcpy(bcc_ptr, "Linux version ");
456 bcc_ptr += strlen("Linux version ");
457 strcpy(bcc_ptr, init_utsname()->release);
458 bcc_ptr += strlen(init_utsname()->release) + 1;
460 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
461 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
463 *pbcc_area = bcc_ptr;
467 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
468 const struct nls_table *nls_cp)
471 char *data = *pbcc_area;
473 cifs_dbg(FYI, "bleft %d\n", bleft);
475 kfree(ses->serverOS);
476 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
477 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
478 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
484 kfree(ses->serverNOS);
485 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
486 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
487 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
493 kfree(ses->serverDomain);
494 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
495 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
500 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
501 struct cifs_ses *ses,
502 const struct nls_table *nls_cp)
505 char *bcc_ptr = *pbcc_area;
507 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
509 len = strnlen(bcc_ptr, bleft);
513 kfree(ses->serverOS);
515 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
517 memcpy(ses->serverOS, bcc_ptr, len);
518 ses->serverOS[len] = 0;
519 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
520 cifs_dbg(FYI, "OS/2 server\n");
526 len = strnlen(bcc_ptr, bleft);
530 kfree(ses->serverNOS);
532 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
533 if (ses->serverNOS) {
534 memcpy(ses->serverNOS, bcc_ptr, len);
535 ses->serverNOS[len] = 0;
541 len = strnlen(bcc_ptr, bleft);
545 /* No domain field in LANMAN case. Domain is
546 returned by old servers in the SMB negprot response */
547 /* BB For newer servers which do not support Unicode,
548 but thus do return domain here we could add parsing
549 for it later, but it is not very important */
550 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
553 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
554 struct cifs_ses *ses)
556 unsigned int tioffset; /* challenge message target info area */
557 unsigned int tilen; /* challenge message target info area length */
559 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
561 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
562 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
566 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
567 cifs_dbg(VFS, "blob signature incorrect %s\n",
571 if (pblob->MessageType != NtLmChallenge) {
572 cifs_dbg(VFS, "Incorrect message type %d\n",
577 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
578 /* BB we could decode pblob->NegotiateFlags; some may be useful */
579 /* In particular we can examine sign flags */
580 /* BB spec says that if AvId field of MsvAvTimestamp is populated then
581 we must set the MIC field of the AUTHENTICATE_MESSAGE */
582 ses->ntlmssp->server_flags = le32_to_cpu(pblob->NegotiateFlags);
583 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
584 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
585 if (tioffset > blob_len || tioffset + tilen > blob_len) {
586 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
591 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
593 if (!ses->auth_key.response) {
594 cifs_dbg(VFS, "Challenge target info alloc failure\n");
597 ses->auth_key.len = tilen;
603 /* BB Move to ntlmssp.c eventually */
605 /* We do not malloc the blob, it is passed in pbuffer, because
606 it is fixed size, and small, making this approach cleaner */
607 void build_ntlmssp_negotiate_blob(unsigned char *pbuffer,
608 struct cifs_ses *ses)
610 struct TCP_Server_Info *server = cifs_ses_server(ses);
611 NEGOTIATE_MESSAGE *sec_blob = (NEGOTIATE_MESSAGE *)pbuffer;
614 memset(pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
615 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
616 sec_blob->MessageType = NtLmNegotiate;
618 /* BB is NTLMV2 session security format easier to use here? */
619 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
620 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
621 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
622 NTLMSSP_NEGOTIATE_SEAL;
624 flags |= NTLMSSP_NEGOTIATE_SIGN;
625 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
626 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
628 sec_blob->NegotiateFlags = cpu_to_le32(flags);
630 sec_blob->WorkstationName.BufferOffset = 0;
631 sec_blob->WorkstationName.Length = 0;
632 sec_blob->WorkstationName.MaximumLength = 0;
634 /* Domain name is sent on the Challenge not Negotiate NTLMSSP request */
635 sec_blob->DomainName.BufferOffset = 0;
636 sec_blob->DomainName.Length = 0;
637 sec_blob->DomainName.MaximumLength = 0;
640 static int size_of_ntlmssp_blob(struct cifs_ses *ses)
642 int sz = sizeof(AUTHENTICATE_MESSAGE) + ses->auth_key.len
643 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
646 sz += 2 * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
651 sz += 2 * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
658 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
660 struct cifs_ses *ses,
661 const struct nls_table *nls_cp)
664 AUTHENTICATE_MESSAGE *sec_blob;
668 rc = setup_ntlmv2_rsp(ses, nls_cp);
670 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
672 goto setup_ntlmv2_ret;
674 *pbuffer = kmalloc(size_of_ntlmssp_blob(ses), GFP_KERNEL);
677 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
679 goto setup_ntlmv2_ret;
681 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
683 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
684 sec_blob->MessageType = NtLmAuthenticate;
686 flags = NTLMSSP_NEGOTIATE_56 |
687 NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_TARGET_INFO |
688 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
689 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
690 NTLMSSP_NEGOTIATE_SEAL;
691 if (ses->server->sign)
692 flags |= NTLMSSP_NEGOTIATE_SIGN;
693 if (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
694 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
696 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
697 sec_blob->NegotiateFlags = cpu_to_le32(flags);
699 sec_blob->LmChallengeResponse.BufferOffset =
700 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
701 sec_blob->LmChallengeResponse.Length = 0;
702 sec_blob->LmChallengeResponse.MaximumLength = 0;
704 sec_blob->NtChallengeResponse.BufferOffset =
705 cpu_to_le32(tmp - *pbuffer);
706 if (ses->user_name != NULL) {
707 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
708 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
709 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
711 sec_blob->NtChallengeResponse.Length =
712 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
713 sec_blob->NtChallengeResponse.MaximumLength =
714 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
717 * don't send an NT Response for anonymous access
719 sec_blob->NtChallengeResponse.Length = 0;
720 sec_blob->NtChallengeResponse.MaximumLength = 0;
723 if (ses->domainName == NULL) {
724 sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
725 sec_blob->DomainName.Length = 0;
726 sec_blob->DomainName.MaximumLength = 0;
730 len = cifs_strtoUTF16((__le16 *)tmp, ses->domainName,
731 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
732 len *= 2; /* unicode is 2 bytes each */
733 sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
734 sec_blob->DomainName.Length = cpu_to_le16(len);
735 sec_blob->DomainName.MaximumLength = cpu_to_le16(len);
739 if (ses->user_name == NULL) {
740 sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
741 sec_blob->UserName.Length = 0;
742 sec_blob->UserName.MaximumLength = 0;
746 len = cifs_strtoUTF16((__le16 *)tmp, ses->user_name,
747 CIFS_MAX_USERNAME_LEN, nls_cp);
748 len *= 2; /* unicode is 2 bytes each */
749 sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
750 sec_blob->UserName.Length = cpu_to_le16(len);
751 sec_blob->UserName.MaximumLength = cpu_to_le16(len);
755 sec_blob->WorkstationName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
756 sec_blob->WorkstationName.Length = 0;
757 sec_blob->WorkstationName.MaximumLength = 0;
760 if (((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) ||
761 (ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC))
762 && !calc_seckey(ses)) {
763 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
764 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
765 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
766 sec_blob->SessionKey.MaximumLength =
767 cpu_to_le16(CIFS_CPHTXT_SIZE);
768 tmp += CIFS_CPHTXT_SIZE;
770 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
771 sec_blob->SessionKey.Length = 0;
772 sec_blob->SessionKey.MaximumLength = 0;
775 *buflen = tmp - *pbuffer;
781 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
783 switch (server->negflavor) {
784 case CIFS_NEGFLAVOR_EXTENDED:
790 if (server->sec_ntlmssp &&
791 (global_secflags & CIFSSEC_MAY_NTLMSSP))
793 if ((server->sec_kerberos || server->sec_mskerberos) &&
794 (global_secflags & CIFSSEC_MAY_KRB5))
800 case CIFS_NEGFLAVOR_UNENCAP:
805 if (global_secflags & CIFSSEC_MAY_NTLMV2)
819 struct cifs_ses *ses;
820 struct nls_table *nls_cp;
821 void (*func)(struct sess_data *);
824 /* we will send the SMB in three pieces:
825 * a fixed length beginning part, an optional
826 * SPNEGO blob (which can be zero length), and a
827 * last part which will include the strings
828 * and rest of bcc area. This allows us to avoid
829 * a large buffer 17K allocation
836 sess_alloc_buffer(struct sess_data *sess_data, int wct)
839 struct cifs_ses *ses = sess_data->ses;
840 struct smb_hdr *smb_buf;
842 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
848 sess_data->iov[0].iov_base = (char *)smb_buf;
849 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
851 * This variable will be used to clear the buffer
852 * allocated above in case of any error in the calling function.
854 sess_data->buf0_type = CIFS_SMALL_BUFFER;
856 /* 2000 big enough to fit max user, domain, NOS name etc. */
857 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
858 if (!sess_data->iov[2].iov_base) {
860 goto out_free_smb_buf;
866 cifs_small_buf_release(smb_buf);
867 sess_data->iov[0].iov_base = NULL;
868 sess_data->iov[0].iov_len = 0;
869 sess_data->buf0_type = CIFS_NO_BUFFER;
874 sess_free_buffer(struct sess_data *sess_data)
877 free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
878 sess_data->buf0_type = CIFS_NO_BUFFER;
879 kfree(sess_data->iov[2].iov_base);
883 sess_establish_session(struct sess_data *sess_data)
885 struct cifs_ses *ses = sess_data->ses;
887 mutex_lock(&ses->server->srv_mutex);
888 if (!ses->server->session_estab) {
889 if (ses->server->sign) {
890 ses->server->session_key.response =
891 kmemdup(ses->auth_key.response,
892 ses->auth_key.len, GFP_KERNEL);
893 if (!ses->server->session_key.response) {
894 mutex_unlock(&ses->server->srv_mutex);
897 ses->server->session_key.len =
900 ses->server->sequence_number = 0x2;
901 ses->server->session_estab = true;
903 mutex_unlock(&ses->server->srv_mutex);
905 cifs_dbg(FYI, "CIFS session established successfully\n");
906 spin_lock(&GlobalMid_Lock);
907 ses->status = CifsGood;
908 ses->need_reconnect = false;
909 spin_unlock(&GlobalMid_Lock);
915 sess_sendreceive(struct sess_data *sess_data)
918 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
920 struct kvec rsp_iov = { NULL, 0 };
922 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
923 be32_add_cpu(&smb_buf->smb_buf_length, count);
924 put_bcc(count, smb_buf);
926 rc = SendReceive2(sess_data->xid, sess_data->ses,
927 sess_data->iov, 3 /* num_iovecs */,
928 &sess_data->buf0_type,
929 CIFS_LOG_ERROR, &rsp_iov);
930 cifs_small_buf_release(sess_data->iov[0].iov_base);
931 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
937 sess_auth_ntlmv2(struct sess_data *sess_data)
940 struct smb_hdr *smb_buf;
941 SESSION_SETUP_ANDX *pSMB;
943 struct cifs_ses *ses = sess_data->ses;
945 __u16 bytes_remaining;
947 /* old style NTLM sessionsetup */
949 rc = sess_alloc_buffer(sess_data, 13);
953 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
954 bcc_ptr = sess_data->iov[2].iov_base;
955 capabilities = cifs_ssetup_hdr(ses, pSMB);
957 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
959 /* LM2 password would be here if we supported it */
960 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
962 if (ses->user_name != NULL) {
963 /* calculate nlmv2 response and session key */
964 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
966 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
970 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
971 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
972 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
974 /* set case sensitive password length after tilen may get
975 * assigned, tilen is 0 otherwise.
977 pSMB->req_no_secext.CaseSensitivePasswordLength =
978 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
980 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
983 if (ses->capabilities & CAP_UNICODE) {
984 if (sess_data->iov[0].iov_len % 2) {
988 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
990 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
994 sess_data->iov[2].iov_len = (long) bcc_ptr -
995 (long) sess_data->iov[2].iov_base;
997 rc = sess_sendreceive(sess_data);
1001 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1002 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1004 if (smb_buf->WordCount != 3) {
1006 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1010 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1011 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1013 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1014 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1016 bytes_remaining = get_bcc(smb_buf);
1017 bcc_ptr = pByteArea(smb_buf);
1019 /* BB check if Unicode and decode strings */
1020 if (bytes_remaining == 0) {
1021 /* no string area to decode, do nothing */
1022 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1023 /* unicode string area must be word-aligned */
1024 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1028 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1031 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1035 rc = sess_establish_session(sess_data);
1037 sess_data->result = rc;
1038 sess_data->func = NULL;
1039 sess_free_buffer(sess_data);
1040 kfree(ses->auth_key.response);
1041 ses->auth_key.response = NULL;
1044 #ifdef CONFIG_CIFS_UPCALL
1046 sess_auth_kerberos(struct sess_data *sess_data)
1049 struct smb_hdr *smb_buf;
1050 SESSION_SETUP_ANDX *pSMB;
1052 struct cifs_ses *ses = sess_data->ses;
1054 __u16 bytes_remaining;
1055 struct key *spnego_key = NULL;
1056 struct cifs_spnego_msg *msg;
1059 /* extended security */
1061 rc = sess_alloc_buffer(sess_data, 12);
1065 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1066 bcc_ptr = sess_data->iov[2].iov_base;
1067 capabilities = cifs_ssetup_hdr(ses, pSMB);
1069 spnego_key = cifs_get_spnego_key(ses);
1070 if (IS_ERR(spnego_key)) {
1071 rc = PTR_ERR(spnego_key);
1076 msg = spnego_key->payload.data[0];
1078 * check version field to make sure that cifs.upcall is
1079 * sending us a response in an expected form
1081 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1082 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1083 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1085 goto out_put_spnego_key;
1088 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1090 if (!ses->auth_key.response) {
1091 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1094 goto out_put_spnego_key;
1096 ses->auth_key.len = msg->sesskey_len;
1098 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1099 capabilities |= CAP_EXTENDED_SECURITY;
1100 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1101 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1102 sess_data->iov[1].iov_len = msg->secblob_len;
1103 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1105 if (ses->capabilities & CAP_UNICODE) {
1106 /* unicode strings must be word aligned */
1107 if ((sess_data->iov[0].iov_len
1108 + sess_data->iov[1].iov_len) % 2) {
1112 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1113 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1115 /* BB: is this right? */
1116 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1119 sess_data->iov[2].iov_len = (long) bcc_ptr -
1120 (long) sess_data->iov[2].iov_base;
1122 rc = sess_sendreceive(sess_data);
1124 goto out_put_spnego_key;
1126 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1127 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1129 if (smb_buf->WordCount != 4) {
1131 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1132 goto out_put_spnego_key;
1135 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1136 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1138 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1139 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1141 bytes_remaining = get_bcc(smb_buf);
1142 bcc_ptr = pByteArea(smb_buf);
1144 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1145 if (blob_len > bytes_remaining) {
1146 cifs_dbg(VFS, "bad security blob length %d\n",
1149 goto out_put_spnego_key;
1151 bcc_ptr += blob_len;
1152 bytes_remaining -= blob_len;
1154 /* BB check if Unicode and decode strings */
1155 if (bytes_remaining == 0) {
1156 /* no string area to decode, do nothing */
1157 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1158 /* unicode string area must be word-aligned */
1159 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1163 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1166 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1170 rc = sess_establish_session(sess_data);
1172 key_invalidate(spnego_key);
1173 key_put(spnego_key);
1175 sess_data->result = rc;
1176 sess_data->func = NULL;
1177 sess_free_buffer(sess_data);
1178 kfree(ses->auth_key.response);
1179 ses->auth_key.response = NULL;
1182 #endif /* ! CONFIG_CIFS_UPCALL */
1185 * The required kvec buffers have to be allocated before calling this
1189 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1191 SESSION_SETUP_ANDX *pSMB;
1192 struct cifs_ses *ses = sess_data->ses;
1196 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1198 capabilities = cifs_ssetup_hdr(ses, pSMB);
1199 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1200 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1204 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1205 capabilities |= CAP_EXTENDED_SECURITY;
1206 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1208 bcc_ptr = sess_data->iov[2].iov_base;
1209 /* unicode strings must be word aligned */
1210 if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1214 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1216 sess_data->iov[2].iov_len = (long) bcc_ptr -
1217 (long) sess_data->iov[2].iov_base;
1223 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1226 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1229 struct smb_hdr *smb_buf;
1230 SESSION_SETUP_ANDX *pSMB;
1231 struct cifs_ses *ses = sess_data->ses;
1232 __u16 bytes_remaining;
1236 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1239 * if memory allocation is successful, caller of this function
1242 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1243 if (!ses->ntlmssp) {
1247 ses->ntlmssp->sesskey_per_smbsess = false;
1250 rc = sess_alloc_buffer(sess_data, 12);
1254 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1256 /* Build security blob before we assemble the request */
1257 build_ntlmssp_negotiate_blob(pSMB->req.SecurityBlob, ses);
1258 sess_data->iov[1].iov_len = sizeof(NEGOTIATE_MESSAGE);
1259 sess_data->iov[1].iov_base = pSMB->req.SecurityBlob;
1260 pSMB->req.SecurityBlobLength = cpu_to_le16(sizeof(NEGOTIATE_MESSAGE));
1262 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1266 rc = sess_sendreceive(sess_data);
1268 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1269 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1271 /* If true, rc here is expected and not an error */
1272 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1273 smb_buf->Status.CifsError ==
1274 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1280 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1282 if (smb_buf->WordCount != 4) {
1284 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1288 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1289 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1291 bytes_remaining = get_bcc(smb_buf);
1292 bcc_ptr = pByteArea(smb_buf);
1294 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1295 if (blob_len > bytes_remaining) {
1296 cifs_dbg(VFS, "bad security blob length %d\n",
1302 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1304 sess_free_buffer(sess_data);
1307 sess_data->func = sess_auth_rawntlmssp_authenticate;
1311 /* Else error. Cleanup */
1312 kfree(ses->auth_key.response);
1313 ses->auth_key.response = NULL;
1314 kfree(ses->ntlmssp);
1315 ses->ntlmssp = NULL;
1317 sess_data->func = NULL;
1318 sess_data->result = rc;
1322 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1325 struct smb_hdr *smb_buf;
1326 SESSION_SETUP_ANDX *pSMB;
1327 struct cifs_ses *ses = sess_data->ses;
1328 __u16 bytes_remaining;
1330 unsigned char *ntlmsspblob = NULL;
1333 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1336 rc = sess_alloc_buffer(sess_data, 12);
1340 /* Build security blob before we assemble the request */
1341 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1342 smb_buf = (struct smb_hdr *)pSMB;
1343 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1344 &blob_len, ses, sess_data->nls_cp);
1346 goto out_free_ntlmsspblob;
1347 sess_data->iov[1].iov_len = blob_len;
1348 sess_data->iov[1].iov_base = ntlmsspblob;
1349 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1351 * Make sure that we tell the server that we are using
1352 * the uid that it just gave us back on the response
1355 smb_buf->Uid = ses->Suid;
1357 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1359 goto out_free_ntlmsspblob;
1361 rc = sess_sendreceive(sess_data);
1363 goto out_free_ntlmsspblob;
1365 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1366 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1367 if (smb_buf->WordCount != 4) {
1369 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1370 goto out_free_ntlmsspblob;
1373 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1374 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1376 if (ses->Suid != smb_buf->Uid) {
1377 ses->Suid = smb_buf->Uid;
1378 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1381 bytes_remaining = get_bcc(smb_buf);
1382 bcc_ptr = pByteArea(smb_buf);
1383 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1384 if (blob_len > bytes_remaining) {
1385 cifs_dbg(VFS, "bad security blob length %d\n",
1388 goto out_free_ntlmsspblob;
1390 bcc_ptr += blob_len;
1391 bytes_remaining -= blob_len;
1394 /* BB check if Unicode and decode strings */
1395 if (bytes_remaining == 0) {
1396 /* no string area to decode, do nothing */
1397 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1398 /* unicode string area must be word-aligned */
1399 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1403 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1406 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1410 out_free_ntlmsspblob:
1413 sess_free_buffer(sess_data);
1416 rc = sess_establish_session(sess_data);
1419 kfree(ses->auth_key.response);
1420 ses->auth_key.response = NULL;
1421 kfree(ses->ntlmssp);
1422 ses->ntlmssp = NULL;
1424 sess_data->func = NULL;
1425 sess_data->result = rc;
1428 static int select_sec(struct cifs_ses *ses, struct sess_data *sess_data)
1432 type = cifs_select_sectype(ses->server, ses->sectype);
1433 cifs_dbg(FYI, "sess setup type %d\n", type);
1434 if (type == Unspecified) {
1435 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1441 sess_data->func = sess_auth_ntlmv2;
1444 #ifdef CONFIG_CIFS_UPCALL
1445 sess_data->func = sess_auth_kerberos;
1448 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1450 #endif /* CONFIG_CIFS_UPCALL */
1452 sess_data->func = sess_auth_rawntlmssp_negotiate;
1455 cifs_dbg(VFS, "secType %d not supported!\n", type);
1462 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1463 const struct nls_table *nls_cp)
1466 struct sess_data *sess_data;
1469 WARN(1, "%s: ses == NULL!", __func__);
1473 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1477 rc = select_sec(ses, sess_data);
1481 sess_data->xid = xid;
1482 sess_data->ses = ses;
1483 sess_data->buf0_type = CIFS_NO_BUFFER;
1484 sess_data->nls_cp = (struct nls_table *) nls_cp;
1486 while (sess_data->func)
1487 sess_data->func(sess_data);
1489 /* Store result before we free sess_data */
1490 rc = sess_data->result;