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