1 // SPDX-License-Identifier: LGPL-2.1
4 * SMB/CIFS session setup handling routines
6 * Copyright (c) International Business Machines Corp., 2006, 2009
7 * Author(s): Steve French (sfrench@us.ibm.com)
13 #include "cifsproto.h"
14 #include "cifs_unicode.h"
15 #include "cifs_debug.h"
18 #include <linux/utsname.h>
19 #include <linux/slab.h>
20 #include <linux/version.h>
22 #include "cifs_spnego.h"
23 #include "smb2proto.h"
24 #include "fs_context.h"
27 cifs_ses_add_channel(struct cifs_ses
*ses
,
28 struct cifs_server_iface
*iface
);
31 is_server_using_iface(struct TCP_Server_Info
*server
,
32 struct cifs_server_iface
*iface
)
34 struct sockaddr_in
*i4
= (struct sockaddr_in
*)&iface
->sockaddr
;
35 struct sockaddr_in6
*i6
= (struct sockaddr_in6
*)&iface
->sockaddr
;
36 struct sockaddr_in
*s4
= (struct sockaddr_in
*)&server
->dstaddr
;
37 struct sockaddr_in6
*s6
= (struct sockaddr_in6
*)&server
->dstaddr
;
39 if (server
->dstaddr
.ss_family
!= iface
->sockaddr
.ss_family
)
41 if (server
->dstaddr
.ss_family
== AF_INET
) {
42 if (s4
->sin_addr
.s_addr
!= i4
->sin_addr
.s_addr
)
44 } else if (server
->dstaddr
.ss_family
== AF_INET6
) {
45 if (memcmp(&s6
->sin6_addr
, &i6
->sin6_addr
,
46 sizeof(i6
->sin6_addr
)) != 0)
49 /* unknown family.. */
55 bool is_ses_using_iface(struct cifs_ses
*ses
, struct cifs_server_iface
*iface
)
59 spin_lock(&ses
->chan_lock
);
60 for (i
= 0; i
< ses
->chan_count
; i
++) {
61 if (ses
->chans
[i
].iface
== iface
) {
62 spin_unlock(&ses
->chan_lock
);
66 spin_unlock(&ses
->chan_lock
);
70 /* channel helper functions. assumed that chan_lock is held by caller. */
73 cifs_ses_get_chan_index(struct cifs_ses
*ses
,
74 struct TCP_Server_Info
*server
)
78 /* 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
)
87 /* If we didn't find the channel, it is likely a bug */
89 cifs_dbg(VFS
, "unable to get chan index for server: 0x%llx",
91 return CIFS_INVAL_CHAN_INDEX
;
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
)
103 ses
->chans
[chan_index
].in_reconnect
= true;
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
)
115 ses
->chans
[chan_index
].in_reconnect
= false;
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
)
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
);
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
)
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
);
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
);
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
;
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
;
189 spin_unlock(&ses
->chan_lock
);
191 "ses already at max_channels (%zu), nothing to open\n",
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");
202 if (!(server
->capabilities
& SMB2_GLOBAL_CAP_MULTI_CHANNEL
)) {
203 spin_unlock(&ses
->chan_lock
);
204 cifs_server_dbg(VFS
, "no multichannel support\n");
207 spin_unlock(&ses
->chan_lock
);
212 if (tries
> 3*ses
->chan_max
) {
213 cifs_dbg(VFS
, "too many channel open attempts (%d channels left to open)\n",
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
);
227 iface
= list_first_entry(&ses
->iface_list
, struct cifs_server_iface
,
229 last_iface
= list_last_entry(&ses
->iface_list
, struct cifs_server_iface
,
231 iface_min_speed
= last_iface
->speed
;
233 list_for_each_entry_safe_from(iface
, niface
, &ses
->iface_list
,
235 /* do not mix rdma and non-rdma interfaces */
236 if (iface
->rdma_capable
!= ses
->server
->rdma
)
239 /* skip ifaces that are unusable */
240 if (!iface
->is_active
||
241 (is_ses_using_iface(ses
, iface
) &&
242 !iface
->rss_capable
))
245 /* check if we already allocated enough channels */
246 iface_weight
= iface
->speed
/ iface_min_speed
;
248 if (iface
->weight_fulfilled
>= iface_weight
)
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
);
259 cifs_dbg(VFS
, "failed to open extra channel on iface:%pIS rc=%d\n",
262 kref_put(&iface
->refcount
, release_iface
);
263 /* failure to add chan should increase weight */
264 iface
->weight_fulfilled
++;
268 iface
->num_channels
++;
269 iface
->weight_fulfilled
++;
270 cifs_dbg(VFS
, "successfully opened new channel on iface:%pIS\n",
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
);
283 spin_unlock(&ses
->iface_lock
);
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.
298 cifs_disable_secondary_channels(struct cifs_ses
*ses
)
301 struct TCP_Server_Info
*server
;
302 struct cifs_server_iface
*iface
;
304 spin_lock(&ses
->chan_lock
);
305 chan_count
= ses
->chan_count
;
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
);
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
);
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
);
347 spin_unlock(&ses
->chan_lock
);
350 /* update the iface for the channel if necessary. */
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
);
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
);
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
);
388 last_iface
= list_last_entry(&ses
->iface_list
, struct cifs_server_iface
,
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
) {
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
))
400 kref_get(&iface
->refcount
);
404 /* do not mix rdma and non-rdma interfaces */
405 if (iface
->rdma_capable
!= server
->rdma
)
408 if (!iface
->is_active
||
409 (is_ses_using_iface(ses
, iface
) &&
410 !iface
->rss_capable
)) {
414 /* check if we already allocated enough channels */
415 iface_weight
= iface
->speed
/ iface_min_speed
;
417 if (iface
->weight_fulfilled
>= iface_weight
)
420 kref_get(&iface
->refcount
);
424 if (list_entry_is_head(iface
, &ses
->iface_list
, iface_head
)) {
426 cifs_dbg(FYI
, "unable to find a suitable iface\n");
431 cifs_dbg(FYI
, "unable to get the interface matching: %pIS\n",
434 cifs_dbg(FYI
, "unable to find another interface to replace: %pIS\n",
435 &old_iface
->sockaddr
);
438 spin_unlock(&ses
->iface_lock
);
442 /* now drop the ref to the current iface */
444 cifs_dbg(FYI
, "replacing iface: %pIS with %pIS\n",
445 &old_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",
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
);
471 ses
->chans
[chan_index
].iface
= iface
;
472 spin_unlock(&ses
->chan_lock
);
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
;
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",
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",
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
505 * We only need the ctx here, so we can reuse memory from
506 * the session and server without caring about memory
509 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
515 /* Always make new connection for now (TODO?) */
516 ctx
->nosharesock
= true;
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
;
531 /* XXX: Use ses->server->hostname? */
532 len
= sizeof(unc_fmt
) + SERVER_NAME_LEN_WITH_NULL
;
533 ctx
->UNC
= kzalloc(len
, GFP_KERNEL
);
538 scnprintf(ctx
->UNC
, len
, unc_fmt
, ses
->ip_addr
);
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
);
574 spin_unlock(&ses
->chan_lock
);
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
);
594 cifs_dbg(VFS
, "%s: crypto alloc failed\n", __func__
);
595 mutex_unlock(&ses
->session_mutex
);
599 rc
= cifs_negotiate_protocol(xid
, ses
, chan
->server
);
601 rc
= cifs_setup_session(xid
, ses
, chan
->server
, ses
->local_nls
);
603 mutex_unlock(&ses
->session_mutex
);
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
);
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
);
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,
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
;
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
;
678 unicode_oslm_strings(char **pbcc_area
, const struct nls_table
*nls_cp
)
680 char *bcc_ptr
= *pbcc_area
;
683 /* Copy OS version */
684 bytes_ret
= cifs_strtoUTF16((__le16
*)bcc_ptr
, "Linux version ", 32,
686 bcc_ptr
+= 2 * bytes_ret
;
687 bytes_ret
= cifs_strtoUTF16((__le16
*) bcc_ptr
, init_utsname()->release
,
689 bcc_ptr
+= 2 * bytes_ret
;
690 bcc_ptr
+= 2; /* trailing null */
692 bytes_ret
= cifs_strtoUTF16((__le16
*) bcc_ptr
, CIFS_NETWORK_OPSYS
,
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
;
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
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
;
730 /* BB FIXME add check that strings less than 335 or will need to send as arrays */
733 if (ses
->user_name
== NULL
) {
734 /* null user mount */
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
;
757 /* BB what about null user mounts - check that we do this BB */
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;
765 /* else null user mount */
767 bcc_ptr
++; /* account for null termination */
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;
775 } /* else we send a null domain name so server will default to its own domain */
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
;
793 decode_unicode_ssetup(char **pbcc_area
, int bleft
, struct cifs_ses
*ses
,
794 const struct nls_table
*nls_cp
)
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;
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;
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
);
826 static void decode_ascii_ssetup(char **pbcc_area
, __u16 bleft
,
827 struct cifs_ses
*ses
,
828 const struct nls_table
*nls_cp
)
831 char *bcc_ptr
= *pbcc_area
;
833 cifs_dbg(FYI
, "decode sessetup ascii. bleft %d\n", bleft
);
835 len
= strnlen(bcc_ptr
, bleft
);
839 kfree(ses
->serverOS
);
841 ses
->serverOS
= kmalloc(len
+ 1, GFP_KERNEL
);
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");
852 len
= strnlen(bcc_ptr
, bleft
);
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;
867 len
= strnlen(bcc_ptr
, bleft
);
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
;
891 if (blob_len
< sizeof(CHALLENGE_MESSAGE
)) {
892 cifs_dbg(VFS
, "challenge blob len %d too small\n", blob_len
);
896 if (memcmp(pblob
->Signature
, "NTLMSSP", 8)) {
897 cifs_dbg(VFS
, "blob signature incorrect %s\n",
901 if (pblob
->MessageType
!= NtLmChallenge
) {
902 cifs_dbg(VFS
, "Incorrect message type %d\n",
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",
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__
);
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",
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",
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",
949 kfree_sensitive(ses
->auth_key
.response
);
950 ses
->auth_key
.response
= kmemdup(bcc_ptr
+ tioffset
, tilen
,
952 if (!ses
->auth_key
.response
) {
953 cifs_dbg(VFS
, "Challenge target info alloc failure\n");
956 ses
->auth_key
.len
= tilen
;
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;
968 sz
+= sizeof(__le16
) * strnlen(ses
->domainName
, CIFS_MAX_DOMAINNAME_LEN
);
970 sz
+= sizeof(__le16
);
973 sz
+= sizeof(__le16
) * strnlen(ses
->user_name
, CIFS_MAX_USERNAME_LEN
);
975 sz
+= sizeof(__le16
);
977 if (ses
->workstation_name
[0])
978 sz
+= sizeof(__le16
) * strnlen(ses
->workstation_name
,
979 ntlmssp_workstation_name_size(ses
));
981 sz
+= sizeof(__le16
);
986 static inline void cifs_security_buffer_from_str(SECURITY_BUFFER
*pbuf
,
989 unsigned char *pstart
,
990 unsigned char **pcur
,
991 const struct nls_table
*nls_cp
)
993 unsigned char *tmp
= pstart
;
1003 pbuf
->BufferOffset
= cpu_to_le32(*pcur
- pstart
);
1005 pbuf
->MaximumLength
= 0;
1006 *pcur
+= sizeof(__le16
);
1008 len
= cifs_strtoUTF16((__le16
*)*pcur
,
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
);
1020 /* BB Move to ntlmssp.c eventually */
1022 int build_ntlmssp_negotiate_blob(unsigned char **pbuffer
,
1024 struct cifs_ses
*ses
,
1025 struct TCP_Server_Info
*server
,
1026 const struct nls_table
*nls_cp
)
1029 NEGOTIATE_MESSAGE
*sec_blob
;
1034 len
= size_of_ntlmssp_blob(ses
, sizeof(NEGOTIATE_MESSAGE
));
1035 *pbuffer
= kmalloc(len
, GFP_KERNEL
);
1038 cifs_dbg(VFS
, "Error %d during NTLMSSP allocation\n", rc
);
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
,
1064 CIFS_MAX_DOMAINNAME_LEN
,
1068 cifs_security_buffer_from_str(&sec_blob
->WorkstationName
,
1070 CIFS_MAX_WORKSTATION_LEN
,
1074 *buflen
= tmp
- *pbuffer
;
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
,
1086 struct cifs_ses
*ses
,
1087 struct TCP_Server_Info
*server
,
1088 const struct nls_table
*nls_cp
)
1091 struct negotiate_message
*sec_blob
;
1096 len
= size_of_ntlmssp_blob(ses
, sizeof(struct negotiate_message
));
1097 *pbuffer
= kmalloc(len
, GFP_KERNEL
);
1100 cifs_dbg(VFS
, "Error %d during NTLMSSP allocation\n", rc
);
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
,
1131 CIFS_MAX_DOMAINNAME_LEN
,
1135 cifs_security_buffer_from_str(&sec_blob
->WorkstationName
,
1137 CIFS_MAX_WORKSTATION_LEN
,
1141 *buflen
= tmp
- *pbuffer
;
1142 setup_ntlm_smb3_neg_ret
:
1147 /* See MS-NLMP 2.2.1.3 */
1148 int build_ntlmssp_auth_blob(unsigned char **pbuffer
,
1150 struct cifs_ses
*ses
,
1151 struct TCP_Server_Info
*server
,
1152 const struct nls_table
*nls_cp
)
1155 AUTHENTICATE_MESSAGE
*sec_blob
;
1160 rc
= setup_ntlmv2_rsp(ses
, nls_cp
);
1162 cifs_dbg(VFS
, "Error %d during NTLMSSP authentication\n", rc
);
1164 goto setup_ntlmv2_ret
;
1167 len
= size_of_ntlmssp_blob(ses
, sizeof(AUTHENTICATE_MESSAGE
));
1168 *pbuffer
= kmalloc(len
, GFP_KERNEL
);
1171 cifs_dbg(VFS
, "Error %d during NTLMSSP allocation\n", rc
);
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
);
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
,
1219 CIFS_MAX_DOMAINNAME_LEN
,
1223 cifs_security_buffer_from_str(&sec_blob
->UserName
,
1225 CIFS_MAX_USERNAME_LEN
,
1229 cifs_security_buffer_from_str(&sec_blob
->WorkstationName
,
1230 ses
->workstation_name
,
1231 ntlmssp_workstation_name_size(ses
),
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
;
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
;
1256 cifs_select_sectype(struct TCP_Server_Info
*server
, enum securityEnum requested
)
1258 switch (server
->negflavor
) {
1259 case CIFS_NEGFLAVOR_EXTENDED
:
1260 switch (requested
) {
1265 if (server
->sec_ntlmssp
&&
1266 (global_secflags
& CIFSSEC_MAY_NTLMSSP
))
1268 if ((server
->sec_kerberos
|| server
->sec_mskerberos
) &&
1269 (global_secflags
& CIFSSEC_MAY_KRB5
))
1275 case CIFS_NEGFLAVOR_UNENCAP
:
1276 switch (requested
) {
1280 if (global_secflags
& CIFSSEC_MAY_NTLMV2
)
1294 struct cifs_ses
*ses
;
1295 struct TCP_Server_Info
*server
;
1296 struct nls_table
*nls_cp
;
1297 void (*func
)(struct sess_data
*);
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
1311 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1313 sess_alloc_buffer(struct sess_data
*sess_data
, int wct
)
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
,
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
) {
1337 goto 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
;
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
);
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
) {
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
);
1383 server
->session_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");
1396 sess_sendreceive(struct sess_data
*sess_data
)
1399 struct smb_hdr
*smb_buf
= (struct smb_hdr
*) sess_data
->iov
[0].iov_base
;
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
));
1418 sess_auth_ntlmv2(struct sess_data
*sess_data
)
1421 struct smb_hdr
*smb_buf
;
1422 SESSION_SETUP_ANDX
*pSMB
;
1424 struct cifs_ses
*ses
= sess_data
->ses
;
1425 struct TCP_Server_Info
*server
= sess_data
->server
;
1427 __u16 bytes_remaining
;
1429 /* old style NTLM sessionsetup */
1431 rc
= sess_alloc_buffer(sess_data
, 13);
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
);
1448 cifs_dbg(VFS
, "Error %d during NTLMv2 authentication\n", rc
);
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
);
1462 pSMB
->req_no_secext
.CaseSensitivePasswordLength
= 0;
1465 if (ses
->capabilities
& CAP_UNICODE
) {
1466 if (!IS_ALIGNED(sess_data
->iov
[0].iov_len
, 2)) {
1470 unicode_ssetup_strings(&bcc_ptr
, ses
, sess_data
->nls_cp
);
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
);
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) {
1488 cifs_dbg(VFS
, "bad word count %d\n", smb_buf
->WordCount
);
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)) {
1510 decode_unicode_ssetup(&bcc_ptr
, bytes_remaining
, ses
,
1513 decode_ascii_ssetup(&bcc_ptr
, bytes_remaining
, ses
,
1517 rc
= sess_establish_session(sess_data
);
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
1528 sess_auth_kerberos(struct sess_data
*sess_data
)
1531 struct smb_hdr
*smb_buf
;
1532 SESSION_SETUP_ANDX
*pSMB
;
1534 struct cifs_ses
*ses
= sess_data
->ses
;
1535 struct TCP_Server_Info
*server
= sess_data
->server
;
1537 __u16 bytes_remaining
;
1538 struct key
*spnego_key
= NULL
;
1539 struct cifs_spnego_msg
*msg
;
1542 /* extended security */
1544 rc
= sess_alloc_buffer(sess_data
, 12);
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
);
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
);
1568 goto out_put_spnego_key
;
1571 kfree_sensitive(ses
->auth_key
.response
);
1572 ses
->auth_key
.response
= kmemdup(msg
->data
, msg
->sesskey_len
,
1574 if (!ses
->auth_key
.response
) {
1575 cifs_dbg(VFS
, "Kerberos can't allocate (%u bytes) memory\n",
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)) {
1595 unicode_oslm_strings(&bcc_ptr
, sess_data
->nls_cp
);
1596 unicode_domain_string(&bcc_ptr
, ses
, sess_data
->nls_cp
);
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
);
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) {
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",
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)) {
1646 decode_unicode_ssetup(&bcc_ptr
, bytes_remaining
, ses
,
1649 decode_ascii_ssetup(&bcc_ptr
, bytes_remaining
, ses
,
1653 rc
= sess_establish_session(sess_data
);
1655 key_invalidate(spnego_key
);
1656 key_put(spnego_key
);
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
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
;
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");
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)) {
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
;
1707 sess_auth_rawntlmssp_authenticate(struct sess_data
*sess_data
);
1710 sess_auth_rawntlmssp_negotiate(struct sess_data
*sess_data
)
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
;
1719 unsigned char *ntlmsspblob
= NULL
;
1722 cifs_dbg(FYI
, "rawntlmssp session setup negotiate phase\n");
1725 * if memory allocation is successful, caller of this function
1728 ses
->ntlmssp
= kmalloc(sizeof(struct ntlmssp_auth
), GFP_KERNEL
);
1729 if (!ses
->ntlmssp
) {
1733 ses
->ntlmssp
->sesskey_per_smbsess
= false;
1736 rc
= sess_alloc_buffer(sess_data
, 12);
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
,
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
);
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
))
1769 goto out_free_ntlmsspblob
;
1771 cifs_dbg(FYI
, "rawntlmssp session setup challenge phase\n");
1773 if (smb_buf
->WordCount
!= 4) {
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",
1790 goto out_free_ntlmsspblob
;
1793 rc
= decode_ntlmssp_challenge(bcc_ptr
, blob_len
, ses
);
1795 out_free_ntlmsspblob
:
1796 kfree_sensitive(ntlmsspblob
);
1798 sess_free_buffer(sess_data
);
1801 sess_data
->func
= sess_auth_rawntlmssp_authenticate
;
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
;
1816 sess_auth_rawntlmssp_authenticate(struct sess_data
*sess_data
)
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
;
1825 unsigned char *ntlmsspblob
= NULL
;
1828 cifs_dbg(FYI
, "rawntlmssp session setup authenticate phase\n");
1831 rc
= sess_alloc_buffer(sess_data
, 12);
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
,
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
1851 smb_buf
->Uid
= ses
->Suid
;
1853 rc
= _sess_auth_rawntlmssp_assemble_req(sess_data
);
1855 goto out_free_ntlmsspblob
;
1857 rc
= sess_sendreceive(sess_data
);
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) {
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",
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)) {
1899 decode_unicode_ssetup(&bcc_ptr
, bytes_remaining
, ses
,
1902 decode_ascii_ssetup(&bcc_ptr
, bytes_remaining
, ses
,
1906 out_free_ntlmsspblob
:
1907 kfree_sensitive(ntlmsspblob
);
1909 sess_free_buffer(sess_data
);
1912 rc
= sess_establish_session(sess_data
);
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
)
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");
1939 sess_data
->func
= sess_auth_ntlmv2
;
1942 #ifdef CONFIG_CIFS_UPCALL
1943 sess_data
->func
= sess_auth_kerberos
;
1946 cifs_dbg(VFS
, "Kerberos negotiated but upcall support disabled!\n");
1948 #endif /* CONFIG_CIFS_UPCALL */
1950 sess_data
->func
= sess_auth_rawntlmssp_negotiate
;
1953 cifs_dbg(VFS
, "secType %d not supported!\n", type
);
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
)
1965 struct sess_data
*sess_data
;
1968 WARN(1, "%s: ses == NULL!", __func__
);
1972 sess_data
= kzalloc(sizeof(struct sess_data
), GFP_KERNEL
);
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
);
1986 while (sess_data
->func
)
1987 sess_data
->func(sess_data
);
1989 /* Store result before we free sess_data */
1990 rc
= sess_data
->result
;
1993 kfree_sensitive(sess_data
);
1996 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */