1ffdd7dadc55ae9f1df8a819dde533b35e651afc
[linux-2.6-microblaze.git] / fs / cifs / sess.c
1 /*
2  *   fs/cifs/sess.c
3  *
4  *   SMB/CIFS session setup handling routines
5  *
6  *   Copyright (c) International Business Machines  Corp., 2006, 2009
7  *   Author(s): Steve French (sfrench@us.ibm.com)
8  *
9  *   This library is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU Lesser General Public License as published
11  *   by the Free Software Foundation; either version 2.1 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This library is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
17  *   the GNU Lesser General Public License for more details.
18  *
19  *   You should have received a copy of the GNU Lesser General Public License
20  *   along with this library; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23
24 #include "cifspdu.h"
25 #include "cifsglob.h"
26 #include "cifsproto.h"
27 #include "cifs_unicode.h"
28 #include "cifs_debug.h"
29 #include "ntlmssp.h"
30 #include "nterr.h"
31 #include <linux/utsname.h>
32 #include <linux/slab.h>
33 #include "cifs_spnego.h"
34 #include "smb2proto.h"
35
36 bool
37 is_server_using_iface(struct TCP_Server_Info *server,
38                       struct cifs_server_iface *iface)
39 {
40         struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr;
41         struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr;
42         struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr;
43         struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr;
44
45         if (server->dstaddr.ss_family != iface->sockaddr.ss_family)
46                 return false;
47         if (server->dstaddr.ss_family == AF_INET) {
48                 if (s4->sin_addr.s_addr != i4->sin_addr.s_addr)
49                         return false;
50         } else if (server->dstaddr.ss_family == AF_INET6) {
51                 if (memcmp(&s6->sin6_addr, &i6->sin6_addr,
52                            sizeof(i6->sin6_addr)) != 0)
53                         return false;
54         } else {
55                 /* unknown family.. */
56                 return false;
57         }
58         return true;
59 }
60
61 bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface)
62 {
63         int i;
64
65         for (i = 0; i < ses->chan_count; i++) {
66                 if (is_server_using_iface(ses->chans[i].server, iface))
67                         return true;
68         }
69         return false;
70 }
71
72 /* returns number of channels added */
73 int cifs_try_adding_channels(struct cifs_ses *ses)
74 {
75         int old_chan_count = ses->chan_count;
76         int left = ses->chan_max - ses->chan_count;
77         int i = 0;
78         int rc = 0;
79         int tries = 0;
80         struct cifs_server_iface *ifaces = NULL;
81         size_t iface_count;
82
83         if (left <= 0) {
84                 cifs_dbg(FYI,
85                          "ses already at max_channels (%zu), nothing to open\n",
86                          ses->chan_max);
87                 return 0;
88         }
89
90         if (ses->server->dialect < SMB30_PROT_ID) {
91                 cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n");
92                 return 0;
93         }
94
95         /*
96          * Make a copy of the iface list at the time and use that
97          * instead so as to not hold the iface spinlock for opening
98          * channels
99          */
100         spin_lock(&ses->iface_lock);
101         iface_count = ses->iface_count;
102         if (iface_count <= 0) {
103                 spin_unlock(&ses->iface_lock);
104                 cifs_dbg(VFS, "no iface list available to open channels\n");
105                 return 0;
106         }
107         ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces),
108                          GFP_ATOMIC);
109         if (!ifaces) {
110                 spin_unlock(&ses->iface_lock);
111                 return 0;
112         }
113         spin_unlock(&ses->iface_lock);
114
115         /*
116          * Keep connecting to same, fastest, iface for all channels as
117          * long as its RSS. Try next fastest one if not RSS or channel
118          * creation fails.
119          */
120         while (left > 0) {
121                 struct cifs_server_iface *iface;
122
123                 tries++;
124                 if (tries > 3*ses->chan_max) {
125                         cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n",
126                                  left);
127                         break;
128                 }
129
130                 iface = &ifaces[i];
131                 if (is_ses_using_iface(ses, iface) && !iface->rss_capable) {
132                         i = (i+1) % iface_count;
133                         continue;
134                 }
135
136                 rc = cifs_ses_add_channel(ses, iface);
137                 if (rc) {
138                         cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n",
139                                  i, rc);
140                         i = (i+1) % iface_count;
141                         continue;
142                 }
143
144                 cifs_dbg(FYI, "successfully opened new channel on iface#%d\n",
145                          i);
146                 left--;
147         }
148
149         kfree(ifaces);
150         return ses->chan_count - old_chan_count;
151 }
152
153 int
154 cifs_ses_add_channel(struct cifs_ses *ses, struct cifs_server_iface *iface)
155 {
156         struct cifs_chan *chan;
157         struct smb_vol vol = {NULL};
158         static const char unc_fmt[] = "\\%s\\foo";
159         char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0};
160         struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
161         struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
162         int rc;
163         unsigned int xid = get_xid();
164
165         if (iface->sockaddr.ss_family == AF_INET)
166                 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
167                          ses, iface->speed, iface->rdma_capable ? "yes" : "no",
168                          &ipv4->sin_addr);
169         else
170                 cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n",
171                          ses, iface->speed, iface->rdma_capable ? "yes" : "no",
172                          &ipv6->sin6_addr);
173
174         /*
175          * Setup a smb_vol with mostly the same info as the existing
176          * session and overwrite it with the requested iface data.
177          *
178          * We need to setup at least the fields used for negprot and
179          * sesssetup.
180          *
181          * We only need the volume here, so we can reuse memory from
182          * the session and server without caring about memory
183          * management.
184          */
185
186         /* Always make new connection for now (TODO?) */
187         vol.nosharesock = true;
188
189         /* Auth */
190         vol.domainauto = ses->domainAuto;
191         vol.domainname = ses->domainName;
192         vol.username = ses->user_name;
193         vol.password = ses->password;
194         vol.sectype = ses->sectype;
195         vol.sign = ses->sign;
196
197         /* UNC and paths */
198         /* XXX: Use ses->server->hostname? */
199         sprintf(unc, unc_fmt, ses->serverName);
200         vol.UNC = unc;
201         vol.prepath = "";
202
203         /* Reuse same version as master connection */
204         vol.vals = ses->server->vals;
205         vol.ops = ses->server->ops;
206
207         vol.noblocksnd = ses->server->noblocksnd;
208         vol.noautotune = ses->server->noautotune;
209         vol.sockopt_tcp_nodelay = ses->server->tcp_nodelay;
210         vol.echo_interval = ses->server->echo_interval / HZ;
211
212         /*
213          * This will be used for encoding/decoding user/domain/pw
214          * during sess setup auth.
215          *
216          * XXX: We use the default for simplicity but the proper way
217          * would be to use the one that ses used, which is not
218          * stored. This might break when dealing with non-ascii
219          * strings.
220          */
221         vol.local_nls = load_nls_default();
222
223         /* Use RDMA if possible */
224         vol.rdma = iface->rdma_capable;
225         memcpy(&vol.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage));
226
227         /* reuse master con client guid */
228         memcpy(&vol.client_guid, ses->server->client_guid,
229                SMB2_CLIENT_GUID_SIZE);
230         vol.use_client_guid = true;
231
232         mutex_lock(&ses->session_mutex);
233
234         chan = ses->binding_chan = &ses->chans[ses->chan_count];
235         chan->server = cifs_get_tcp_session(&vol);
236         if (IS_ERR(chan->server)) {
237                 rc = PTR_ERR(chan->server);
238                 chan->server = NULL;
239                 goto out;
240         }
241         spin_lock(&cifs_tcp_ses_lock);
242         chan->server->is_channel = true;
243         spin_unlock(&cifs_tcp_ses_lock);
244
245         /*
246          * We need to allocate the server crypto now as we will need
247          * to sign packets before we generate the channel signing key
248          * (we sign with the session key)
249          */
250         rc = smb311_crypto_shash_allocate(chan->server);
251         if (rc) {
252                 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
253                 goto out;
254         }
255
256         ses->binding = true;
257         rc = cifs_negotiate_protocol(xid, ses);
258         if (rc)
259                 goto out;
260
261         rc = cifs_setup_session(xid, ses, vol.local_nls);
262         if (rc)
263                 goto out;
264
265         /* success, put it on the list
266          * XXX: sharing ses between 2 tcp servers is not possible, the
267          * way "internal" linked lists works in linux makes element
268          * only able to belong to one list
269          *
270          * the binding session is already established so the rest of
271          * the code should be able to look it up, no need to add the
272          * ses to the new server.
273          */
274
275         ses->chan_count++;
276         atomic_set(&ses->chan_seq, 0);
277 out:
278         ses->binding = false;
279         ses->binding_chan = NULL;
280         mutex_unlock(&ses->session_mutex);
281
282         if (rc && chan->server)
283                 cifs_put_tcp_session(chan->server, 0);
284         unload_nls(vol.local_nls);
285
286         return rc;
287 }
288
289 static __u32 cifs_ssetup_hdr(struct cifs_ses *ses, SESSION_SETUP_ANDX *pSMB)
290 {
291         __u32 capabilities = 0;
292
293         /* init fields common to all four types of SessSetup */
294         /* Note that offsets for first seven fields in req struct are same  */
295         /*      in CIFS Specs so does not matter which of 3 forms of struct */
296         /*      that we use in next few lines                               */
297         /* Note that header is initialized to zero in header_assemble */
298         pSMB->req.AndXCommand = 0xFF;
299         pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32,
300                                         CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4,
301                                         USHRT_MAX));
302         pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
303         pSMB->req.VcNumber = cpu_to_le16(1);
304
305         /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */
306
307         /* BB verify whether signing required on neg or just on auth frame
308            (and NTLM case) */
309
310         capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
311                         CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
312
313         if (ses->server->sign)
314                 pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
315
316         if (ses->capabilities & CAP_UNICODE) {
317                 pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE;
318                 capabilities |= CAP_UNICODE;
319         }
320         if (ses->capabilities & CAP_STATUS32) {
321                 pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS;
322                 capabilities |= CAP_STATUS32;
323         }
324         if (ses->capabilities & CAP_DFS) {
325                 pSMB->req.hdr.Flags2 |= SMBFLG2_DFS;
326                 capabilities |= CAP_DFS;
327         }
328         if (ses->capabilities & CAP_UNIX)
329                 capabilities |= CAP_UNIX;
330
331         return capabilities;
332 }
333
334 static void
335 unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp)
336 {
337         char *bcc_ptr = *pbcc_area;
338         int bytes_ret = 0;
339
340         /* Copy OS version */
341         bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32,
342                                     nls_cp);
343         bcc_ptr += 2 * bytes_ret;
344         bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release,
345                                     32, nls_cp);
346         bcc_ptr += 2 * bytes_ret;
347         bcc_ptr += 2; /* trailing null */
348
349         bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
350                                     32, nls_cp);
351         bcc_ptr += 2 * bytes_ret;
352         bcc_ptr += 2; /* trailing null */
353
354         *pbcc_area = bcc_ptr;
355 }
356
357 static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses,
358                                    const struct nls_table *nls_cp)
359 {
360         char *bcc_ptr = *pbcc_area;
361         int bytes_ret = 0;
362
363         /* copy domain */
364         if (ses->domainName == NULL) {
365                 /* Sending null domain better than using a bogus domain name (as
366                 we did briefly in 2.6.18) since server will use its default */
367                 *bcc_ptr = 0;
368                 *(bcc_ptr+1) = 0;
369                 bytes_ret = 0;
370         } else
371                 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName,
372                                             CIFS_MAX_DOMAINNAME_LEN, nls_cp);
373         bcc_ptr += 2 * bytes_ret;
374         bcc_ptr += 2;  /* account for null terminator */
375
376         *pbcc_area = bcc_ptr;
377 }
378
379
380 static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
381                                    const struct nls_table *nls_cp)
382 {
383         char *bcc_ptr = *pbcc_area;
384         int bytes_ret = 0;
385
386         /* BB FIXME add check that strings total less
387         than 335 or will need to send them as arrays */
388
389         /* unicode strings, must be word aligned before the call */
390 /*      if ((long) bcc_ptr % 2) {
391                 *bcc_ptr = 0;
392                 bcc_ptr++;
393         } */
394         /* copy user */
395         if (ses->user_name == NULL) {
396                 /* null user mount */
397                 *bcc_ptr = 0;
398                 *(bcc_ptr+1) = 0;
399         } else {
400                 bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name,
401                                             CIFS_MAX_USERNAME_LEN, nls_cp);
402         }
403         bcc_ptr += 2 * bytes_ret;
404         bcc_ptr += 2; /* account for null termination */
405
406         unicode_domain_string(&bcc_ptr, ses, nls_cp);
407         unicode_oslm_strings(&bcc_ptr, nls_cp);
408
409         *pbcc_area = bcc_ptr;
410 }
411
412 static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses,
413                                  const struct nls_table *nls_cp)
414 {
415         char *bcc_ptr = *pbcc_area;
416         int len;
417
418         /* copy user */
419         /* BB what about null user mounts - check that we do this BB */
420         /* copy user */
421         if (ses->user_name != NULL) {
422                 len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN);
423                 if (WARN_ON_ONCE(len < 0))
424                         len = CIFS_MAX_USERNAME_LEN - 1;
425                 bcc_ptr += len;
426         }
427         /* else null user mount */
428         *bcc_ptr = 0;
429         bcc_ptr++; /* account for null termination */
430
431         /* copy domain */
432         if (ses->domainName != NULL) {
433                 len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
434                 if (WARN_ON_ONCE(len < 0))
435                         len = CIFS_MAX_DOMAINNAME_LEN - 1;
436                 bcc_ptr += len;
437         } /* else we will send a null domain name
438              so the server will default to its own domain */
439         *bcc_ptr = 0;
440         bcc_ptr++;
441
442         /* BB check for overflow here */
443
444         strcpy(bcc_ptr, "Linux version ");
445         bcc_ptr += strlen("Linux version ");
446         strcpy(bcc_ptr, init_utsname()->release);
447         bcc_ptr += strlen(init_utsname()->release) + 1;
448
449         strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
450         bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
451
452         *pbcc_area = bcc_ptr;
453 }
454
455 static void
456 decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses,
457                       const struct nls_table *nls_cp)
458 {
459         int len;
460         char *data = *pbcc_area;
461
462         cifs_dbg(FYI, "bleft %d\n", bleft);
463
464         kfree(ses->serverOS);
465         ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
466         cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
467         len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
468         data += len;
469         bleft -= len;
470         if (bleft <= 0)
471                 return;
472
473         kfree(ses->serverNOS);
474         ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
475         cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
476         len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
477         data += len;
478         bleft -= len;
479         if (bleft <= 0)
480                 return;
481
482         kfree(ses->serverDomain);
483         ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp);
484         cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
485
486         return;
487 }
488
489 static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
490                                 struct cifs_ses *ses,
491                                 const struct nls_table *nls_cp)
492 {
493         int len;
494         char *bcc_ptr = *pbcc_area;
495
496         cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft);
497
498         len = strnlen(bcc_ptr, bleft);
499         if (len >= bleft)
500                 return;
501
502         kfree(ses->serverOS);
503
504         ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
505         if (ses->serverOS) {
506                 memcpy(ses->serverOS, bcc_ptr, len);
507                 ses->serverOS[len] = 0;
508                 if (strncmp(ses->serverOS, "OS/2", 4) == 0)
509                         cifs_dbg(FYI, "OS/2 server\n");
510         }
511
512         bcc_ptr += len + 1;
513         bleft -= len + 1;
514
515         len = strnlen(bcc_ptr, bleft);
516         if (len >= bleft)
517                 return;
518
519         kfree(ses->serverNOS);
520
521         ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
522         if (ses->serverNOS) {
523                 memcpy(ses->serverNOS, bcc_ptr, len);
524                 ses->serverNOS[len] = 0;
525         }
526
527         bcc_ptr += len + 1;
528         bleft -= len + 1;
529
530         len = strnlen(bcc_ptr, bleft);
531         if (len > bleft)
532                 return;
533
534         /* No domain field in LANMAN case. Domain is
535            returned by old servers in the SMB negprot response */
536         /* BB For newer servers which do not support Unicode,
537            but thus do return domain here we could add parsing
538            for it later, but it is not very important */
539         cifs_dbg(FYI, "ascii: bytes left %d\n", bleft);
540 }
541
542 int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len,
543                                     struct cifs_ses *ses)
544 {
545         unsigned int tioffset; /* challenge message target info area */
546         unsigned int tilen; /* challenge message target info area length  */
547
548         CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr;
549
550         if (blob_len < sizeof(CHALLENGE_MESSAGE)) {
551                 cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len);
552                 return -EINVAL;
553         }
554
555         if (memcmp(pblob->Signature, "NTLMSSP", 8)) {
556                 cifs_dbg(VFS, "blob signature incorrect %s\n",
557                          pblob->Signature);
558                 return -EINVAL;
559         }
560         if (pblob->MessageType != NtLmChallenge) {
561                 cifs_dbg(VFS, "Incorrect message type %d\n",
562                          pblob->MessageType);
563                 return -EINVAL;
564         }
565
566         memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE);
567         /* BB we could decode pblob->NegotiateFlags; some may be useful */
568         /* In particular we can examine sign flags */
569         /* BB spec says that if AvId field of MsvAvTimestamp is populated then
570                 we must set the MIC field of the AUTHENTICATE_MESSAGE */
571         ses->ntlmssp->server_flags = le32_to_cpu(pblob->NegotiateFlags);
572         tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset);
573         tilen = le16_to_cpu(pblob->TargetInfoArray.Length);
574         if (tioffset > blob_len || tioffset + tilen > blob_len) {
575                 cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n",
576                          tioffset, tilen);
577                 return -EINVAL;
578         }
579         if (tilen) {
580                 ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
581                                                  GFP_KERNEL);
582                 if (!ses->auth_key.response) {
583                         cifs_dbg(VFS, "Challenge target info alloc failure\n");
584                         return -ENOMEM;
585                 }
586                 ses->auth_key.len = tilen;
587         }
588
589         return 0;
590 }
591
592 /* BB Move to ntlmssp.c eventually */
593
594 /* We do not malloc the blob, it is passed in pbuffer, because
595    it is fixed size, and small, making this approach cleaner */
596 void build_ntlmssp_negotiate_blob(unsigned char *pbuffer,
597                                          struct cifs_ses *ses)
598 {
599         struct TCP_Server_Info *server = cifs_ses_server(ses);
600         NEGOTIATE_MESSAGE *sec_blob = (NEGOTIATE_MESSAGE *)pbuffer;
601         __u32 flags;
602
603         memset(pbuffer, 0, sizeof(NEGOTIATE_MESSAGE));
604         memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
605         sec_blob->MessageType = NtLmNegotiate;
606
607         /* BB is NTLMV2 session security format easier to use here? */
608         flags = NTLMSSP_NEGOTIATE_56 |  NTLMSSP_REQUEST_TARGET |
609                 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
610                 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
611                 NTLMSSP_NEGOTIATE_SEAL;
612         if (server->sign)
613                 flags |= NTLMSSP_NEGOTIATE_SIGN;
614         if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
615                 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
616
617         sec_blob->NegotiateFlags = cpu_to_le32(flags);
618
619         sec_blob->WorkstationName.BufferOffset = 0;
620         sec_blob->WorkstationName.Length = 0;
621         sec_blob->WorkstationName.MaximumLength = 0;
622
623         /* Domain name is sent on the Challenge not Negotiate NTLMSSP request */
624         sec_blob->DomainName.BufferOffset = 0;
625         sec_blob->DomainName.Length = 0;
626         sec_blob->DomainName.MaximumLength = 0;
627 }
628
629 static int size_of_ntlmssp_blob(struct cifs_ses *ses)
630 {
631         int sz = sizeof(AUTHENTICATE_MESSAGE) + ses->auth_key.len
632                 - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2;
633
634         if (ses->domainName)
635                 sz += 2 * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN);
636         else
637                 sz += 2;
638
639         if (ses->user_name)
640                 sz += 2 * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN);
641         else
642                 sz += 2;
643
644         return sz;
645 }
646
647 int build_ntlmssp_auth_blob(unsigned char **pbuffer,
648                                         u16 *buflen,
649                                    struct cifs_ses *ses,
650                                    const struct nls_table *nls_cp)
651 {
652         int rc;
653         AUTHENTICATE_MESSAGE *sec_blob;
654         __u32 flags;
655         unsigned char *tmp;
656
657         rc = setup_ntlmv2_rsp(ses, nls_cp);
658         if (rc) {
659                 cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc);
660                 *buflen = 0;
661                 goto setup_ntlmv2_ret;
662         }
663         *pbuffer = kmalloc(size_of_ntlmssp_blob(ses), GFP_KERNEL);
664         if (!*pbuffer) {
665                 rc = -ENOMEM;
666                 cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc);
667                 *buflen = 0;
668                 goto setup_ntlmv2_ret;
669         }
670         sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer;
671
672         memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8);
673         sec_blob->MessageType = NtLmAuthenticate;
674
675         flags = NTLMSSP_NEGOTIATE_56 |
676                 NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_TARGET_INFO |
677                 NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE |
678                 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC |
679                 NTLMSSP_NEGOTIATE_SEAL;
680         if (ses->server->sign)
681                 flags |= NTLMSSP_NEGOTIATE_SIGN;
682         if (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess)
683                 flags |= NTLMSSP_NEGOTIATE_KEY_XCH;
684
685         tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE);
686         sec_blob->NegotiateFlags = cpu_to_le32(flags);
687
688         sec_blob->LmChallengeResponse.BufferOffset =
689                                 cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE));
690         sec_blob->LmChallengeResponse.Length = 0;
691         sec_blob->LmChallengeResponse.MaximumLength = 0;
692
693         sec_blob->NtChallengeResponse.BufferOffset =
694                                 cpu_to_le32(tmp - *pbuffer);
695         if (ses->user_name != NULL) {
696                 memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
697                                 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
698                 tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
699
700                 sec_blob->NtChallengeResponse.Length =
701                                 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
702                 sec_blob->NtChallengeResponse.MaximumLength =
703                                 cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
704         } else {
705                 /*
706                  * don't send an NT Response for anonymous access
707                  */
708                 sec_blob->NtChallengeResponse.Length = 0;
709                 sec_blob->NtChallengeResponse.MaximumLength = 0;
710         }
711
712         if (ses->domainName == NULL) {
713                 sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
714                 sec_blob->DomainName.Length = 0;
715                 sec_blob->DomainName.MaximumLength = 0;
716                 tmp += 2;
717         } else {
718                 int len;
719                 len = cifs_strtoUTF16((__le16 *)tmp, ses->domainName,
720                                       CIFS_MAX_DOMAINNAME_LEN, nls_cp);
721                 len *= 2; /* unicode is 2 bytes each */
722                 sec_blob->DomainName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
723                 sec_blob->DomainName.Length = cpu_to_le16(len);
724                 sec_blob->DomainName.MaximumLength = cpu_to_le16(len);
725                 tmp += len;
726         }
727
728         if (ses->user_name == NULL) {
729                 sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
730                 sec_blob->UserName.Length = 0;
731                 sec_blob->UserName.MaximumLength = 0;
732                 tmp += 2;
733         } else {
734                 int len;
735                 len = cifs_strtoUTF16((__le16 *)tmp, ses->user_name,
736                                       CIFS_MAX_USERNAME_LEN, nls_cp);
737                 len *= 2; /* unicode is 2 bytes each */
738                 sec_blob->UserName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
739                 sec_blob->UserName.Length = cpu_to_le16(len);
740                 sec_blob->UserName.MaximumLength = cpu_to_le16(len);
741                 tmp += len;
742         }
743
744         sec_blob->WorkstationName.BufferOffset = cpu_to_le32(tmp - *pbuffer);
745         sec_blob->WorkstationName.Length = 0;
746         sec_blob->WorkstationName.MaximumLength = 0;
747         tmp += 2;
748
749         if (((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) ||
750                 (ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC))
751                         && !calc_seckey(ses)) {
752                 memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE);
753                 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
754                 sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE);
755                 sec_blob->SessionKey.MaximumLength =
756                                 cpu_to_le16(CIFS_CPHTXT_SIZE);
757                 tmp += CIFS_CPHTXT_SIZE;
758         } else {
759                 sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer);
760                 sec_blob->SessionKey.Length = 0;
761                 sec_blob->SessionKey.MaximumLength = 0;
762         }
763
764         *buflen = tmp - *pbuffer;
765 setup_ntlmv2_ret:
766         return rc;
767 }
768
769 enum securityEnum
770 cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
771 {
772         switch (server->negflavor) {
773         case CIFS_NEGFLAVOR_EXTENDED:
774                 switch (requested) {
775                 case Kerberos:
776                 case RawNTLMSSP:
777                         return requested;
778                 case Unspecified:
779                         if (server->sec_ntlmssp &&
780                             (global_secflags & CIFSSEC_MAY_NTLMSSP))
781                                 return RawNTLMSSP;
782                         if ((server->sec_kerberos || server->sec_mskerberos) &&
783                             (global_secflags & CIFSSEC_MAY_KRB5))
784                                 return Kerberos;
785                         /* Fallthrough */
786                 default:
787                         return Unspecified;
788                 }
789         case CIFS_NEGFLAVOR_UNENCAP:
790                 switch (requested) {
791                 case NTLM:
792                 case NTLMv2:
793                         return requested;
794                 case Unspecified:
795                         if (global_secflags & CIFSSEC_MAY_NTLMV2)
796                                 return NTLMv2;
797                         if (global_secflags & CIFSSEC_MAY_NTLM)
798                                 return NTLM;
799                 default:
800                         break;
801                 }
802                 /* Fallthrough - to attempt LANMAN authentication next */
803         case CIFS_NEGFLAVOR_LANMAN:
804                 switch (requested) {
805                 case LANMAN:
806                         return requested;
807                 case Unspecified:
808                         if (global_secflags & CIFSSEC_MAY_LANMAN)
809                                 return LANMAN;
810                         /* Fallthrough */
811                 default:
812                         return Unspecified;
813                 }
814         default:
815                 return Unspecified;
816         }
817 }
818
819 struct sess_data {
820         unsigned int xid;
821         struct cifs_ses *ses;
822         struct nls_table *nls_cp;
823         void (*func)(struct sess_data *);
824         int result;
825
826         /* we will send the SMB in three pieces:
827          * a fixed length beginning part, an optional
828          * SPNEGO blob (which can be zero length), and a
829          * last part which will include the strings
830          * and rest of bcc area. This allows us to avoid
831          * a large buffer 17K allocation
832          */
833         int buf0_type;
834         struct kvec iov[3];
835 };
836
837 static int
838 sess_alloc_buffer(struct sess_data *sess_data, int wct)
839 {
840         int rc;
841         struct cifs_ses *ses = sess_data->ses;
842         struct smb_hdr *smb_buf;
843
844         rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses,
845                                   (void **)&smb_buf);
846
847         if (rc)
848                 return rc;
849
850         sess_data->iov[0].iov_base = (char *)smb_buf;
851         sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4;
852         /*
853          * This variable will be used to clear the buffer
854          * allocated above in case of any error in the calling function.
855          */
856         sess_data->buf0_type = CIFS_SMALL_BUFFER;
857
858         /* 2000 big enough to fit max user, domain, NOS name etc. */
859         sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL);
860         if (!sess_data->iov[2].iov_base) {
861                 rc = -ENOMEM;
862                 goto out_free_smb_buf;
863         }
864
865         return 0;
866
867 out_free_smb_buf:
868         kfree(smb_buf);
869         sess_data->iov[0].iov_base = NULL;
870         sess_data->iov[0].iov_len = 0;
871         sess_data->buf0_type = CIFS_NO_BUFFER;
872         return rc;
873 }
874
875 static void
876 sess_free_buffer(struct sess_data *sess_data)
877 {
878
879         free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base);
880         sess_data->buf0_type = CIFS_NO_BUFFER;
881         kfree(sess_data->iov[2].iov_base);
882 }
883
884 static int
885 sess_establish_session(struct sess_data *sess_data)
886 {
887         struct cifs_ses *ses = sess_data->ses;
888
889         mutex_lock(&ses->server->srv_mutex);
890         if (!ses->server->session_estab) {
891                 if (ses->server->sign) {
892                         ses->server->session_key.response =
893                                 kmemdup(ses->auth_key.response,
894                                 ses->auth_key.len, GFP_KERNEL);
895                         if (!ses->server->session_key.response) {
896                                 mutex_unlock(&ses->server->srv_mutex);
897                                 return -ENOMEM;
898                         }
899                         ses->server->session_key.len =
900                                                 ses->auth_key.len;
901                 }
902                 ses->server->sequence_number = 0x2;
903                 ses->server->session_estab = true;
904         }
905         mutex_unlock(&ses->server->srv_mutex);
906
907         cifs_dbg(FYI, "CIFS session established successfully\n");
908         spin_lock(&GlobalMid_Lock);
909         ses->status = CifsGood;
910         ses->need_reconnect = false;
911         spin_unlock(&GlobalMid_Lock);
912
913         return 0;
914 }
915
916 static int
917 sess_sendreceive(struct sess_data *sess_data)
918 {
919         int rc;
920         struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base;
921         __u16 count;
922         struct kvec rsp_iov = { NULL, 0 };
923
924         count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len;
925         smb_buf->smb_buf_length =
926                 cpu_to_be32(be32_to_cpu(smb_buf->smb_buf_length) + count);
927         put_bcc(count, smb_buf);
928
929         rc = SendReceive2(sess_data->xid, sess_data->ses,
930                           sess_data->iov, 3 /* num_iovecs */,
931                           &sess_data->buf0_type,
932                           CIFS_LOG_ERROR, &rsp_iov);
933         cifs_small_buf_release(sess_data->iov[0].iov_base);
934         memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
935
936         return rc;
937 }
938
939 /*
940  * LANMAN and plaintext are less secure and off by default.
941  * So we make this explicitly be turned on in kconfig (in the
942  * build) and turned on at runtime (changed from the default)
943  * in proc/fs/cifs or via mount parm.  Unfortunately this is
944  * needed for old Win (e.g. Win95), some obscure NAS and OS/2
945  */
946 #ifdef CONFIG_CIFS_WEAK_PW_HASH
947 static void
948 sess_auth_lanman(struct sess_data *sess_data)
949 {
950         int rc = 0;
951         struct smb_hdr *smb_buf;
952         SESSION_SETUP_ANDX *pSMB;
953         char *bcc_ptr;
954         struct cifs_ses *ses = sess_data->ses;
955         char lnm_session_key[CIFS_AUTH_RESP_SIZE];
956         __u16 bytes_remaining;
957
958         /* lanman 2 style sessionsetup */
959         /* wct = 10 */
960         rc = sess_alloc_buffer(sess_data, 10);
961         if (rc)
962                 goto out;
963
964         pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
965         bcc_ptr = sess_data->iov[2].iov_base;
966         (void)cifs_ssetup_hdr(ses, pSMB);
967
968         pSMB->req.hdr.Flags2 &= ~SMBFLG2_UNICODE;
969
970         if (ses->user_name != NULL) {
971                 /* no capabilities flags in old lanman negotiation */
972                 pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
973
974                 /* Calculate hash with password and copy into bcc_ptr.
975                  * Encryption Key (stored as in cryptkey) gets used if the
976                  * security mode bit in Negotiate Protocol response states
977                  * to use challenge/response method (i.e. Password bit is 1).
978                  */
979                 rc = calc_lanman_hash(ses->password, ses->server->cryptkey,
980                                       ses->server->sec_mode & SECMODE_PW_ENCRYPT ?
981                                       true : false, lnm_session_key);
982                 if (rc)
983                         goto out;
984
985                 memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_AUTH_RESP_SIZE);
986                 bcc_ptr += CIFS_AUTH_RESP_SIZE;
987         } else {
988                 pSMB->old_req.PasswordLength = 0;
989         }
990
991         /*
992          * can not sign if LANMAN negotiated so no need
993          * to calculate signing key? but what if server
994          * changed to do higher than lanman dialect and
995          * we reconnected would we ever calc signing_key?
996          */
997
998         cifs_dbg(FYI, "Negotiating LANMAN setting up strings\n");
999         /* Unicode not allowed for LANMAN dialects */
1000         ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1001
1002         sess_data->iov[2].iov_len = (long) bcc_ptr -
1003                         (long) sess_data->iov[2].iov_base;
1004
1005         rc = sess_sendreceive(sess_data);
1006         if (rc)
1007                 goto out;
1008
1009         pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1010         smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1011
1012         /* lanman response has a word count of 3 */
1013         if (smb_buf->WordCount != 3) {
1014                 rc = -EIO;
1015                 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1016                 goto out;
1017         }
1018
1019         if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1020                 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1021
1022         ses->Suid = smb_buf->Uid;   /* UID left in wire format (le) */
1023         cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1024
1025         bytes_remaining = get_bcc(smb_buf);
1026         bcc_ptr = pByteArea(smb_buf);
1027
1028         /* BB check if Unicode and decode strings */
1029         if (bytes_remaining == 0) {
1030                 /* no string area to decode, do nothing */
1031         } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1032                 /* unicode string area must be word-aligned */
1033                 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1034                         ++bcc_ptr;
1035                         --bytes_remaining;
1036                 }
1037                 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1038                                       sess_data->nls_cp);
1039         } else {
1040                 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1041                                     sess_data->nls_cp);
1042         }
1043
1044         rc = sess_establish_session(sess_data);
1045 out:
1046         sess_data->result = rc;
1047         sess_data->func = NULL;
1048         sess_free_buffer(sess_data);
1049 }
1050
1051 #endif
1052
1053 static void
1054 sess_auth_ntlm(struct sess_data *sess_data)
1055 {
1056         int rc = 0;
1057         struct smb_hdr *smb_buf;
1058         SESSION_SETUP_ANDX *pSMB;
1059         char *bcc_ptr;
1060         struct cifs_ses *ses = sess_data->ses;
1061         __u32 capabilities;
1062         __u16 bytes_remaining;
1063
1064         /* old style NTLM sessionsetup */
1065         /* wct = 13 */
1066         rc = sess_alloc_buffer(sess_data, 13);
1067         if (rc)
1068                 goto out;
1069
1070         pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1071         bcc_ptr = sess_data->iov[2].iov_base;
1072         capabilities = cifs_ssetup_hdr(ses, pSMB);
1073
1074         pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1075         if (ses->user_name != NULL) {
1076                 pSMB->req_no_secext.CaseInsensitivePasswordLength =
1077                                 cpu_to_le16(CIFS_AUTH_RESP_SIZE);
1078                 pSMB->req_no_secext.CaseSensitivePasswordLength =
1079                                 cpu_to_le16(CIFS_AUTH_RESP_SIZE);
1080
1081                 /* calculate ntlm response and session key */
1082                 rc = setup_ntlm_response(ses, sess_data->nls_cp);
1083                 if (rc) {
1084                         cifs_dbg(VFS, "Error %d during NTLM authentication\n",
1085                                          rc);
1086                         goto out;
1087                 }
1088
1089                 /* copy ntlm response */
1090                 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1091                                 CIFS_AUTH_RESP_SIZE);
1092                 bcc_ptr += CIFS_AUTH_RESP_SIZE;
1093                 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1094                                 CIFS_AUTH_RESP_SIZE);
1095                 bcc_ptr += CIFS_AUTH_RESP_SIZE;
1096         } else {
1097                 pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1098                 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1099         }
1100
1101         if (ses->capabilities & CAP_UNICODE) {
1102                 /* unicode strings must be word aligned */
1103                 if (sess_data->iov[0].iov_len % 2) {
1104                         *bcc_ptr = 0;
1105                         bcc_ptr++;
1106                 }
1107                 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1108         } else {
1109                 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1110         }
1111
1112
1113         sess_data->iov[2].iov_len = (long) bcc_ptr -
1114                         (long) sess_data->iov[2].iov_base;
1115
1116         rc = sess_sendreceive(sess_data);
1117         if (rc)
1118                 goto out;
1119
1120         pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1121         smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1122
1123         if (smb_buf->WordCount != 3) {
1124                 rc = -EIO;
1125                 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1126                 goto out;
1127         }
1128
1129         if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1130                 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1131
1132         ses->Suid = smb_buf->Uid;   /* UID left in wire format (le) */
1133         cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1134
1135         bytes_remaining = get_bcc(smb_buf);
1136         bcc_ptr = pByteArea(smb_buf);
1137
1138         /* BB check if Unicode and decode strings */
1139         if (bytes_remaining == 0) {
1140                 /* no string area to decode, do nothing */
1141         } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1142                 /* unicode string area must be word-aligned */
1143                 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1144                         ++bcc_ptr;
1145                         --bytes_remaining;
1146                 }
1147                 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1148                                       sess_data->nls_cp);
1149         } else {
1150                 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1151                                     sess_data->nls_cp);
1152         }
1153
1154         rc = sess_establish_session(sess_data);
1155 out:
1156         sess_data->result = rc;
1157         sess_data->func = NULL;
1158         sess_free_buffer(sess_data);
1159         kfree(ses->auth_key.response);
1160         ses->auth_key.response = NULL;
1161 }
1162
1163 static void
1164 sess_auth_ntlmv2(struct sess_data *sess_data)
1165 {
1166         int rc = 0;
1167         struct smb_hdr *smb_buf;
1168         SESSION_SETUP_ANDX *pSMB;
1169         char *bcc_ptr;
1170         struct cifs_ses *ses = sess_data->ses;
1171         __u32 capabilities;
1172         __u16 bytes_remaining;
1173
1174         /* old style NTLM sessionsetup */
1175         /* wct = 13 */
1176         rc = sess_alloc_buffer(sess_data, 13);
1177         if (rc)
1178                 goto out;
1179
1180         pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1181         bcc_ptr = sess_data->iov[2].iov_base;
1182         capabilities = cifs_ssetup_hdr(ses, pSMB);
1183
1184         pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
1185
1186         /* LM2 password would be here if we supported it */
1187         pSMB->req_no_secext.CaseInsensitivePasswordLength = 0;
1188
1189         if (ses->user_name != NULL) {
1190                 /* calculate nlmv2 response and session key */
1191                 rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp);
1192                 if (rc) {
1193                         cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc);
1194                         goto out;
1195                 }
1196
1197                 memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE,
1198                                 ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1199                 bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE;
1200
1201                 /* set case sensitive password length after tilen may get
1202                  * assigned, tilen is 0 otherwise.
1203                  */
1204                 pSMB->req_no_secext.CaseSensitivePasswordLength =
1205                         cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE);
1206         } else {
1207                 pSMB->req_no_secext.CaseSensitivePasswordLength = 0;
1208         }
1209
1210         if (ses->capabilities & CAP_UNICODE) {
1211                 if (sess_data->iov[0].iov_len % 2) {
1212                         *bcc_ptr = 0;
1213                         bcc_ptr++;
1214                 }
1215                 unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1216         } else {
1217                 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1218         }
1219
1220
1221         sess_data->iov[2].iov_len = (long) bcc_ptr -
1222                         (long) sess_data->iov[2].iov_base;
1223
1224         rc = sess_sendreceive(sess_data);
1225         if (rc)
1226                 goto out;
1227
1228         pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1229         smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1230
1231         if (smb_buf->WordCount != 3) {
1232                 rc = -EIO;
1233                 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1234                 goto out;
1235         }
1236
1237         if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1238                 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1239
1240         ses->Suid = smb_buf->Uid;   /* UID left in wire format (le) */
1241         cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1242
1243         bytes_remaining = get_bcc(smb_buf);
1244         bcc_ptr = pByteArea(smb_buf);
1245
1246         /* BB check if Unicode and decode strings */
1247         if (bytes_remaining == 0) {
1248                 /* no string area to decode, do nothing */
1249         } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1250                 /* unicode string area must be word-aligned */
1251                 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1252                         ++bcc_ptr;
1253                         --bytes_remaining;
1254                 }
1255                 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1256                                       sess_data->nls_cp);
1257         } else {
1258                 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1259                                     sess_data->nls_cp);
1260         }
1261
1262         rc = sess_establish_session(sess_data);
1263 out:
1264         sess_data->result = rc;
1265         sess_data->func = NULL;
1266         sess_free_buffer(sess_data);
1267         kfree(ses->auth_key.response);
1268         ses->auth_key.response = NULL;
1269 }
1270
1271 #ifdef CONFIG_CIFS_UPCALL
1272 static void
1273 sess_auth_kerberos(struct sess_data *sess_data)
1274 {
1275         int rc = 0;
1276         struct smb_hdr *smb_buf;
1277         SESSION_SETUP_ANDX *pSMB;
1278         char *bcc_ptr;
1279         struct cifs_ses *ses = sess_data->ses;
1280         __u32 capabilities;
1281         __u16 bytes_remaining;
1282         struct key *spnego_key = NULL;
1283         struct cifs_spnego_msg *msg;
1284         u16 blob_len;
1285
1286         /* extended security */
1287         /* wct = 12 */
1288         rc = sess_alloc_buffer(sess_data, 12);
1289         if (rc)
1290                 goto out;
1291
1292         pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1293         bcc_ptr = sess_data->iov[2].iov_base;
1294         capabilities = cifs_ssetup_hdr(ses, pSMB);
1295
1296         spnego_key = cifs_get_spnego_key(ses);
1297         if (IS_ERR(spnego_key)) {
1298                 rc = PTR_ERR(spnego_key);
1299                 spnego_key = NULL;
1300                 goto out;
1301         }
1302
1303         msg = spnego_key->payload.data[0];
1304         /*
1305          * check version field to make sure that cifs.upcall is
1306          * sending us a response in an expected form
1307          */
1308         if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
1309                 cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n",
1310                          CIFS_SPNEGO_UPCALL_VERSION, msg->version);
1311                 rc = -EKEYREJECTED;
1312                 goto out_put_spnego_key;
1313         }
1314
1315         ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
1316                                          GFP_KERNEL);
1317         if (!ses->auth_key.response) {
1318                 cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
1319                          msg->sesskey_len);
1320                 rc = -ENOMEM;
1321                 goto out_put_spnego_key;
1322         }
1323         ses->auth_key.len = msg->sesskey_len;
1324
1325         pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1326         capabilities |= CAP_EXTENDED_SECURITY;
1327         pSMB->req.Capabilities = cpu_to_le32(capabilities);
1328         sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
1329         sess_data->iov[1].iov_len = msg->secblob_len;
1330         pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len);
1331
1332         if (ses->capabilities & CAP_UNICODE) {
1333                 /* unicode strings must be word aligned */
1334                 if ((sess_data->iov[0].iov_len
1335                         + sess_data->iov[1].iov_len) % 2) {
1336                         *bcc_ptr = 0;
1337                         bcc_ptr++;
1338                 }
1339                 unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1340                 unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp);
1341         } else {
1342                 /* BB: is this right? */
1343                 ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp);
1344         }
1345
1346         sess_data->iov[2].iov_len = (long) bcc_ptr -
1347                         (long) sess_data->iov[2].iov_base;
1348
1349         rc = sess_sendreceive(sess_data);
1350         if (rc)
1351                 goto out_put_spnego_key;
1352
1353         pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1354         smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1355
1356         if (smb_buf->WordCount != 4) {
1357                 rc = -EIO;
1358                 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1359                 goto out_put_spnego_key;
1360         }
1361
1362         if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1363                 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1364
1365         ses->Suid = smb_buf->Uid;   /* UID left in wire format (le) */
1366         cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1367
1368         bytes_remaining = get_bcc(smb_buf);
1369         bcc_ptr = pByteArea(smb_buf);
1370
1371         blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1372         if (blob_len > bytes_remaining) {
1373                 cifs_dbg(VFS, "bad security blob length %d\n",
1374                                 blob_len);
1375                 rc = -EINVAL;
1376                 goto out_put_spnego_key;
1377         }
1378         bcc_ptr += blob_len;
1379         bytes_remaining -= blob_len;
1380
1381         /* BB check if Unicode and decode strings */
1382         if (bytes_remaining == 0) {
1383                 /* no string area to decode, do nothing */
1384         } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1385                 /* unicode string area must be word-aligned */
1386                 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1387                         ++bcc_ptr;
1388                         --bytes_remaining;
1389                 }
1390                 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1391                                       sess_data->nls_cp);
1392         } else {
1393                 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1394                                     sess_data->nls_cp);
1395         }
1396
1397         rc = sess_establish_session(sess_data);
1398 out_put_spnego_key:
1399         key_invalidate(spnego_key);
1400         key_put(spnego_key);
1401 out:
1402         sess_data->result = rc;
1403         sess_data->func = NULL;
1404         sess_free_buffer(sess_data);
1405         kfree(ses->auth_key.response);
1406         ses->auth_key.response = NULL;
1407 }
1408
1409 #endif /* ! CONFIG_CIFS_UPCALL */
1410
1411 /*
1412  * The required kvec buffers have to be allocated before calling this
1413  * function.
1414  */
1415 static int
1416 _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data)
1417 {
1418         SESSION_SETUP_ANDX *pSMB;
1419         struct cifs_ses *ses = sess_data->ses;
1420         __u32 capabilities;
1421         char *bcc_ptr;
1422
1423         pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1424
1425         capabilities = cifs_ssetup_hdr(ses, pSMB);
1426         if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) {
1427                 cifs_dbg(VFS, "NTLMSSP requires Unicode support\n");
1428                 return -ENOSYS;
1429         }
1430
1431         pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
1432         capabilities |= CAP_EXTENDED_SECURITY;
1433         pSMB->req.Capabilities |= cpu_to_le32(capabilities);
1434
1435         bcc_ptr = sess_data->iov[2].iov_base;
1436         /* unicode strings must be word aligned */
1437         if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) {
1438                 *bcc_ptr = 0;
1439                 bcc_ptr++;
1440         }
1441         unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp);
1442
1443         sess_data->iov[2].iov_len = (long) bcc_ptr -
1444                                         (long) sess_data->iov[2].iov_base;
1445
1446         return 0;
1447 }
1448
1449 static void
1450 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data);
1451
1452 static void
1453 sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data)
1454 {
1455         int rc;
1456         struct smb_hdr *smb_buf;
1457         SESSION_SETUP_ANDX *pSMB;
1458         struct cifs_ses *ses = sess_data->ses;
1459         __u16 bytes_remaining;
1460         char *bcc_ptr;
1461         u16 blob_len;
1462
1463         cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n");
1464
1465         /*
1466          * if memory allocation is successful, caller of this function
1467          * frees it.
1468          */
1469         ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
1470         if (!ses->ntlmssp) {
1471                 rc = -ENOMEM;
1472                 goto out;
1473         }
1474         ses->ntlmssp->sesskey_per_smbsess = false;
1475
1476         /* wct = 12 */
1477         rc = sess_alloc_buffer(sess_data, 12);
1478         if (rc)
1479                 goto out;
1480
1481         pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1482
1483         /* Build security blob before we assemble the request */
1484         build_ntlmssp_negotiate_blob(pSMB->req.SecurityBlob, ses);
1485         sess_data->iov[1].iov_len = sizeof(NEGOTIATE_MESSAGE);
1486         sess_data->iov[1].iov_base = pSMB->req.SecurityBlob;
1487         pSMB->req.SecurityBlobLength = cpu_to_le16(sizeof(NEGOTIATE_MESSAGE));
1488
1489         rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1490         if (rc)
1491                 goto out;
1492
1493         rc = sess_sendreceive(sess_data);
1494
1495         pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1496         smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1497
1498         /* If true, rc here is expected and not an error */
1499         if (sess_data->buf0_type != CIFS_NO_BUFFER &&
1500             smb_buf->Status.CifsError ==
1501                         cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
1502                 rc = 0;
1503
1504         if (rc)
1505                 goto out;
1506
1507         cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
1508
1509         if (smb_buf->WordCount != 4) {
1510                 rc = -EIO;
1511                 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1512                 goto out;
1513         }
1514
1515         ses->Suid = smb_buf->Uid;   /* UID left in wire format (le) */
1516         cifs_dbg(FYI, "UID = %llu\n", ses->Suid);
1517
1518         bytes_remaining = get_bcc(smb_buf);
1519         bcc_ptr = pByteArea(smb_buf);
1520
1521         blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1522         if (blob_len > bytes_remaining) {
1523                 cifs_dbg(VFS, "bad security blob length %d\n",
1524                                 blob_len);
1525                 rc = -EINVAL;
1526                 goto out;
1527         }
1528
1529         rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses);
1530 out:
1531         sess_free_buffer(sess_data);
1532
1533         if (!rc) {
1534                 sess_data->func = sess_auth_rawntlmssp_authenticate;
1535                 return;
1536         }
1537
1538         /* Else error. Cleanup */
1539         kfree(ses->auth_key.response);
1540         ses->auth_key.response = NULL;
1541         kfree(ses->ntlmssp);
1542         ses->ntlmssp = NULL;
1543
1544         sess_data->func = NULL;
1545         sess_data->result = rc;
1546 }
1547
1548 static void
1549 sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data)
1550 {
1551         int rc;
1552         struct smb_hdr *smb_buf;
1553         SESSION_SETUP_ANDX *pSMB;
1554         struct cifs_ses *ses = sess_data->ses;
1555         __u16 bytes_remaining;
1556         char *bcc_ptr;
1557         unsigned char *ntlmsspblob = NULL;
1558         u16 blob_len;
1559
1560         cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n");
1561
1562         /* wct = 12 */
1563         rc = sess_alloc_buffer(sess_data, 12);
1564         if (rc)
1565                 goto out;
1566
1567         /* Build security blob before we assemble the request */
1568         pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1569         smb_buf = (struct smb_hdr *)pSMB;
1570         rc = build_ntlmssp_auth_blob(&ntlmsspblob,
1571                                         &blob_len, ses, sess_data->nls_cp);
1572         if (rc)
1573                 goto out_free_ntlmsspblob;
1574         sess_data->iov[1].iov_len = blob_len;
1575         sess_data->iov[1].iov_base = ntlmsspblob;
1576         pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len);
1577         /*
1578          * Make sure that we tell the server that we are using
1579          * the uid that it just gave us back on the response
1580          * (challenge)
1581          */
1582         smb_buf->Uid = ses->Suid;
1583
1584         rc = _sess_auth_rawntlmssp_assemble_req(sess_data);
1585         if (rc)
1586                 goto out_free_ntlmsspblob;
1587
1588         rc = sess_sendreceive(sess_data);
1589         if (rc)
1590                 goto out_free_ntlmsspblob;
1591
1592         pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base;
1593         smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base;
1594         if (smb_buf->WordCount != 4) {
1595                 rc = -EIO;
1596                 cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount);
1597                 goto out_free_ntlmsspblob;
1598         }
1599
1600         if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN)
1601                 cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */
1602
1603         if (ses->Suid != smb_buf->Uid) {
1604                 ses->Suid = smb_buf->Uid;
1605                 cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid);
1606         }
1607
1608         bytes_remaining = get_bcc(smb_buf);
1609         bcc_ptr = pByteArea(smb_buf);
1610         blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength);
1611         if (blob_len > bytes_remaining) {
1612                 cifs_dbg(VFS, "bad security blob length %d\n",
1613                                 blob_len);
1614                 rc = -EINVAL;
1615                 goto out_free_ntlmsspblob;
1616         }
1617         bcc_ptr += blob_len;
1618         bytes_remaining -= blob_len;
1619
1620
1621         /* BB check if Unicode and decode strings */
1622         if (bytes_remaining == 0) {
1623                 /* no string area to decode, do nothing */
1624         } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) {
1625                 /* unicode string area must be word-aligned */
1626                 if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) {
1627                         ++bcc_ptr;
1628                         --bytes_remaining;
1629                 }
1630                 decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses,
1631                                       sess_data->nls_cp);
1632         } else {
1633                 decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses,
1634                                     sess_data->nls_cp);
1635         }
1636
1637 out_free_ntlmsspblob:
1638         kfree(ntlmsspblob);
1639 out:
1640         sess_free_buffer(sess_data);
1641
1642          if (!rc)
1643                 rc = sess_establish_session(sess_data);
1644
1645         /* Cleanup */
1646         kfree(ses->auth_key.response);
1647         ses->auth_key.response = NULL;
1648         kfree(ses->ntlmssp);
1649         ses->ntlmssp = NULL;
1650
1651         sess_data->func = NULL;
1652         sess_data->result = rc;
1653 }
1654
1655 static int select_sec(struct cifs_ses *ses, struct sess_data *sess_data)
1656 {
1657         int type;
1658
1659         type = cifs_select_sectype(ses->server, ses->sectype);
1660         cifs_dbg(FYI, "sess setup type %d\n", type);
1661         if (type == Unspecified) {
1662                 cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
1663                 return -EINVAL;
1664         }
1665
1666         switch (type) {
1667         case LANMAN:
1668                 /* LANMAN and plaintext are less secure and off by default.
1669                  * So we make this explicitly be turned on in kconfig (in the
1670                  * build) and turned on at runtime (changed from the default)
1671                  * in proc/fs/cifs or via mount parm.  Unfortunately this is
1672                  * needed for old Win (e.g. Win95), some obscure NAS and OS/2 */
1673 #ifdef CONFIG_CIFS_WEAK_PW_HASH
1674                 sess_data->func = sess_auth_lanman;
1675                 break;
1676 #else
1677                 return -EOPNOTSUPP;
1678 #endif
1679         case NTLM:
1680                 sess_data->func = sess_auth_ntlm;
1681                 break;
1682         case NTLMv2:
1683                 sess_data->func = sess_auth_ntlmv2;
1684                 break;
1685         case Kerberos:
1686 #ifdef CONFIG_CIFS_UPCALL
1687                 sess_data->func = sess_auth_kerberos;
1688                 break;
1689 #else
1690                 cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
1691                 return -ENOSYS;
1692                 break;
1693 #endif /* CONFIG_CIFS_UPCALL */
1694         case RawNTLMSSP:
1695                 sess_data->func = sess_auth_rawntlmssp_negotiate;
1696                 break;
1697         default:
1698                 cifs_dbg(VFS, "secType %d not supported!\n", type);
1699                 return -ENOSYS;
1700         }
1701
1702         return 0;
1703 }
1704
1705 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
1706                     const struct nls_table *nls_cp)
1707 {
1708         int rc = 0;
1709         struct sess_data *sess_data;
1710
1711         if (ses == NULL) {
1712                 WARN(1, "%s: ses == NULL!", __func__);
1713                 return -EINVAL;
1714         }
1715
1716         sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL);
1717         if (!sess_data)
1718                 return -ENOMEM;
1719
1720         rc = select_sec(ses, sess_data);
1721         if (rc)
1722                 goto out;
1723
1724         sess_data->xid = xid;
1725         sess_data->ses = ses;
1726         sess_data->buf0_type = CIFS_NO_BUFFER;
1727         sess_data->nls_cp = (struct nls_table *) nls_cp;
1728
1729         while (sess_data->func)
1730                 sess_data->func(sess_data);
1731
1732         /* Store result before we free sess_data */
1733         rc = sess_data->result;
1734
1735 out:
1736         kfree(sess_data);
1737         return rc;
1738 }