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