Merge tag 'timers_urgent_for_v6.13_rc1' of git://git.kernel.org/pub/scm/linux/kernel...
[drm/drm-misc.git] / fs / smb / client / sess.c
blob0bb77f9ec686138e81ca56b7e5b50d13b4fc02bb
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
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)
9 */
11 #include "cifspdu.h"
12 #include "cifsglob.h"
13 #include "cifsproto.h"
14 #include "cifs_unicode.h"
15 #include "cifs_debug.h"
16 #include "ntlmssp.h"
17 #include "nterr.h"
18 #include <linux/utsname.h>
19 #include <linux/slab.h>
20 #include <linux/version.h>
21 #include "cifsfs.h"
22 #include "cifs_spnego.h"
23 #include "smb2proto.h"
24 #include "fs_context.h"
26 static int
27 cifs_ses_add_channel(struct cifs_ses *ses,
28 struct cifs_server_iface *iface);
30 bool
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)
40 return false;
41 if (server->dstaddr.ss_family == AF_INET) {
42 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
43 return false;
44 } else if (server->dstaddr.ss_family == AF_INET6) {
45 if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
46 sizeof(i6->sin6_addr)) != 0)
47 return false;
48 } else {
49 /* unknown family.. */
50 return false;
52 return true;
55 bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
57 int i;
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);
63 return true;
66 spin_unlock(&ses->chan_lock);
67 return false;
70 /* channel helper functions. assumed that chan_lock is held by caller. */
72 int
73 cifs_ses_get_chan_index(struct cifs_ses *ses,
74 struct TCP_Server_Info *server)
76 unsigned int i;
78 /* if the channel is waiting for termination */
79 if (server && server->terminate)
80 return CIFS_INVAL_CHAN_INDEX;
82 for (i = 0; i < ses->chan_count; i++) {
83 if (ses->chans[i].server == server)
84 return i;
87 /* If we didn't find the channel, it is likely a bug */
88 if (server)
89 cifs_dbg(VFS, "unable to get chan index for server: 0x%llx",
90 server->conn_id);
91 return CIFS_INVAL_CHAN_INDEX;
94 void
95 cifs_chan_set_in_reconnect(struct cifs_ses *ses,
96 struct TCP_Server_Info *server)
98 int chan_index = cifs_ses_get_chan_index(ses, server);
100 if (chan_index == CIFS_INVAL_CHAN_INDEX)
101 return;
103 ses->chans[chan_index].in_reconnect = true;
106 void
107 cifs_chan_clear_in_reconnect(struct cifs_ses *ses,
108 struct TCP_Server_Info *server)
110 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
112 if (chan_index == CIFS_INVAL_CHAN_INDEX)
113 return;
115 ses->chans[chan_index].in_reconnect = false;
118 void
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 if (chan_index == CIFS_INVAL_CHAN_INDEX)
125 return;
127 set_bit(chan_index, &ses->chans_need_reconnect);
128 cifs_dbg(FYI, "Set reconnect bitmask for chan %u; now 0x%lx\n",
129 chan_index, ses->chans_need_reconnect);
132 void
133 cifs_chan_clear_need_reconnect(struct cifs_ses *ses,
134 struct TCP_Server_Info *server)
136 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
138 if (chan_index == CIFS_INVAL_CHAN_INDEX)
139 return;
141 clear_bit(chan_index, &ses->chans_need_reconnect);
142 cifs_dbg(FYI, "Cleared reconnect bitmask for chan %u; now 0x%lx\n",
143 chan_index, ses->chans_need_reconnect);
146 bool
147 cifs_chan_needs_reconnect(struct cifs_ses *ses,
148 struct TCP_Server_Info *server)
150 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
152 if (chan_index == CIFS_INVAL_CHAN_INDEX)
153 return true; /* err on the safer side */
155 return CIFS_CHAN_NEEDS_RECONNECT(ses, chan_index);
158 bool
159 cifs_chan_is_iface_active(struct cifs_ses *ses,
160 struct TCP_Server_Info *server)
162 unsigned int chan_index = cifs_ses_get_chan_index(ses, server);
164 if (chan_index == CIFS_INVAL_CHAN_INDEX)
165 return true; /* err on the safer side */
167 return ses->chans[chan_index].iface &&
168 ses->chans[chan_index].iface->is_active;
171 /* returns number of channels added */
172 int cifs_try_adding_channels(struct cifs_ses *ses)
174 struct TCP_Server_Info *server = ses->server;
175 int old_chan_count, new_chan_count;
176 int left;
177 int rc = 0;
178 int tries = 0;
179 size_t iface_weight = 0, iface_min_speed = 0;
180 struct cifs_server_iface *iface = NULL, *niface = NULL;
181 struct cifs_server_iface *last_iface = NULL;
183 spin_lock(&ses->chan_lock);
185 new_chan_count = old_chan_count = ses->chan_count;
186 left = ses->chan_max - ses->chan_count;
188 if (left <= 0) {
189 spin_unlock(&ses->chan_lock);
190 cifs_dbg(FYI,
191 "ses already at max_channels (%zu), nothing to open\n",
192 ses->chan_max);
193 return 0;
196 if (server->dialect < SMB30_PROT_ID) {
197 spin_unlock(&ses->chan_lock);
198 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
199 return 0;
202 if (!(server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
203 spin_unlock(&ses->chan_lock);
204 cifs_server_dbg(VFS, "no multichannel support\n");
205 return 0;
207 spin_unlock(&ses->chan_lock);
209 while (left > 0) {
211 tries++;
212 if (tries > 3*ses->chan_max) {
213 cifs_dbg(VFS, "too many channel open attempts (%d channels left to open)\n",
214 left);
215 break;
218 spin_lock(&ses->iface_lock);
219 if (!ses->iface_count) {
220 spin_unlock(&ses->iface_lock);
221 cifs_dbg(ONCE, "server %s does not advertise interfaces\n",
222 ses->server->hostname);
223 break;
226 if (!iface)
227 iface = list_first_entry(&ses->iface_list, struct cifs_server_iface,
228 iface_head);
229 last_iface = list_last_entry(&ses->iface_list, struct cifs_server_iface,
230 iface_head);
231 iface_min_speed = last_iface->speed;
233 list_for_each_entry_safe_from(iface, niface, &ses->iface_list,
234 iface_head) {
235 /* do not mix rdma and non-rdma interfaces */
236 if (iface->rdma_capable != ses->server->rdma)
237 continue;
239 /* skip ifaces that are unusable */
240 if (!iface->is_active ||
241 (is_ses_using_iface(ses, iface) &&
242 !iface->rss_capable))
243 continue;
245 /* check if we already allocated enough channels */
246 iface_weight = iface->speed / iface_min_speed;
248 if (iface->weight_fulfilled >= iface_weight)
249 continue;
251 /* take ref before unlock */
252 kref_get(&iface->refcount);
254 spin_unlock(&ses->iface_lock);
255 rc = cifs_ses_add_channel(ses, iface);
256 spin_lock(&ses->iface_lock);
258 if (rc) {
259 cifs_dbg(VFS, "failed to open extra channel on iface:%pIS rc=%d\n",
260 &iface->sockaddr,
261 rc);
262 kref_put(&iface->refcount, release_iface);
263 /* failure to add chan should increase weight */
264 iface->weight_fulfilled++;
265 continue;
268 iface->num_channels++;
269 iface->weight_fulfilled++;
270 cifs_dbg(VFS, "successfully opened new channel on iface:%pIS\n",
271 &iface->sockaddr);
272 break;
275 /* reached end of list. reset weight_fulfilled and start over */
276 if (list_entry_is_head(iface, &ses->iface_list, iface_head)) {
277 list_for_each_entry(iface, &ses->iface_list, iface_head)
278 iface->weight_fulfilled = 0;
279 spin_unlock(&ses->iface_lock);
280 iface = NULL;
281 continue;
283 spin_unlock(&ses->iface_lock);
285 left--;
286 new_chan_count++;
289 return new_chan_count - old_chan_count;
293 * called when multichannel is disabled by the server.
294 * this always gets called from smb2_reconnect
295 * and cannot get called in parallel threads.
297 void
298 cifs_disable_secondary_channels(struct cifs_ses *ses)
300 int i, chan_count;
301 struct TCP_Server_Info *server;
302 struct cifs_server_iface *iface;
304 spin_lock(&ses->chan_lock);
305 chan_count = ses->chan_count;
306 if (chan_count == 1)
307 goto done;
309 ses->chan_count = 1;
311 /* for all secondary channels reset the need reconnect bit */
312 ses->chans_need_reconnect &= 1;
314 for (i = 1; i < chan_count; i++) {
315 iface = ses->chans[i].iface;
316 server = ses->chans[i].server;
319 * remove these references first, since we need to unlock
320 * the chan_lock here, since iface_lock is a higher lock
322 ses->chans[i].iface = NULL;
323 ses->chans[i].server = NULL;
324 spin_unlock(&ses->chan_lock);
326 if (iface) {
327 spin_lock(&ses->iface_lock);
328 iface->num_channels--;
329 if (iface->weight_fulfilled)
330 iface->weight_fulfilled--;
331 kref_put(&iface->refcount, release_iface);
332 spin_unlock(&ses->iface_lock);
335 if (server) {
336 if (!server->terminate) {
337 server->terminate = true;
338 cifs_signal_cifsd_for_reconnect(server, false);
340 cifs_put_tcp_session(server, false);
343 spin_lock(&ses->chan_lock);
346 done:
347 spin_unlock(&ses->chan_lock);
350 /* update the iface for the channel if necessary. */
351 void
352 cifs_chan_update_iface(struct cifs_ses *ses, struct TCP_Server_Info *server)
354 unsigned int chan_index;
355 size_t iface_weight = 0, iface_min_speed = 0;
356 struct cifs_server_iface *iface = NULL;
357 struct cifs_server_iface *old_iface = NULL;
358 struct cifs_server_iface *last_iface = NULL;
359 struct sockaddr_storage ss;
361 spin_lock(&ses->chan_lock);
362 chan_index = cifs_ses_get_chan_index(ses, server);
363 if (chan_index == CIFS_INVAL_CHAN_INDEX) {
364 spin_unlock(&ses->chan_lock);
365 return;
368 if (ses->chans[chan_index].iface) {
369 old_iface = ses->chans[chan_index].iface;
370 if (old_iface->is_active) {
371 spin_unlock(&ses->chan_lock);
372 return;
375 spin_unlock(&ses->chan_lock);
377 spin_lock(&server->srv_lock);
378 ss = server->dstaddr;
379 spin_unlock(&server->srv_lock);
381 spin_lock(&ses->iface_lock);
382 if (!ses->iface_count) {
383 spin_unlock(&ses->iface_lock);
384 cifs_dbg(ONCE, "server %s does not advertise interfaces\n", ses->server->hostname);
385 return;
388 last_iface = list_last_entry(&ses->iface_list, struct cifs_server_iface,
389 iface_head);
390 iface_min_speed = last_iface->speed;
392 /* then look for a new one */
393 list_for_each_entry(iface, &ses->iface_list, iface_head) {
394 if (!chan_index) {
395 /* if we're trying to get the updated iface for primary channel */
396 if (!cifs_match_ipaddr((struct sockaddr *) &ss,
397 (struct sockaddr *) &iface->sockaddr))
398 continue;
400 kref_get(&iface->refcount);
401 break;
404 /* do not mix rdma and non-rdma interfaces */
405 if (iface->rdma_capable != server->rdma)
406 continue;
408 if (!iface->is_active ||
409 (is_ses_using_iface(ses, iface) &&
410 !iface->rss_capable)) {
411 continue;
414 /* check if we already allocated enough channels */
415 iface_weight = iface->speed / iface_min_speed;
417 if (iface->weight_fulfilled >= iface_weight)
418 continue;
420 kref_get(&iface->refcount);
421 break;
424 if (list_entry_is_head(iface, &ses->iface_list, iface_head)) {
425 iface = NULL;
426 cifs_dbg(FYI, "unable to find a suitable iface\n");
429 if (!iface) {
430 if (!chan_index)
431 cifs_dbg(FYI, "unable to get the interface matching: %pIS\n",
432 &ss);
433 else {
434 cifs_dbg(FYI, "unable to find another interface to replace: %pIS\n",
435 &old_iface->sockaddr);
438 spin_unlock(&ses->iface_lock);
439 return;
442 /* now drop the ref to the current iface */
443 if (old_iface) {
444 cifs_dbg(FYI, "replacing iface: %pIS with %pIS\n",
445 &old_iface->sockaddr,
446 &iface->sockaddr);
448 old_iface->num_channels--;
449 if (old_iface->weight_fulfilled)
450 old_iface->weight_fulfilled--;
451 iface->num_channels++;
452 iface->weight_fulfilled++;
454 kref_put(&old_iface->refcount, release_iface);
455 } else if (!chan_index) {
456 /* special case: update interface for primary channel */
457 cifs_dbg(FYI, "referencing primary channel iface: %pIS\n",
458 &iface->sockaddr);
459 iface->num_channels++;
460 iface->weight_fulfilled++;
462 spin_unlock(&ses->iface_lock);
464 spin_lock(&ses->chan_lock);
465 chan_index = cifs_ses_get_chan_index(ses, server);
466 if (chan_index == CIFS_INVAL_CHAN_INDEX) {
467 spin_unlock(&ses->chan_lock);
468 return;
471 ses->chans[chan_index].iface = iface;
472 spin_unlock(&ses->chan_lock);
475 static int
476 cifs_ses_add_channel(struct cifs_ses *ses,
477 struct cifs_server_iface *iface)
479 struct TCP_Server_Info *chan_server;
480 struct cifs_chan *chan;
481 struct smb3_fs_context *ctx;
482 static const char unc_fmt[] = "\\%s\\foo";
483 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
484 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
485 size_t len;
486 int rc;
487 unsigned int xid = get_xid();
489 if (iface->sockaddr.ss_family == AF_INET)
490 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
491 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
492 &ipv4->sin_addr);
493 else
494 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n",
495 ses, iface->speed, iface->rdma_capable ? "yes" : "no",
496 &ipv6->sin6_addr);
499 * Setup a ctx with mostly the same info as the existing
500 * session and overwrite it with the requested iface data.
502 * We need to setup at least the fields used for negprot and
503 * sesssetup.
505 * We only need the ctx here, so we can reuse memory from
506 * the session and server without caring about memory
507 * management.
509 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
510 if (!ctx) {
511 rc = -ENOMEM;
512 goto out_free_xid;
515 /* Always make new connection for now (TODO?) */
516 ctx->nosharesock = true;
518 /* Auth */
519 ctx->domainauto = ses->domainAuto;
520 ctx->domainname = ses->domainName;
522 /* no hostname for extra channels */
523 ctx->server_hostname = "";
525 ctx->username = ses->user_name;
526 ctx->password = ses->password;
527 ctx->sectype = ses->sectype;
528 ctx->sign = ses->sign;
530 /* UNC and paths */
531 /* XXX: Use ses->server->hostname? */
532 len = sizeof(unc_fmt) + SERVER_NAME_LEN_WITH_NULL;
533 ctx->UNC = kzalloc(len, GFP_KERNEL);
534 if (!ctx->UNC) {
535 rc = -ENOMEM;
536 goto out_free_ctx;
538 scnprintf(ctx->UNC, len, unc_fmt, ses->ip_addr);
539 ctx->prepath = "";
541 /* Reuse same version as master connection */
542 ctx->vals = ses->server->vals;
543 ctx->ops = ses->server->ops;
545 ctx->noblocksnd = ses->server->noblocksnd;
546 ctx->noautotune = ses->server->noautotune;
547 ctx->sockopt_tcp_nodelay = ses->server->tcp_nodelay;
548 ctx->echo_interval = ses->server->echo_interval / HZ;
549 ctx->max_credits = ses->server->max_credits;
552 * This will be used for encoding/decoding user/domain/pw
553 * during sess setup auth.
555 ctx->local_nls = ses->local_nls;
557 /* Use RDMA if possible */
558 ctx->rdma = iface->rdma_capable;
559 memcpy(&ctx->dstaddr, &iface->sockaddr, sizeof(ctx->dstaddr));
561 /* reuse master con client guid */
562 memcpy(&ctx->client_guid, ses->server->client_guid,
563 sizeof(ctx->client_guid));
564 ctx->use_client_guid = true;
566 chan_server = cifs_get_tcp_session(ctx, ses->server);
568 spin_lock(&ses->chan_lock);
569 chan = &ses->chans[ses->chan_count];
570 chan->server = chan_server;
571 if (IS_ERR(chan->server)) {
572 rc = PTR_ERR(chan->server);
573 chan->server = NULL;
574 spin_unlock(&ses->chan_lock);
575 goto out;
577 chan->iface = iface;
578 ses->chan_count++;
579 atomic_set(&ses->chan_seq, 0);
581 /* Mark this channel as needing connect/setup */
582 cifs_chan_set_need_reconnect(ses, chan->server);
584 spin_unlock(&ses->chan_lock);
586 mutex_lock(&ses->session_mutex);
588 * We need to allocate the server crypto now as we will need
589 * to sign packets before we generate the channel signing key
590 * (we sign with the session key)
592 rc = smb311_crypto_shash_allocate(chan->server);
593 if (rc) {
594 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
595 mutex_unlock(&ses->session_mutex);
596 goto out;
599 rc = cifs_negotiate_protocol(xid, ses, chan->server);
600 if (!rc)
601 rc = cifs_setup_session(xid, ses, chan->server, ses->local_nls);
603 mutex_unlock(&ses->session_mutex);
605 out:
606 if (rc && chan->server) {
607 cifs_put_tcp_session(chan->server, 0);
609 spin_lock(&ses->chan_lock);
611 /* we rely on all bits beyond chan_count to be clear */
612 cifs_chan_clear_need_reconnect(ses, chan->server);
613 ses->chan_count--;
615 * chan_count should never reach 0 as at least the primary
616 * channel is always allocated
618 WARN_ON(ses->chan_count < 1);
619 spin_unlock(&ses->chan_lock);
622 kfree(ctx->UNC);
623 out_free_ctx:
624 kfree(ctx);
625 out_free_xid:
626 free_xid(xid);
627 return rc;
630 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
631 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses,
632 struct TCP_Server_Info *server,
633 SESSION_SETUP_ANDX *pSMB)
635 __u32 capabilities = 0;
637 /* init fields common to all four types of SessSetup */
638 /* Note that offsets for first seven fields in req struct are same */
639 /* in CIFS Specs so does not matter which of 3 forms of struct */
640 /* that we use in next few lines */
641 /* Note that header is initialized to zero in header_assemble */
642 pSMB->req.AndXCommand = 0xFF;
643 pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
644 CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
645 USHRT_MAX));
646 pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq);
647 pSMB->req.VcNumber = cpu_to_le16(1);
649 /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
651 /* BB verify whether signing required on neg or just auth frame (and NTLM case) */
653 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
654 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
656 if (server->sign)
657 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
659 if (ses->capabilities & CAP_UNICODE) {
660 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
661 capabilities |= CAP_UNICODE;
663 if (ses->capabilities & CAP_STATUS32) {
664 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
665 capabilities |= CAP_STATUS32;
667 if (ses->capabilities & CAP_DFS) {
668 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
669 capabilities |= CAP_DFS;
671 if (ses->capabilities & CAP_UNIX)
672 capabilities |= CAP_UNIX;
674 return capabilities;
677 static void
678 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
680 char *bcc_ptr = *pbcc_area;
681 int bytes_ret = 0;
683 /* Copy OS version */
684 bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
685 nls_cp);
686 bcc_ptr += 2 * bytes_ret;
687 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
688 32, nls_cp);
689 bcc_ptr += 2 * bytes_ret;
690 bcc_ptr += 2; /* trailing null */
692 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
693 32, nls_cp);
694 bcc_ptr += 2 * bytes_ret;
695 bcc_ptr += 2; /* trailing null */
697 *pbcc_area = bcc_ptr;
700 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
701 const struct nls_table *nls_cp)
703 char *bcc_ptr = *pbcc_area;
704 int bytes_ret = 0;
706 /* copy domain */
707 if (ses->domainName == NULL) {
709 * Sending null domain better than using a bogus domain name (as
710 * we did briefly in 2.6.18) since server will use its default
712 *bcc_ptr = 0;
713 *(bcc_ptr+1) = 0;
714 bytes_ret = 0;
715 } else
716 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
717 CIFS_MAX_DOMAINNAME_LEN, nls_cp);
718 bcc_ptr += 2 * bytes_ret;
719 bcc_ptr += 2; /* account for null terminator */
721 *pbcc_area = bcc_ptr;
724 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
725 const struct nls_table *nls_cp)
727 char *bcc_ptr = *pbcc_area;
728 int bytes_ret = 0;
730 /* BB FIXME add check that strings less than 335 or will need to send as arrays */
732 /* copy user */
733 if (ses->user_name == NULL) {
734 /* null user mount */
735 *bcc_ptr = 0;
736 *(bcc_ptr+1) = 0;
737 } else {
738 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
739 CIFS_MAX_USERNAME_LEN, nls_cp);
741 bcc_ptr += 2 * bytes_ret;
742 bcc_ptr += 2; /* account for null termination */
744 unicode_domain_string(&bcc_ptr, ses, nls_cp);
745 unicode_oslm_strings(&bcc_ptr, nls_cp);
747 *pbcc_area = bcc_ptr;
750 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
751 const struct nls_table *nls_cp)
753 char *bcc_ptr = *pbcc_area;
754 int len;
756 /* copy user */
757 /* BB what about null user mounts - check that we do this BB */
758 /* copy user */
759 if (ses->user_name != NULL) {
760 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
761 if (WARN_ON_ONCE(len < 0))
762 len = CIFS_MAX_USERNAME_LEN - 1;
763 bcc_ptr += len;
765 /* else null user mount */
766 *bcc_ptr = 0;
767 bcc_ptr++; /* account for null termination */
769 /* copy domain */
770 if (ses->domainName != NULL) {
771 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
772 if (WARN_ON_ONCE(len < 0))
773 len = CIFS_MAX_DOMAINNAME_LEN - 1;
774 bcc_ptr += len;
775 } /* else we send a null domain name so server will default to its own domain */
776 *bcc_ptr = 0;
777 bcc_ptr++;
779 /* BB check for overflow here */
781 strcpy(bcc_ptr, "Linux version ");
782 bcc_ptr += strlen("Linux version ");
783 strcpy(bcc_ptr, init_utsname()->release);
784 bcc_ptr += strlen(init_utsname()->release) + 1;
786 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
787 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
789 *pbcc_area = bcc_ptr;
792 static void
793 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
794 const struct nls_table *nls_cp)
796 int len;
797 char *data = *pbcc_area;
799 cifs_dbg(FYI, "bleft %d\n", bleft);
801 kfree(ses->serverOS);
802 ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
803 cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
804 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
805 data += len;
806 bleft -= len;
807 if (bleft <= 0)
808 return;
810 kfree(ses->serverNOS);
811 ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
812 cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
813 len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
814 data += len;
815 bleft -= len;
816 if (bleft <= 0)
817 return;
819 kfree(ses->serverDomain);
820 ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
821 cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
823 return;
826 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
827 struct cifs_ses *ses,
828 const struct nls_table *nls_cp)
830 int len;
831 char *bcc_ptr = *pbcc_area;
833 cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
835 len = strnlen(bcc_ptr, bleft);
836 if (len >= bleft)
837 return;
839 kfree(ses->serverOS);
841 ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
842 if (ses->serverOS) {
843 memcpy(ses->serverOS, bcc_ptr, len);
844 ses->serverOS[len] = 0;
845 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
846 cifs_dbg(FYI, "OS/2 server\n");
849 bcc_ptr += len + 1;
850 bleft -= len + 1;
852 len = strnlen(bcc_ptr, bleft);
853 if (len >= bleft)
854 return;
856 kfree(ses->serverNOS);
858 ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
859 if (ses->serverNOS) {
860 memcpy(ses->serverNOS, bcc_ptr, len);
861 ses->serverNOS[len] = 0;
864 bcc_ptr += len + 1;
865 bleft -= len + 1;
867 len = strnlen(bcc_ptr, bleft);
868 if (len > bleft)
869 return;
872 * No domain field in LANMAN case. Domain is
873 * returned by old servers in the SMB negprot response
875 * BB For newer servers which do not support Unicode,
876 * but thus do return domain here, we could add parsing
877 * for it later, but it is not very important
879 cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
881 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
883 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
884 struct cifs_ses *ses)
886 unsigned int tioffset; /* challenge message target info area */
887 unsigned int tilen; /* challenge message target info area length */
888 CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
889 __u32 server_flags;
891 if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
892 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
893 return -EINVAL;
896 if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
897 cifs_dbg(VFS, "blob signature incorrect %s\n",
898 pblob->Signature);
899 return -EINVAL;
901 if (pblob->MessageType != NtLmChallenge) {
902 cifs_dbg(VFS, "Incorrect message type %d\n",
903 pblob->MessageType);
904 return -EINVAL;
907 server_flags = le32_to_cpu(pblob->NegotiateFlags);
908 cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__,
909 ses->ntlmssp->client_flags, server_flags);
911 if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) &&
912 (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) {
913 cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n",
914 __func__);
915 return -EINVAL;
917 if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) {
918 cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__);
919 return -EINVAL;
921 if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) {
922 cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n",
923 __func__);
924 return -EOPNOTSUPP;
926 if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
927 !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH))
928 pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n",
929 __func__);
931 ses->ntlmssp->server_flags = server_flags;
933 memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
935 * In particular we can examine sign flags
937 * BB spec says that if AvId field of MsvAvTimestamp is populated then
938 * we must set the MIC field of the AUTHENTICATE_MESSAGE
941 tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
942 tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
943 if (tioffset > blob_len || tioffset + tilen > blob_len) {
944 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
945 tioffset, tilen);
946 return -EINVAL;
948 if (tilen) {
949 kfree_sensitive(ses->auth_key.response);
950 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
951 GFP_KERNEL);
952 if (!ses->auth_key.response) {
953 cifs_dbg(VFS, "Challenge target info alloc failure\n");
954 return -ENOMEM;
956 ses->auth_key.len = tilen;
959 return 0;
962 static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size)
964 int sz = base_size + ses->auth_key.len
965 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
967 if (ses->domainName)
968 sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
969 else
970 sz += sizeof(__le16);
972 if (ses->user_name)
973 sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
974 else
975 sz += sizeof(__le16);
977 if (ses->workstation_name[0])
978 sz += sizeof(__le16) * strnlen(ses->workstation_name,
979 ntlmssp_workstation_name_size(ses));
980 else
981 sz += sizeof(__le16);
983 return sz;
986 static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf,
987 char *str_value,
988 int str_length,
989 unsigned char *pstart,
990 unsigned char **pcur,
991 const struct nls_table *nls_cp)
993 unsigned char *tmp = pstart;
994 int len;
996 if (!pbuf)
997 return;
999 if (!pcur)
1000 pcur = &tmp;
1002 if (!str_value) {
1003 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
1004 pbuf->Length = 0;
1005 pbuf->MaximumLength = 0;
1006 *pcur += sizeof(__le16);
1007 } else {
1008 len = cifs_strtoUTF16((__le16 *)*pcur,
1009 str_value,
1010 str_length,
1011 nls_cp);
1012 len *= sizeof(__le16);
1013 pbuf->BufferOffset = cpu_to_le32(*pcur - pstart);
1014 pbuf->Length = cpu_to_le16(len);
1015 pbuf->MaximumLength = cpu_to_le16(len);
1016 *pcur += len;
1020 /* BB Move to ntlmssp.c eventually */
1022 int build_ntlmssp_negotiate_blob(unsigned char **pbuffer,
1023 u16 *buflen,
1024 struct cifs_ses *ses,
1025 struct TCP_Server_Info *server,
1026 const struct nls_table *nls_cp)
1028 int rc = 0;
1029 NEGOTIATE_MESSAGE *sec_blob;
1030 __u32 flags;
1031 unsigned char *tmp;
1032 int len;
1034 len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE));
1035 *pbuffer = kmalloc(len, GFP_KERNEL);
1036 if (!*pbuffer) {
1037 rc = -ENOMEM;
1038 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
1039 *buflen = 0;
1040 goto setup_ntlm_neg_ret;
1042 sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer;
1044 memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
1045 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1046 sec_blob->MessageType = NtLmNegotiate;
1048 /* BB is NTLMV2 session security format easier to use here? */
1049 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
1050 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
1051 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
1052 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
1053 NTLMSSP_NEGOTIATE_SIGN;
1054 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
1055 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
1057 tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE);
1058 ses->ntlmssp->client_flags = flags;
1059 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1061 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
1062 cifs_security_buffer_from_str(&sec_blob->DomainName,
1063 NULL,
1064 CIFS_MAX_DOMAINNAME_LEN,
1065 *pbuffer, &tmp,
1066 nls_cp);
1068 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1069 NULL,
1070 CIFS_MAX_WORKSTATION_LEN,
1071 *pbuffer, &tmp,
1072 nls_cp);
1074 *buflen = tmp - *pbuffer;
1075 setup_ntlm_neg_ret:
1076 return rc;
1080 * Build ntlmssp blob with additional fields, such as version,
1081 * supported by modern servers. For safety limit to SMB3 or later
1082 * See notes in MS-NLMP Section 2.2.2.1 e.g.
1084 int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer,
1085 u16 *buflen,
1086 struct cifs_ses *ses,
1087 struct TCP_Server_Info *server,
1088 const struct nls_table *nls_cp)
1090 int rc = 0;
1091 struct negotiate_message *sec_blob;
1092 __u32 flags;
1093 unsigned char *tmp;
1094 int len;
1096 len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message));
1097 *pbuffer = kmalloc(len, GFP_KERNEL);
1098 if (!*pbuffer) {
1099 rc = -ENOMEM;
1100 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
1101 *buflen = 0;
1102 goto setup_ntlm_smb3_neg_ret;
1104 sec_blob = (struct negotiate_message *)*pbuffer;
1106 memset(*pbuffer, 0, sizeof(struct negotiate_message));
1107 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1108 sec_blob->MessageType = NtLmNegotiate;
1110 /* BB is NTLMV2 session security format easier to use here? */
1111 flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET |
1112 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
1113 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
1114 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
1115 NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION;
1116 if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
1117 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
1119 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
1120 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
1121 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
1122 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
1124 tmp = *pbuffer + sizeof(struct negotiate_message);
1125 ses->ntlmssp->client_flags = flags;
1126 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1128 /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */
1129 cifs_security_buffer_from_str(&sec_blob->DomainName,
1130 NULL,
1131 CIFS_MAX_DOMAINNAME_LEN,
1132 *pbuffer, &tmp,
1133 nls_cp);
1135 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1136 NULL,
1137 CIFS_MAX_WORKSTATION_LEN,
1138 *pbuffer, &tmp,
1139 nls_cp);
1141 *buflen = tmp - *pbuffer;
1142 setup_ntlm_smb3_neg_ret:
1143 return rc;
1147 /* See MS-NLMP 2.2.1.3 */
1148 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
1149 u16 *buflen,
1150 struct cifs_ses *ses,
1151 struct TCP_Server_Info *server,
1152 const struct nls_table *nls_cp)
1154 int rc;
1155 AUTHENTICATE_MESSAGE *sec_blob;
1156 __u32 flags;
1157 unsigned char *tmp;
1158 int len;
1160 rc = setup_ntlmv2_rsp(ses, nls_cp);
1161 if (rc) {
1162 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
1163 *buflen = 0;
1164 goto setup_ntlmv2_ret;
1167 len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE));
1168 *pbuffer = kmalloc(len, GFP_KERNEL);
1169 if (!*pbuffer) {
1170 rc = -ENOMEM;
1171 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
1172 *buflen = 0;
1173 goto setup_ntlmv2_ret;
1175 sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
1177 memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
1178 sec_blob->MessageType = NtLmAuthenticate;
1180 /* send version information in ntlmssp authenticate also */
1181 flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET |
1182 NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_VERSION |
1183 NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED;
1185 sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR;
1186 sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL;
1187 sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD);
1188 sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3;
1190 tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
1191 sec_blob->NegotiateFlags = cpu_to_le32(flags);
1193 sec_blob->LmChallengeResponse.BufferOffset =
1194 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
1195 sec_blob->LmChallengeResponse.Length = 0;
1196 sec_blob->LmChallengeResponse.MaximumLength = 0;
1198 sec_blob->NtChallengeResponse.BufferOffset =
1199 cpu_to_le32(tmp - *pbuffer);
1200 if (ses->user_name != NULL) {
1201 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1202 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1203 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1205 sec_blob->NtChallengeResponse.Length =
1206 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1207 sec_blob->NtChallengeResponse.MaximumLength =
1208 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1209 } else {
1211 * don't send an NT Response for anonymous access
1213 sec_blob->NtChallengeResponse.Length = 0;
1214 sec_blob->NtChallengeResponse.MaximumLength = 0;
1217 cifs_security_buffer_from_str(&sec_blob->DomainName,
1218 ses->domainName,
1219 CIFS_MAX_DOMAINNAME_LEN,
1220 *pbuffer, &tmp,
1221 nls_cp);
1223 cifs_security_buffer_from_str(&sec_blob->UserName,
1224 ses->user_name,
1225 CIFS_MAX_USERNAME_LEN,
1226 *pbuffer, &tmp,
1227 nls_cp);
1229 cifs_security_buffer_from_str(&sec_blob->WorkstationName,
1230 ses->workstation_name,
1231 ntlmssp_workstation_name_size(ses),
1232 *pbuffer, &tmp,
1233 nls_cp);
1235 if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) &&
1236 (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) &&
1237 !calc_seckey(ses)) {
1238 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
1239 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1240 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
1241 sec_blob->SessionKey.MaximumLength =
1242 cpu_to_le16(CIFS_CPHTXT_SIZE);
1243 tmp += CIFS_CPHTXT_SIZE;
1244 } else {
1245 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
1246 sec_blob->SessionKey.Length = 0;
1247 sec_blob->SessionKey.MaximumLength = 0;
1250 *buflen = tmp - *pbuffer;
1251 setup_ntlmv2_ret:
1252 return rc;
1255 enum securityEnum
1256 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
1258 switch (server->negflavor) {
1259 case CIFS_NEGFLAVOR_EXTENDED:
1260 switch (requested) {
1261 case Kerberos:
1262 case RawNTLMSSP:
1263 return requested;
1264 case Unspecified:
1265 if (server->sec_ntlmssp &&
1266 (global_secflags & CIFSSEC_MAY_NTLMSSP))
1267 return RawNTLMSSP;
1268 if ((server->sec_kerberos || server->sec_mskerberos) &&
1269 (global_secflags & CIFSSEC_MAY_KRB5))
1270 return Kerberos;
1271 fallthrough;
1272 default:
1273 return Unspecified;
1275 case CIFS_NEGFLAVOR_UNENCAP:
1276 switch (requested) {
1277 case NTLMv2:
1278 return requested;
1279 case Unspecified:
1280 if (global_secflags & CIFSSEC_MAY_NTLMV2)
1281 return NTLMv2;
1282 break;
1283 default:
1284 break;
1286 fallthrough;
1287 default:
1288 return Unspecified;
1292 struct sess_data {
1293 unsigned int xid;
1294 struct cifs_ses *ses;
1295 struct TCP_Server_Info *server;
1296 struct nls_table *nls_cp;
1297 void (*func)(struct sess_data *);
1298 int result;
1300 /* we will send the SMB in three pieces:
1301 * a fixed length beginning part, an optional
1302 * SPNEGO blob (which can be zero length), and a
1303 * last part which will include the strings
1304 * and rest of bcc area. This allows us to avoid
1305 * a large buffer 17K allocation
1307 int buf0_type;
1308 struct kvec iov[3];
1311 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1312 static int
1313 sess_alloc_buffer(struct sess_data *sess_data, int wct)
1315 int rc;
1316 struct cifs_ses *ses = sess_data->ses;
1317 struct smb_hdr *smb_buf;
1319 rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
1320 (void **)&smb_buf);
1322 if (rc)
1323 return rc;
1325 sess_data->iov[0].iov_base = (char *)smb_buf;
1326 sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
1328 * This variable will be used to clear the buffer
1329 * allocated above in case of any error in the calling function.
1331 sess_data->buf0_type = CIFS_SMALL_BUFFER;
1333 /* 2000 big enough to fit max user, domain, NOS name etc. */
1334 sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
1335 if (!sess_data->iov[2].iov_base) {
1336 rc = -ENOMEM;
1337 goto out_free_smb_buf;
1340 return 0;
1342 out_free_smb_buf:
1343 cifs_small_buf_release(smb_buf);
1344 sess_data->iov[0].iov_base = NULL;
1345 sess_data->iov[0].iov_len = 0;
1346 sess_data->buf0_type = CIFS_NO_BUFFER;
1347 return rc;
1350 static void
1351 sess_free_buffer(struct sess_data *sess_data)
1353 struct kvec *iov = sess_data->iov;
1356 * Zero the session data before freeing, as it might contain sensitive info (keys, etc).
1357 * Note that iov[1] is already freed by caller.
1359 if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
1360 memzero_explicit(iov[0].iov_base, iov[0].iov_len);
1362 free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
1363 sess_data->buf0_type = CIFS_NO_BUFFER;
1364 kfree_sensitive(iov[2].iov_base);
1367 static int
1368 sess_establish_session(struct sess_data *sess_data)
1370 struct cifs_ses *ses = sess_data->ses;
1371 struct TCP_Server_Info *server = sess_data->server;
1373 cifs_server_lock(server);
1374 if (!server->session_estab) {
1375 if (server->sign) {
1376 server->session_key.response =
1377 kmemdup(ses->auth_key.response,
1378 ses->auth_key.len, GFP_KERNEL);
1379 if (!server->session_key.response) {
1380 cifs_server_unlock(server);
1381 return -ENOMEM;
1383 server->session_key.len =
1384 ses->auth_key.len;
1386 server->sequence_number = 0x2;
1387 server->session_estab = true;
1389 cifs_server_unlock(server);
1391 cifs_dbg(FYI, "CIFS session established successfully\n");
1392 return 0;
1395 static int
1396 sess_sendreceive(struct sess_data *sess_data)
1398 int rc;
1399 struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
1400 __u16 count;
1401 struct kvec rsp_iov = { NULL, 0 };
1403 count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
1404 be32_add_cpu(&smb_buf->smb_buf_length, count);
1405 put_bcc(count, smb_buf);
1407 rc = SendReceive2(sess_data->xid, sess_data->ses,
1408 sess_data->iov, 3 /* num_iovecs */,
1409 &sess_data->buf0_type,
1410 CIFS_LOG_ERROR, &rsp_iov);
1411 cifs_small_buf_release(sess_data->iov[0].iov_base);
1412 memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
1414 return rc;
1417 static void
1418 sess_auth_ntlmv2(struct sess_data *sess_data)
1420 int rc = 0;
1421 struct smb_hdr *smb_buf;
1422 SESSION_SETUP_ANDX *pSMB;
1423 char *bcc_ptr;
1424 struct cifs_ses *ses = sess_data->ses;
1425 struct TCP_Server_Info *server = sess_data->server;
1426 __u32 capabilities;
1427 __u16 bytes_remaining;
1429 /* old style NTLM sessionsetup */
1430 /* wct = 13 */
1431 rc = sess_alloc_buffer(sess_data, 13);
1432 if (rc)
1433 goto out;
1435 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1436 bcc_ptr = sess_data->iov[2].iov_base;
1437 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1439 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1441 /* LM2 password would be here if we supported it */
1442 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1444 if (ses->user_name != NULL) {
1445 /* calculate nlmv2 response and session key */
1446 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1447 if (rc) {
1448 cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1449 goto out;
1452 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1453 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1454 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1456 /* set case sensitive password length after tilen may get
1457 * assigned, tilen is 0 otherwise.
1459 pSMB->req_no_secext.CaseSensitivePasswordLength =
1460 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1461 } else {
1462 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1465 if (ses->capabilities & CAP_UNICODE) {
1466 if (!IS_ALIGNED(sess_data->iov[0].iov_len, 2)) {
1467 *bcc_ptr = 0;
1468 bcc_ptr++;
1470 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1471 } else {
1472 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1476 sess_data->iov[2].iov_len = (long) bcc_ptr -
1477 (long) sess_data->iov[2].iov_base;
1479 rc = sess_sendreceive(sess_data);
1480 if (rc)
1481 goto out;
1483 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1484 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1486 if (smb_buf->WordCount != 3) {
1487 rc = -EIO;
1488 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1489 goto out;
1492 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1493 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1495 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1496 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1498 bytes_remaining = get_bcc(smb_buf);
1499 bcc_ptr = pByteArea(smb_buf);
1501 /* BB check if Unicode and decode strings */
1502 if (bytes_remaining == 0) {
1503 /* no string area to decode, do nothing */
1504 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1505 /* unicode string area must be word-aligned */
1506 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1507 ++bcc_ptr;
1508 --bytes_remaining;
1510 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1511 sess_data->nls_cp);
1512 } else {
1513 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1514 sess_data->nls_cp);
1517 rc = sess_establish_session(sess_data);
1518 out:
1519 sess_data->result = rc;
1520 sess_data->func = NULL;
1521 sess_free_buffer(sess_data);
1522 kfree_sensitive(ses->auth_key.response);
1523 ses->auth_key.response = NULL;
1526 #ifdef CONFIG_CIFS_UPCALL
1527 static void
1528 sess_auth_kerberos(struct sess_data *sess_data)
1530 int rc = 0;
1531 struct smb_hdr *smb_buf;
1532 SESSION_SETUP_ANDX *pSMB;
1533 char *bcc_ptr;
1534 struct cifs_ses *ses = sess_data->ses;
1535 struct TCP_Server_Info *server = sess_data->server;
1536 __u32 capabilities;
1537 __u16 bytes_remaining;
1538 struct key *spnego_key = NULL;
1539 struct cifs_spnego_msg *msg;
1540 u16 blob_len;
1542 /* extended security */
1543 /* wct = 12 */
1544 rc = sess_alloc_buffer(sess_data, 12);
1545 if (rc)
1546 goto out;
1548 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1549 bcc_ptr = sess_data->iov[2].iov_base;
1550 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1552 spnego_key = cifs_get_spnego_key(ses, server);
1553 if (IS_ERR(spnego_key)) {
1554 rc = PTR_ERR(spnego_key);
1555 spnego_key = NULL;
1556 goto out;
1559 msg = spnego_key->payload.data[0];
1561 * check version field to make sure that cifs.upcall is
1562 * sending us a response in an expected form
1564 if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1565 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1566 CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1567 rc = -EKEYREJECTED;
1568 goto out_put_spnego_key;
1571 kfree_sensitive(ses->auth_key.response);
1572 ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1573 GFP_KERNEL);
1574 if (!ses->auth_key.response) {
1575 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1576 msg->sesskey_len);
1577 rc = -ENOMEM;
1578 goto out_put_spnego_key;
1580 ses->auth_key.len = msg->sesskey_len;
1582 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1583 capabilities |= CAP_EXTENDED_SECURITY;
1584 pSMB->req.Capabilities = cpu_to_le32(capabilities);
1585 sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1586 sess_data->iov[1].iov_len = msg->secblob_len;
1587 pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1589 if (ses->capabilities & CAP_UNICODE) {
1590 /* unicode strings must be word aligned */
1591 if (!IS_ALIGNED(sess_data->iov[0].iov_len + sess_data->iov[1].iov_len, 2)) {
1592 *bcc_ptr = 0;
1593 bcc_ptr++;
1595 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1596 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1597 } else {
1598 /* BB: is this right? */
1599 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1602 sess_data->iov[2].iov_len = (long) bcc_ptr -
1603 (long) sess_data->iov[2].iov_base;
1605 rc = sess_sendreceive(sess_data);
1606 if (rc)
1607 goto out_put_spnego_key;
1609 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1610 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1612 if (smb_buf->WordCount != 4) {
1613 rc = -EIO;
1614 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1615 goto out_put_spnego_key;
1618 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1619 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1621 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1622 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1624 bytes_remaining = get_bcc(smb_buf);
1625 bcc_ptr = pByteArea(smb_buf);
1627 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1628 if (blob_len > bytes_remaining) {
1629 cifs_dbg(VFS, "bad security blob length %d\n",
1630 blob_len);
1631 rc = -EINVAL;
1632 goto out_put_spnego_key;
1634 bcc_ptr += blob_len;
1635 bytes_remaining -= blob_len;
1637 /* BB check if Unicode and decode strings */
1638 if (bytes_remaining == 0) {
1639 /* no string area to decode, do nothing */
1640 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1641 /* unicode string area must be word-aligned */
1642 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1643 ++bcc_ptr;
1644 --bytes_remaining;
1646 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1647 sess_data->nls_cp);
1648 } else {
1649 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1650 sess_data->nls_cp);
1653 rc = sess_establish_session(sess_data);
1654 out_put_spnego_key:
1655 key_invalidate(spnego_key);
1656 key_put(spnego_key);
1657 out:
1658 sess_data->result = rc;
1659 sess_data->func = NULL;
1660 sess_free_buffer(sess_data);
1661 kfree_sensitive(ses->auth_key.response);
1662 ses->auth_key.response = NULL;
1665 #endif /* ! CONFIG_CIFS_UPCALL */
1668 * The required kvec buffers have to be allocated before calling this
1669 * function.
1671 static int
1672 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1674 SESSION_SETUP_ANDX *pSMB;
1675 struct cifs_ses *ses = sess_data->ses;
1676 struct TCP_Server_Info *server = sess_data->server;
1677 __u32 capabilities;
1678 char *bcc_ptr;
1680 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1682 capabilities = cifs_ssetup_hdr(ses, server, pSMB);
1683 if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1684 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1685 return -ENOSYS;
1688 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1689 capabilities |= CAP_EXTENDED_SECURITY;
1690 pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1692 bcc_ptr = sess_data->iov[2].iov_base;
1693 /* unicode strings must be word aligned */
1694 if (!IS_ALIGNED(sess_data->iov[0].iov_len + sess_data->iov[1].iov_len, 2)) {
1695 *bcc_ptr = 0;
1696 bcc_ptr++;
1698 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1700 sess_data->iov[2].iov_len = (long) bcc_ptr -
1701 (long) sess_data->iov[2].iov_base;
1703 return 0;
1706 static void
1707 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1709 static void
1710 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1712 int rc;
1713 struct smb_hdr *smb_buf;
1714 SESSION_SETUP_ANDX *pSMB;
1715 struct cifs_ses *ses = sess_data->ses;
1716 struct TCP_Server_Info *server = sess_data->server;
1717 __u16 bytes_remaining;
1718 char *bcc_ptr;
1719 unsigned char *ntlmsspblob = NULL;
1720 u16 blob_len;
1722 cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1725 * if memory allocation is successful, caller of this function
1726 * frees it.
1728 ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1729 if (!ses->ntlmssp) {
1730 rc = -ENOMEM;
1731 goto out;
1733 ses->ntlmssp->sesskey_per_smbsess = false;
1735 /* wct = 12 */
1736 rc = sess_alloc_buffer(sess_data, 12);
1737 if (rc)
1738 goto out;
1740 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1742 /* Build security blob before we assemble the request */
1743 rc = build_ntlmssp_negotiate_blob(&ntlmsspblob,
1744 &blob_len, ses, server,
1745 sess_data->nls_cp);
1746 if (rc)
1747 goto out_free_ntlmsspblob;
1749 sess_data->iov[1].iov_len = blob_len;
1750 sess_data->iov[1].iov_base = ntlmsspblob;
1751 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1753 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1754 if (rc)
1755 goto out_free_ntlmsspblob;
1757 rc = sess_sendreceive(sess_data);
1759 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1760 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1762 /* If true, rc here is expected and not an error */
1763 if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1764 smb_buf->Status.CifsError ==
1765 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1766 rc = 0;
1768 if (rc)
1769 goto out_free_ntlmsspblob;
1771 cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1773 if (smb_buf->WordCount != 4) {
1774 rc = -EIO;
1775 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1776 goto out_free_ntlmsspblob;
1779 ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */
1780 cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1782 bytes_remaining = get_bcc(smb_buf);
1783 bcc_ptr = pByteArea(smb_buf);
1785 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1786 if (blob_len > bytes_remaining) {
1787 cifs_dbg(VFS, "bad security blob length %d\n",
1788 blob_len);
1789 rc = -EINVAL;
1790 goto out_free_ntlmsspblob;
1793 rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1795 out_free_ntlmsspblob:
1796 kfree_sensitive(ntlmsspblob);
1797 out:
1798 sess_free_buffer(sess_data);
1800 if (!rc) {
1801 sess_data->func = sess_auth_rawntlmssp_authenticate;
1802 return;
1805 /* Else error. Cleanup */
1806 kfree_sensitive(ses->auth_key.response);
1807 ses->auth_key.response = NULL;
1808 kfree_sensitive(ses->ntlmssp);
1809 ses->ntlmssp = NULL;
1811 sess_data->func = NULL;
1812 sess_data->result = rc;
1815 static void
1816 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1818 int rc;
1819 struct smb_hdr *smb_buf;
1820 SESSION_SETUP_ANDX *pSMB;
1821 struct cifs_ses *ses = sess_data->ses;
1822 struct TCP_Server_Info *server = sess_data->server;
1823 __u16 bytes_remaining;
1824 char *bcc_ptr;
1825 unsigned char *ntlmsspblob = NULL;
1826 u16 blob_len;
1828 cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1830 /* wct = 12 */
1831 rc = sess_alloc_buffer(sess_data, 12);
1832 if (rc)
1833 goto out;
1835 /* Build security blob before we assemble the request */
1836 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1837 smb_buf = (struct smb_hdr *)pSMB;
1838 rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1839 &blob_len, ses, server,
1840 sess_data->nls_cp);
1841 if (rc)
1842 goto out_free_ntlmsspblob;
1843 sess_data->iov[1].iov_len = blob_len;
1844 sess_data->iov[1].iov_base = ntlmsspblob;
1845 pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1847 * Make sure that we tell the server that we are using
1848 * the uid that it just gave us back on the response
1849 * (challenge)
1851 smb_buf->Uid = ses->Suid;
1853 rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1854 if (rc)
1855 goto out_free_ntlmsspblob;
1857 rc = sess_sendreceive(sess_data);
1858 if (rc)
1859 goto out_free_ntlmsspblob;
1861 pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1862 smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1863 if (smb_buf->WordCount != 4) {
1864 rc = -EIO;
1865 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1866 goto out_free_ntlmsspblob;
1869 if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1870 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1872 if (ses->Suid != smb_buf->Uid) {
1873 ses->Suid = smb_buf->Uid;
1874 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1877 bytes_remaining = get_bcc(smb_buf);
1878 bcc_ptr = pByteArea(smb_buf);
1879 blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1880 if (blob_len > bytes_remaining) {
1881 cifs_dbg(VFS, "bad security blob length %d\n",
1882 blob_len);
1883 rc = -EINVAL;
1884 goto out_free_ntlmsspblob;
1886 bcc_ptr += blob_len;
1887 bytes_remaining -= blob_len;
1890 /* BB check if Unicode and decode strings */
1891 if (bytes_remaining == 0) {
1892 /* no string area to decode, do nothing */
1893 } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1894 /* unicode string area must be word-aligned */
1895 if (!IS_ALIGNED((unsigned long)bcc_ptr - (unsigned long)smb_buf, 2)) {
1896 ++bcc_ptr;
1897 --bytes_remaining;
1899 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1900 sess_data->nls_cp);
1901 } else {
1902 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1903 sess_data->nls_cp);
1906 out_free_ntlmsspblob:
1907 kfree_sensitive(ntlmsspblob);
1908 out:
1909 sess_free_buffer(sess_data);
1911 if (!rc)
1912 rc = sess_establish_session(sess_data);
1914 /* Cleanup */
1915 kfree_sensitive(ses->auth_key.response);
1916 ses->auth_key.response = NULL;
1917 kfree_sensitive(ses->ntlmssp);
1918 ses->ntlmssp = NULL;
1920 sess_data->func = NULL;
1921 sess_data->result = rc;
1924 static int select_sec(struct sess_data *sess_data)
1926 int type;
1927 struct cifs_ses *ses = sess_data->ses;
1928 struct TCP_Server_Info *server = sess_data->server;
1930 type = cifs_select_sectype(server, ses->sectype);
1931 cifs_dbg(FYI, "sess setup type %d\n", type);
1932 if (type == Unspecified) {
1933 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1934 return -EINVAL;
1937 switch (type) {
1938 case NTLMv2:
1939 sess_data->func = sess_auth_ntlmv2;
1940 break;
1941 case Kerberos:
1942 #ifdef CONFIG_CIFS_UPCALL
1943 sess_data->func = sess_auth_kerberos;
1944 break;
1945 #else
1946 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1947 return -ENOSYS;
1948 #endif /* CONFIG_CIFS_UPCALL */
1949 case RawNTLMSSP:
1950 sess_data->func = sess_auth_rawntlmssp_negotiate;
1951 break;
1952 default:
1953 cifs_dbg(VFS, "secType %d not supported!\n", type);
1954 return -ENOSYS;
1957 return 0;
1960 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1961 struct TCP_Server_Info *server,
1962 const struct nls_table *nls_cp)
1964 int rc = 0;
1965 struct sess_data *sess_data;
1967 if (ses == NULL) {
1968 WARN(1, "%s: ses == NULL!", __func__);
1969 return -EINVAL;
1972 sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1973 if (!sess_data)
1974 return -ENOMEM;
1976 sess_data->xid = xid;
1977 sess_data->ses = ses;
1978 sess_data->server = server;
1979 sess_data->buf0_type = CIFS_NO_BUFFER;
1980 sess_data->nls_cp = (struct nls_table *) nls_cp;
1982 rc = select_sec(sess_data);
1983 if (rc)
1984 goto out;
1986 while (sess_data->func)
1987 sess_data->func(sess_data);
1989 /* Store result before we free sess_data */
1990 rc = sess_data->result;
1992 out:
1993 kfree_sensitive(sess_data);
1994 return rc;
1996 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */