blob: e5dca9a48d9f2d22021aa68b97f689a22d588004 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/connect.c
3 *
Steve French366781c2008-01-25 10:12:41 +00004 * Copyright (C) International Business Machines Corp., 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
Steve Frenchfb8c4b12007-07-10 01:16:18 +000019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21#include <linux/fs.h>
22#include <linux/net.h>
23#include <linux/string.h>
24#include <linux/list.h>
25#include <linux/wait.h>
26#include <linux/ipv6.h>
27#include <linux/pagemap.h>
28#include <linux/ctype.h>
29#include <linux/utsname.h>
30#include <linux/mempool.h>
Steve Frenchb8643e12005-04-28 22:41:07 -070031#include <linux/delay.h>
Steve Frenchf1914012005-08-18 09:37:34 -070032#include <linux/completion.h>
Igor Mammedovaaf737a2007-04-03 19:16:43 +000033#include <linux/kthread.h>
Steve French0ae0efa2005-10-10 10:57:19 -070034#include <linux/pagevec.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
37#include <asm/processor.h>
38#include "cifspdu.h"
39#include "cifsglob.h"
40#include "cifsproto.h"
41#include "cifs_unicode.h"
42#include "cifs_debug.h"
43#include "cifs_fs_sb.h"
44#include "ntlmssp.h"
45#include "nterr.h"
46#include "rfc1002pdu.h"
Steve Frencha2653eb2005-11-10 15:33:38 -080047#include "cn_cifs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define CIFS_PORT 445
50#define RFC1001_PORT 139
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052extern void SMBNTencrypt(unsigned char *passwd, unsigned char *c8,
53 unsigned char *p24);
54
55extern mempool_t *cifs_req_poolp;
56
57struct smb_vol {
58 char *username;
59 char *password;
60 char *domainname;
61 char *UNC;
62 char *UNCip;
Steve French95b1cb92008-05-15 16:44:38 +000063 char *in6_addr; /* ipv6 address as human readable form of in6_addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 char *iocharset; /* local code page for mapping to and from Unicode */
65 char source_rfc1001_name[16]; /* netbios name of client */
Steve Frencha10faeb22005-08-22 21:38:31 -070066 char target_rfc1001_name[16]; /* netbios name of server for Win9x/ME */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 uid_t linux_uid;
68 gid_t linux_gid;
69 mode_t file_mode;
70 mode_t dir_mode;
Steve French189acaa2006-06-23 02:33:48 +000071 unsigned secFlg;
Steve French4b18f2a2008-04-29 00:06:05 +000072 bool rw:1;
73 bool retry:1;
74 bool intr:1;
75 bool setuids:1;
76 bool override_uid:1;
77 bool override_gid:1;
Jeff Laytond0a9c072008-05-12 22:23:49 +000078 bool dynperm:1;
Steve French4b18f2a2008-04-29 00:06:05 +000079 bool noperm:1;
80 bool no_psx_acl:1; /* set if posix acl support should be disabled */
81 bool cifs_acl:1;
82 bool no_xattr:1; /* set if xattr (EA) support should be disabled*/
83 bool server_ino:1; /* use inode numbers from server ie UniqueId */
84 bool direct_io:1;
Steve French95b1cb92008-05-15 16:44:38 +000085 bool remap:1; /* set to remap seven reserved chars in filenames */
86 bool posix_paths:1; /* unset to not ask for posix pathnames. */
Steve French4b18f2a2008-04-29 00:06:05 +000087 bool no_linux_ext:1;
88 bool sfu_emul:1;
Steve French95b1cb92008-05-15 16:44:38 +000089 bool nullauth:1; /* attempt to authenticate with null user */
90 bool nocase:1; /* request case insensitive filenames */
91 bool nobrl:1; /* disable sending byte range locks to srv */
92 bool seal:1; /* request transport encryption on share */
Steve French84210e92008-10-23 04:42:37 +000093 bool nodfs:1; /* Do not request DFS, even if available */
94 bool local_lease:1; /* check leases only on local system, not remote */
Steve Frenchedf1ae42008-10-29 00:47:57 +000095 bool noblocksnd:1;
96 bool noautotune:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 unsigned int rsize;
98 unsigned int wsize;
99 unsigned int sockopt;
100 unsigned short int port;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000101 char *prepath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000104static int ipv4_connect(struct sockaddr_in *psin_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct socket **csocket,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000106 char *netb_name,
Steve Frenchedf1ae42008-10-29 00:47:57 +0000107 char *server_netb_name,
108 bool noblocksnd,
109 bool nosndbuf); /* ipv6 never set sndbuf size */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000110static int ipv6_connect(struct sockaddr_in6 *psin_server,
Steve Frenchedf1ae42008-10-29 00:47:57 +0000111 struct socket **csocket, bool noblocksnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000114 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * cifs tcp session reconnection
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000116 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * mark tcp session as reconnecting so temporarily locked
118 * mark all smb sessions as reconnecting for tcp session
119 * reconnect tcp session
120 * wake up waiters on reconnection? - (not needed currently)
121 */
122
Steve French2cd646a2006-09-28 19:43:08 +0000123static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124cifs_reconnect(struct TCP_Server_Info *server)
125{
126 int rc = 0;
127 struct list_head *tmp;
128 struct cifsSesInfo *ses;
129 struct cifsTconInfo *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000130 struct mid_q_entry *mid_entry;
Steve French50c2f752007-07-13 00:33:32 +0000131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000133 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000134 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 next time through the loop */
136 spin_unlock(&GlobalMid_Lock);
137 return rc;
138 } else
139 server->tcpStatus = CifsNeedReconnect;
140 spin_unlock(&GlobalMid_Lock);
141 server->maxBuf = 0;
142
Steve Frenche4eb2952005-04-28 22:41:09 -0700143 cFYI(1, ("Reconnecting tcp session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* before reconnecting the tcp session, mark the smb session (uid)
146 and the tid bad so they are not used until reconnected */
147 read_lock(&GlobalSMBSeslock);
148 list_for_each(tmp, &GlobalSMBSessionList) {
149 ses = list_entry(tmp, struct cifsSesInfo, cifsSessionList);
150 if (ses->server) {
151 if (ses->server == server) {
152 ses->status = CifsNeedReconnect;
153 ses->ipc_tid = 0;
154 }
155 }
156 /* else tcp and smb sessions need reconnection */
157 }
158 list_for_each(tmp, &GlobalTreeConnectionList) {
159 tcon = list_entry(tmp, struct cifsTconInfo, cifsConnectionList);
Steve Frenchad8b15f2008-08-08 21:10:16 +0000160 if ((tcon->ses) && (tcon->ses->server == server))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 tcon->tidStatus = CifsNeedReconnect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163 read_unlock(&GlobalSMBSeslock);
164 /* do not want to be sending data on a socket we are freeing */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000165 down(&server->tcpSem);
166 if (server->ssocket) {
Steve French467a8f82007-06-27 22:41:32 +0000167 cFYI(1, ("State: 0x%x Flags: 0x%lx", server->ssocket->state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 server->ssocket->flags));
Trond Myklebust91cf45f2007-11-12 18:10:39 -0800169 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000170 cFYI(1, ("Post shutdown state: 0x%x Flags: 0x%lx",
Steve French467a8f82007-06-27 22:41:32 +0000171 server->ssocket->state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 server->ssocket->flags));
173 sock_release(server->ssocket);
174 server->ssocket = NULL;
175 }
176
177 spin_lock(&GlobalMid_Lock);
178 list_for_each(tmp, &server->pending_mid_q) {
179 mid_entry = list_entry(tmp, struct
180 mid_q_entry,
181 qhead);
Steve Frenchad8b15f2008-08-08 21:10:16 +0000182 if (mid_entry->midState == MID_REQUEST_SUBMITTED) {
Steve French09d1db52005-04-28 22:41:08 -0700183 /* Mark other intransit requests as needing
184 retry so we do not immediately mark the
185 session bad again (ie after we reconnect
186 below) as they timeout too */
Steve Frenchad8b15f2008-08-08 21:10:16 +0000187 mid_entry->midState = MID_RETRY_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189 }
190 spin_unlock(&GlobalMid_Lock);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000191 up(&server->tcpSem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Jeff Layton469ee612008-10-16 18:46:39 +0000193 while ((server->tcpStatus != CifsExiting) &&
194 (server->tcpStatus != CifsGood)) {
Steve French6c3d8902006-07-31 22:46:20 +0000195 try_to_freeze();
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000196 if (server->protocolType == IPV6) {
197 rc = ipv6_connect(&server->addr.sockAddr6,
Steve Frenchedf1ae42008-10-29 00:47:57 +0000198 &server->ssocket, server->noautotune);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 } else {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000200 rc = ipv4_connect(&server->addr.sockAddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 &server->ssocket,
Steve Frencha10faeb22005-08-22 21:38:31 -0700202 server->workstation_RFC1001_name,
Steve Frenchedf1ae42008-10-29 00:47:57 +0000203 server->server_RFC1001_name,
204 server->noblocksnd, server->noautotune);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000206 if (rc) {
207 cFYI(1, ("reconnect error %d", rc));
Steve French0cb766a2005-04-28 22:41:11 -0700208 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 } else {
210 atomic_inc(&tcpSesReconnectCount);
211 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000212 if (server->tcpStatus != CifsExiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 server->tcpStatus = CifsGood;
Steve Frenchad009ac2005-04-28 22:41:05 -0700214 server->sequence_number = 0;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000215 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* atomic_set(&server->inFlight,0);*/
217 wake_up(&server->response_q);
218 }
219 }
220 return rc;
221}
222
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000223/*
Steve Frenche4eb2952005-04-28 22:41:09 -0700224 return codes:
225 0 not a transact2, or all data present
226 >0 transact2 with that much data missing
227 -EINVAL = invalid transact2
228
229 */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000230static int check2ndT2(struct smb_hdr *pSMB, unsigned int maxBufSize)
Steve Frenche4eb2952005-04-28 22:41:09 -0700231{
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000232 struct smb_t2_rsp *pSMBt;
233 int total_data_size;
Steve Frenche4eb2952005-04-28 22:41:09 -0700234 int data_in_this_rsp;
235 int remaining;
236
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000237 if (pSMB->Command != SMB_COM_TRANSACTION2)
Steve Frenche4eb2952005-04-28 22:41:09 -0700238 return 0;
239
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000240 /* check for plausible wct, bcc and t2 data and parm sizes */
241 /* check for parm and data offset going beyond end of smb */
242 if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */
Steve French467a8f82007-06-27 22:41:32 +0000243 cFYI(1, ("invalid transact2 word count"));
Steve Frenche4eb2952005-04-28 22:41:09 -0700244 return -EINVAL;
245 }
246
247 pSMBt = (struct smb_t2_rsp *)pSMB;
248
249 total_data_size = le16_to_cpu(pSMBt->t2_rsp.TotalDataCount);
250 data_in_this_rsp = le16_to_cpu(pSMBt->t2_rsp.DataCount);
251
252 remaining = total_data_size - data_in_this_rsp;
253
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000254 if (remaining == 0)
Steve Frenche4eb2952005-04-28 22:41:09 -0700255 return 0;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000256 else if (remaining < 0) {
Steve French467a8f82007-06-27 22:41:32 +0000257 cFYI(1, ("total data %d smaller than data in frame %d",
Steve Frenche4eb2952005-04-28 22:41:09 -0700258 total_data_size, data_in_this_rsp));
259 return -EINVAL;
260 } else {
Steve French467a8f82007-06-27 22:41:32 +0000261 cFYI(1, ("missing %d bytes from transact2, check next response",
Steve Frenche4eb2952005-04-28 22:41:09 -0700262 remaining));
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000263 if (total_data_size > maxBufSize) {
264 cERROR(1, ("TotalDataSize %d is over maximum buffer %d",
265 total_data_size, maxBufSize));
266 return -EINVAL;
Steve Frenche4eb2952005-04-28 22:41:09 -0700267 }
268 return remaining;
269 }
270}
271
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000272static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
Steve Frenche4eb2952005-04-28 22:41:09 -0700273{
274 struct smb_t2_rsp *pSMB2 = (struct smb_t2_rsp *)psecond;
275 struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)pTargetSMB;
276 int total_data_size;
277 int total_in_buf;
278 int remaining;
279 int total_in_buf2;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000280 char *data_area_of_target;
281 char *data_area_of_buf2;
Steve Frenche4eb2952005-04-28 22:41:09 -0700282 __u16 byte_count;
283
284 total_data_size = le16_to_cpu(pSMBt->t2_rsp.TotalDataCount);
285
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000286 if (total_data_size != le16_to_cpu(pSMB2->t2_rsp.TotalDataCount)) {
Steve French63135e02007-07-17 17:34:02 +0000287 cFYI(1, ("total data size of primary and secondary t2 differ"));
Steve Frenche4eb2952005-04-28 22:41:09 -0700288 }
289
290 total_in_buf = le16_to_cpu(pSMBt->t2_rsp.DataCount);
291
292 remaining = total_data_size - total_in_buf;
Steve French50c2f752007-07-13 00:33:32 +0000293
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000294 if (remaining < 0)
Steve Frenche4eb2952005-04-28 22:41:09 -0700295 return -EINVAL;
296
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000297 if (remaining == 0) /* nothing to do, ignore */
Steve Frenche4eb2952005-04-28 22:41:09 -0700298 return 0;
Steve French50c2f752007-07-13 00:33:32 +0000299
Steve Frenche4eb2952005-04-28 22:41:09 -0700300 total_in_buf2 = le16_to_cpu(pSMB2->t2_rsp.DataCount);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000301 if (remaining < total_in_buf2) {
Steve French467a8f82007-06-27 22:41:32 +0000302 cFYI(1, ("transact2 2nd response contains too much data"));
Steve Frenche4eb2952005-04-28 22:41:09 -0700303 }
304
305 /* find end of first SMB data area */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000306 data_area_of_target = (char *)&pSMBt->hdr.Protocol +
Steve Frenche4eb2952005-04-28 22:41:09 -0700307 le16_to_cpu(pSMBt->t2_rsp.DataOffset);
308 /* validate target area */
309
310 data_area_of_buf2 = (char *) &pSMB2->hdr.Protocol +
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000311 le16_to_cpu(pSMB2->t2_rsp.DataOffset);
Steve Frenche4eb2952005-04-28 22:41:09 -0700312
313 data_area_of_target += total_in_buf;
314
315 /* copy second buffer into end of first buffer */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000316 memcpy(data_area_of_target, data_area_of_buf2, total_in_buf2);
Steve Frenche4eb2952005-04-28 22:41:09 -0700317 total_in_buf += total_in_buf2;
318 pSMBt->t2_rsp.DataCount = cpu_to_le16(total_in_buf);
319 byte_count = le16_to_cpu(BCC_LE(pTargetSMB));
320 byte_count += total_in_buf2;
321 BCC_LE(pTargetSMB) = cpu_to_le16(byte_count);
322
Steve French70ca7342005-09-22 16:32:06 -0700323 byte_count = pTargetSMB->smb_buf_length;
Steve Frenche4eb2952005-04-28 22:41:09 -0700324 byte_count += total_in_buf2;
325
326 /* BB also add check that we are not beyond maximum buffer size */
Steve French50c2f752007-07-13 00:33:32 +0000327
Steve French70ca7342005-09-22 16:32:06 -0700328 pTargetSMB->smb_buf_length = byte_count;
Steve Frenche4eb2952005-04-28 22:41:09 -0700329
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000330 if (remaining == total_in_buf2) {
Steve French467a8f82007-06-27 22:41:32 +0000331 cFYI(1, ("found the last secondary response"));
Steve Frenche4eb2952005-04-28 22:41:09 -0700332 return 0; /* we are done */
333 } else /* more responses to go */
334 return 1;
335
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static int
339cifs_demultiplex_thread(struct TCP_Server_Info *server)
340{
341 int length;
342 unsigned int pdu_length, total_read;
343 struct smb_hdr *smb_buffer = NULL;
Steve Frenchb8643e12005-04-28 22:41:07 -0700344 struct smb_hdr *bigbuf = NULL;
345 struct smb_hdr *smallbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct msghdr smb_msg;
347 struct kvec iov;
348 struct socket *csocket = server->ssocket;
349 struct list_head *tmp;
350 struct cifsSesInfo *ses;
351 struct task_struct *task_to_wake = NULL;
352 struct mid_q_entry *mid_entry;
Steve French70ca7342005-09-22 16:32:06 -0700353 char temp;
Steve French4b18f2a2008-04-29 00:06:05 +0000354 bool isLargeBuf = false;
355 bool isMultiRsp;
Steve Frenche4eb2952005-04-28 22:41:09 -0700356 int reconnect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 current->flags |= PF_MEMALLOC;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700359 cFYI(1, ("Demultiplex PID: %d", task_pid_nr(current)));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400360
361 length = atomic_inc_return(&tcpSesAllocCount);
362 if (length > 1)
Steve French26f57362007-08-30 22:09:15 +0000363 mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
364 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700366 set_freezable();
Jeff Layton469ee612008-10-16 18:46:39 +0000367 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700368 if (try_to_freeze())
369 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700370 if (bigbuf == NULL) {
371 bigbuf = cifs_buf_get();
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000372 if (!bigbuf) {
373 cERROR(1, ("No memory for large SMB response"));
Steve Frenchb8643e12005-04-28 22:41:07 -0700374 msleep(3000);
375 /* retry will check if exiting */
376 continue;
377 }
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000378 } else if (isLargeBuf) {
379 /* we are reusing a dirty large buf, clear its start */
Steve French26f57362007-08-30 22:09:15 +0000380 memset(bigbuf, 0, sizeof(struct smb_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
Steve Frenchb8643e12005-04-28 22:41:07 -0700382
383 if (smallbuf == NULL) {
384 smallbuf = cifs_small_buf_get();
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000385 if (!smallbuf) {
386 cERROR(1, ("No memory for SMB response"));
Steve Frenchb8643e12005-04-28 22:41:07 -0700387 msleep(1000);
388 /* retry will check if exiting */
389 continue;
390 }
391 /* beginning of smb buffer is cleared in our buf_get */
392 } else /* if existing small buf clear beginning */
Steve French26f57362007-08-30 22:09:15 +0000393 memset(smallbuf, 0, sizeof(struct smb_hdr));
Steve Frenchb8643e12005-04-28 22:41:07 -0700394
Steve French4b18f2a2008-04-29 00:06:05 +0000395 isLargeBuf = false;
396 isMultiRsp = false;
Steve Frenchb8643e12005-04-28 22:41:07 -0700397 smb_buffer = smallbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 iov.iov_base = smb_buffer;
399 iov.iov_len = 4;
400 smb_msg.msg_control = NULL;
401 smb_msg.msg_controllen = 0;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000402 pdu_length = 4; /* enough to get RFC1001 header */
403incomplete_rcv:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 length =
405 kernel_recvmsg(csocket, &smb_msg,
Steve Frenchf01d5e12007-08-30 21:13:31 +0000406 &iov, 1, pdu_length, 0 /* BB other flags? */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Jeff Layton469ee612008-10-16 18:46:39 +0000408 if (server->tcpStatus == CifsExiting) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 break;
410 } else if (server->tcpStatus == CifsNeedReconnect) {
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000411 cFYI(1, ("Reconnect after server stopped responding"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 cifs_reconnect(server);
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000413 cFYI(1, ("call to reconnect done"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 csocket = server->ssocket;
415 continue;
416 } else if ((length == -ERESTARTSYS) || (length == -EAGAIN)) {
Steve Frenchb8643e12005-04-28 22:41:07 -0700417 msleep(1); /* minimum sleep to prevent looping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 allowing socket to clear and app threads to set
419 tcpStatus CifsNeedReconnect if server hung */
Steve Frenchc18c7322007-10-17 18:01:11 +0000420 if (pdu_length < 4)
421 goto incomplete_rcv;
422 else
423 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 } else if (length <= 0) {
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000425 if (server->tcpStatus == CifsNew) {
426 cFYI(1, ("tcp session abend after SMBnegprot"));
Steve French09d1db52005-04-28 22:41:08 -0700427 /* some servers kill the TCP session rather than
428 returning an SMB negprot error, in which
429 case reconnecting here is not going to help,
430 and so simply return error to mount */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 break;
432 }
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000433 if (!try_to_freeze() && (length == -EINTR)) {
Steve French467a8f82007-06-27 22:41:32 +0000434 cFYI(1, ("cifsd thread killed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 break;
436 }
Steve French467a8f82007-06-27 22:41:32 +0000437 cFYI(1, ("Reconnect after unexpected peek error %d",
Steve French57337e42005-04-28 22:41:10 -0700438 length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 cifs_reconnect(server);
440 csocket = server->ssocket;
441 wake_up(&server->response_q);
442 continue;
Petr Tesarik2a974682007-11-20 02:24:08 +0000443 } else if (length < pdu_length) {
444 cFYI(1, ("requested %d bytes but only got %d bytes",
445 pdu_length, length));
Steve Frenchf01d5e12007-08-30 21:13:31 +0000446 pdu_length -= length;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000447 msleep(1);
448 goto incomplete_rcv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Steve French67010fb2005-04-28 22:41:09 -0700450
Steve French70ca7342005-09-22 16:32:06 -0700451 /* The right amount was read from socket - 4 bytes */
452 /* so we can now interpret the length field */
Steve French46810cb2005-04-28 22:41:09 -0700453
Steve French70ca7342005-09-22 16:32:06 -0700454 /* the first byte big endian of the length field,
455 is actually not part of the length but the type
456 with the most common, zero, as regular data */
457 temp = *((char *) smb_buffer);
458
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000459 /* Note that FC 1001 length is big endian on the wire,
Steve French70ca7342005-09-22 16:32:06 -0700460 but we convert it here so it is always manipulated
461 as host byte order */
Harvey Harrison5ca33c62008-07-23 17:45:58 -0700462 pdu_length = be32_to_cpu((__force __be32)smb_buffer->smb_buf_length);
Steve French70ca7342005-09-22 16:32:06 -0700463 smb_buffer->smb_buf_length = pdu_length;
Steve French46810cb2005-04-28 22:41:09 -0700464
Steve French467a8f82007-06-27 22:41:32 +0000465 cFYI(1, ("rfc1002 length 0x%x", pdu_length+4));
Steve French70ca7342005-09-22 16:32:06 -0700466
467 if (temp == (char) RFC1002_SESSION_KEEP_ALIVE) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000468 continue;
Steve French70ca7342005-09-22 16:32:06 -0700469 } else if (temp == (char)RFC1002_POSITIVE_SESSION_RESPONSE) {
Steve French467a8f82007-06-27 22:41:32 +0000470 cFYI(1, ("Good RFC 1002 session rsp"));
Steve Frenche4eb2952005-04-28 22:41:09 -0700471 continue;
Steve French70ca7342005-09-22 16:32:06 -0700472 } else if (temp == (char)RFC1002_NEGATIVE_SESSION_RESPONSE) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000473 /* we get this from Windows 98 instead of
Steve French46810cb2005-04-28 22:41:09 -0700474 an error on SMB negprot response */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000475 cFYI(1, ("Negative RFC1002 Session Response Error 0x%x)",
Steve French70ca7342005-09-22 16:32:06 -0700476 pdu_length));
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000477 if (server->tcpStatus == CifsNew) {
478 /* if nack on negprot (rather than
Steve French46810cb2005-04-28 22:41:09 -0700479 ret of smb negprot error) reconnecting
480 not going to help, ret error to mount */
481 break;
482 } else {
483 /* give server a second to
484 clean up before reconnect attempt */
485 msleep(1000);
486 /* always try 445 first on reconnect
487 since we get NACK on some if we ever
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000488 connected to port 139 (the NACK is
Steve French46810cb2005-04-28 22:41:09 -0700489 since we do not begin with RFC1001
490 session initialize frame) */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000491 server->addr.sockAddr.sin_port =
Steve French46810cb2005-04-28 22:41:09 -0700492 htons(CIFS_PORT);
493 cifs_reconnect(server);
494 csocket = server->ssocket;
495 wake_up(&server->response_q);
496 continue;
497 }
Steve French70ca7342005-09-22 16:32:06 -0700498 } else if (temp != (char) 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000499 cERROR(1, ("Unknown RFC 1002 frame"));
Steve French70ca7342005-09-22 16:32:06 -0700500 cifs_dump_mem(" Received Data: ", (char *)smb_buffer,
501 length);
Steve French46810cb2005-04-28 22:41:09 -0700502 cifs_reconnect(server);
503 csocket = server->ssocket;
504 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700505 }
506
507 /* else we have an SMB response */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000508 if ((pdu_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) ||
Steve French26f57362007-08-30 22:09:15 +0000509 (pdu_length < sizeof(struct smb_hdr) - 1 - 4)) {
Steve Frenche4eb2952005-04-28 22:41:09 -0700510 cERROR(1, ("Invalid size SMB length %d pdu_length %d",
Steve French46810cb2005-04-28 22:41:09 -0700511 length, pdu_length+4));
Steve Frenche4eb2952005-04-28 22:41:09 -0700512 cifs_reconnect(server);
513 csocket = server->ssocket;
514 wake_up(&server->response_q);
515 continue;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000516 }
Steve Frenche4eb2952005-04-28 22:41:09 -0700517
518 /* else length ok */
519 reconnect = 0;
520
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000521 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
Steve French4b18f2a2008-04-29 00:06:05 +0000522 isLargeBuf = true;
Steve Frenche4eb2952005-04-28 22:41:09 -0700523 memcpy(bigbuf, smallbuf, 4);
524 smb_buffer = bigbuf;
525 }
526 length = 0;
527 iov.iov_base = 4 + (char *)smb_buffer;
528 iov.iov_len = pdu_length;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000529 for (total_read = 0; total_read < pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -0700530 total_read += length) {
531 length = kernel_recvmsg(csocket, &smb_msg, &iov, 1,
532 pdu_length - total_read, 0);
Jeff Layton469ee612008-10-16 18:46:39 +0000533 if ((server->tcpStatus == CifsExiting) ||
Steve Frenche4eb2952005-04-28 22:41:09 -0700534 (length == -EINTR)) {
535 /* then will exit */
536 reconnect = 2;
537 break;
538 } else if (server->tcpStatus == CifsNeedReconnect) {
Steve French46810cb2005-04-28 22:41:09 -0700539 cifs_reconnect(server);
540 csocket = server->ssocket;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000541 /* Reconnect wakes up rspns q */
Steve Frenche4eb2952005-04-28 22:41:09 -0700542 /* Now we will reread sock */
543 reconnect = 1;
544 break;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000545 } else if ((length == -ERESTARTSYS) ||
Steve Frenche4eb2952005-04-28 22:41:09 -0700546 (length == -EAGAIN)) {
547 msleep(1); /* minimum sleep to prevent looping,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000548 allowing socket to clear and app
Steve Frenche4eb2952005-04-28 22:41:09 -0700549 threads to set tcpStatus
550 CifsNeedReconnect if server hung*/
Steve Frenchc18c7322007-10-17 18:01:11 +0000551 length = 0;
Steve French46810cb2005-04-28 22:41:09 -0700552 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700553 } else if (length <= 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000554 cERROR(1, ("Received no data, expecting %d",
Steve Frenche4eb2952005-04-28 22:41:09 -0700555 pdu_length - total_read));
556 cifs_reconnect(server);
557 csocket = server->ssocket;
558 reconnect = 1;
559 break;
Steve French46810cb2005-04-28 22:41:09 -0700560 }
561 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000562 if (reconnect == 2)
Steve Frenche4eb2952005-04-28 22:41:09 -0700563 break;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000564 else if (reconnect == 1)
Steve Frenche4eb2952005-04-28 22:41:09 -0700565 continue;
566
567 length += 4; /* account for rfc1002 hdr */
Steve French50c2f752007-07-13 00:33:32 +0000568
Steve Frenche4eb2952005-04-28 22:41:09 -0700569
570 dump_smb(smb_buffer, length);
Steve French184ed212006-02-24 06:15:11 +0000571 if (checkSMB(smb_buffer, smb_buffer->Mid, total_read+4)) {
Steve Frenchb387eae2005-10-10 14:21:15 -0700572 cifs_dump_mem("Bad SMB: ", smb_buffer, 48);
Steve Frenche4eb2952005-04-28 22:41:09 -0700573 continue;
574 }
575
576
577 task_to_wake = NULL;
578 spin_lock(&GlobalMid_Lock);
579 list_for_each(tmp, &server->pending_mid_q) {
580 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
581
Steve French50c2f752007-07-13 00:33:32 +0000582 if ((mid_entry->mid == smb_buffer->Mid) &&
Steve Frenche4eb2952005-04-28 22:41:09 -0700583 (mid_entry->midState == MID_REQUEST_SUBMITTED) &&
584 (mid_entry->command == smb_buffer->Command)) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000585 if (check2ndT2(smb_buffer,server->maxBuf) > 0) {
Steve Frenche4eb2952005-04-28 22:41:09 -0700586 /* We have a multipart transact2 resp */
Steve French4b18f2a2008-04-29 00:06:05 +0000587 isMultiRsp = true;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000588 if (mid_entry->resp_buf) {
Steve Frenche4eb2952005-04-28 22:41:09 -0700589 /* merge response - fix up 1st*/
Steve French50c2f752007-07-13 00:33:32 +0000590 if (coalesce_t2(smb_buffer,
Steve Frenche4eb2952005-04-28 22:41:09 -0700591 mid_entry->resp_buf)) {
Steve French4b18f2a2008-04-29 00:06:05 +0000592 mid_entry->multiRsp =
593 true;
Steve Frenche4eb2952005-04-28 22:41:09 -0700594 break;
595 } else {
596 /* all parts received */
Steve French4b18f2a2008-04-29 00:06:05 +0000597 mid_entry->multiEnd =
598 true;
Steve French50c2f752007-07-13 00:33:32 +0000599 goto multi_t2_fnd;
Steve Frenche4eb2952005-04-28 22:41:09 -0700600 }
601 } else {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000602 if (!isLargeBuf) {
Steve Frenche4eb2952005-04-28 22:41:09 -0700603 cERROR(1,("1st trans2 resp needs bigbuf"));
604 /* BB maybe we can fix this up, switch
Steve French50c2f752007-07-13 00:33:32 +0000605 to already allocated large buffer? */
Steve Frenche4eb2952005-04-28 22:41:09 -0700606 } else {
Steve Frenchcd634992005-04-28 22:41:10 -0700607 /* Have first buffer */
Steve Frenche4eb2952005-04-28 22:41:09 -0700608 mid_entry->resp_buf =
609 smb_buffer;
Steve French4b18f2a2008-04-29 00:06:05 +0000610 mid_entry->largeBuf =
611 true;
Steve Frenche4eb2952005-04-28 22:41:09 -0700612 bigbuf = NULL;
613 }
614 }
615 break;
Steve French50c2f752007-07-13 00:33:32 +0000616 }
Steve Frenche4eb2952005-04-28 22:41:09 -0700617 mid_entry->resp_buf = smb_buffer;
Steve French4b18f2a2008-04-29 00:06:05 +0000618 mid_entry->largeBuf = isLargeBuf;
Steve Frenche4eb2952005-04-28 22:41:09 -0700619multi_t2_fnd:
620 task_to_wake = mid_entry->tsk;
621 mid_entry->midState = MID_RESPONSE_RECEIVED;
Steve French1047abc2005-10-11 19:58:06 -0700622#ifdef CONFIG_CIFS_STATS2
623 mid_entry->when_received = jiffies;
624#endif
Steve French3a5ff612006-07-14 22:37:11 +0000625 /* so we do not time out requests to server
626 which is still responding (since server could
627 be busy but not dead) */
628 server->lstrp = jiffies;
Steve Frenche4eb2952005-04-28 22:41:09 -0700629 break;
630 }
631 }
632 spin_unlock(&GlobalMid_Lock);
633 if (task_to_wake) {
Steve Frenchcd634992005-04-28 22:41:10 -0700634 /* Was previous buf put in mpx struct for multi-rsp? */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000635 if (!isMultiRsp) {
Steve Frenchcd634992005-04-28 22:41:10 -0700636 /* smb buffer will be freed by user thread */
Steve French26f57362007-08-30 22:09:15 +0000637 if (isLargeBuf)
Steve Frenchcd634992005-04-28 22:41:10 -0700638 bigbuf = NULL;
Steve French26f57362007-08-30 22:09:15 +0000639 else
Steve Frenchcd634992005-04-28 22:41:10 -0700640 smallbuf = NULL;
641 }
Steve Frenche4eb2952005-04-28 22:41:09 -0700642 wake_up_process(task_to_wake);
Steve French4b18f2a2008-04-29 00:06:05 +0000643 } else if (!is_valid_oplock_break(smb_buffer, server) &&
644 !isMultiRsp) {
Steve French50c2f752007-07-13 00:33:32 +0000645 cERROR(1, ("No task to wake, unknown frame received! "
646 "NumMids %d", midCount.counter));
647 cifs_dump_mem("Received Data is: ", (char *)smb_buffer,
Steve French70ca7342005-09-22 16:32:06 -0700648 sizeof(struct smb_hdr));
Steve French39798772006-05-31 22:40:51 +0000649#ifdef CONFIG_CIFS_DEBUG2
650 cifs_dump_detail(smb_buffer);
651 cifs_dump_mids(server);
652#endif /* CIFS_DEBUG2 */
Steve French50c2f752007-07-13 00:33:32 +0000653
Steve Frenche4eb2952005-04-28 22:41:09 -0700654 }
655 } /* end while !EXITING */
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 spin_lock(&GlobalMid_Lock);
658 server->tcpStatus = CifsExiting;
Steve Frenche691b9d2008-05-11 15:53:33 +0000659 spin_unlock(&GlobalMid_Lock);
Steve Frenchdbdbb872008-06-10 21:21:56 +0000660 wake_up_all(&server->response_q);
Steve Frenche691b9d2008-05-11 15:53:33 +0000661
Steve French31ca3bc2005-04-28 22:41:11 -0700662 /* check if we have blocked requests that need to free */
663 /* Note that cifs_max_pending is normally 50, but
664 can be set at module install time to as little as two */
Steve Frenche691b9d2008-05-11 15:53:33 +0000665 spin_lock(&GlobalMid_Lock);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000666 if (atomic_read(&server->inFlight) >= cifs_max_pending)
Steve French31ca3bc2005-04-28 22:41:11 -0700667 atomic_set(&server->inFlight, cifs_max_pending - 1);
668 /* We do not want to set the max_pending too low or we
669 could end up with the counter going negative */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 spin_unlock(&GlobalMid_Lock);
Steve French50c2f752007-07-13 00:33:32 +0000671 /* Although there should not be any requests blocked on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 this queue it can not hurt to be paranoid and try to wake up requests
Steve French09d1db52005-04-28 22:41:08 -0700673 that may haven been blocked when more than 50 at time were on the wire
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 to the same server - they now will see the session is in exit state
675 and get out of SendReceive. */
676 wake_up_all(&server->request_q);
677 /* give those requests time to exit */
Steve Frenchb8643e12005-04-28 22:41:07 -0700678 msleep(125);
Steve French50c2f752007-07-13 00:33:32 +0000679
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000680 if (server->ssocket) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 sock_release(csocket);
682 server->ssocket = NULL;
683 }
Steve Frenchb8643e12005-04-28 22:41:07 -0700684 /* buffer usuallly freed in free_mid - need to free it here on exit */
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +0000685 cifs_buf_release(bigbuf);
686 if (smallbuf) /* no sense logging a debug message if NULL */
Steve Frenchb8643e12005-04-28 22:41:07 -0700687 cifs_small_buf_release(smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 read_lock(&GlobalSMBSeslock);
690 if (list_empty(&server->pending_mid_q)) {
Steve French09d1db52005-04-28 22:41:08 -0700691 /* loop through server session structures attached to this and
692 mark them dead */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 list_for_each(tmp, &GlobalSMBSessionList) {
694 ses =
695 list_entry(tmp, struct cifsSesInfo,
696 cifsSessionList);
697 if (ses->server == server) {
698 ses->status = CifsExiting;
699 ses->server = NULL;
700 }
701 }
702 read_unlock(&GlobalSMBSeslock);
703 } else {
Steve French31ca3bc2005-04-28 22:41:11 -0700704 /* although we can not zero the server struct pointer yet,
705 since there are active requests which may depnd on them,
706 mark the corresponding SMB sessions as exiting too */
707 list_for_each(tmp, &GlobalSMBSessionList) {
708 ses = list_entry(tmp, struct cifsSesInfo,
709 cifsSessionList);
Steve French26f57362007-08-30 22:09:15 +0000710 if (ses->server == server)
Steve French31ca3bc2005-04-28 22:41:11 -0700711 ses->status = CifsExiting;
Steve French31ca3bc2005-04-28 22:41:11 -0700712 }
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 spin_lock(&GlobalMid_Lock);
715 list_for_each(tmp, &server->pending_mid_q) {
716 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
717 if (mid_entry->midState == MID_REQUEST_SUBMITTED) {
Steve French50c2f752007-07-13 00:33:32 +0000718 cFYI(1, ("Clearing Mid 0x%x - waking up ",
719 mid_entry->mid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 task_to_wake = mid_entry->tsk;
Steve French26f57362007-08-30 22:09:15 +0000721 if (task_to_wake)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 wake_up_process(task_to_wake);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724 }
725 spin_unlock(&GlobalMid_Lock);
726 read_unlock(&GlobalSMBSeslock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* 1/8th of sec is more than enough time for them to exit */
Steve Frenchb8643e12005-04-28 22:41:07 -0700728 msleep(125);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
Steve Frenchf1914012005-08-18 09:37:34 -0700731 if (!list_empty(&server->pending_mid_q)) {
Steve French50c2f752007-07-13 00:33:32 +0000732 /* mpx threads have not exited yet give them
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 at least the smb send timeout time for long ops */
Steve French31ca3bc2005-04-28 22:41:11 -0700734 /* due to delays on oplock break requests, we need
735 to wait at least 45 seconds before giving up
736 on a request getting a response and going ahead
737 and killing cifsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 cFYI(1, ("Wait for exit from demultiplex thread"));
Steve French31ca3bc2005-04-28 22:41:11 -0700739 msleep(46000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 /* if threads still have not exited they are probably never
741 coming home not much else we can do but free the memory */
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Steve French31ca3bc2005-04-28 22:41:11 -0700744 /* last chance to mark ses pointers invalid
745 if there are any pointing to this (e.g
Steve French50c2f752007-07-13 00:33:32 +0000746 if a crazy root user tried to kill cifsd
Steve French31ca3bc2005-04-28 22:41:11 -0700747 kernel thread explicitly this might happen) */
Jeff Layton93d0ec82008-08-02 08:00:48 -0400748 write_lock(&GlobalSMBSeslock);
Steve French31ca3bc2005-04-28 22:41:11 -0700749 list_for_each(tmp, &GlobalSMBSessionList) {
750 ses = list_entry(tmp, struct cifsSesInfo,
751 cifsSessionList);
Steve French26f57362007-08-30 22:09:15 +0000752 if (ses->server == server)
Steve French31ca3bc2005-04-28 22:41:11 -0700753 ses->server = NULL;
Steve French31ca3bc2005-04-28 22:41:11 -0700754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 write_unlock(&GlobalSMBSeslock);
Steve French31ca3bc2005-04-28 22:41:11 -0700756
Jeff Laytonc359cf32007-11-16 22:22:06 +0000757 kfree(server->hostname);
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -0400758 task_to_wake = xchg(&server->tsk, NULL);
Steve French31ca3bc2005-04-28 22:41:11 -0700759 kfree(server);
Jeff Layton93d0ec82008-08-02 08:00:48 -0400760
761 length = atomic_dec_return(&tcpSesAllocCount);
Steve French26f57362007-08-30 22:09:15 +0000762 if (length > 0)
763 mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
764 GFP_KERNEL);
Steve French50c2f752007-07-13 00:33:32 +0000765
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -0400766 /* if server->tsk was NULL then wait for a signal before exiting */
767 if (!task_to_wake) {
768 set_current_state(TASK_INTERRUPTIBLE);
769 while (!signal_pending(current)) {
770 schedule();
771 set_current_state(TASK_INTERRUPTIBLE);
772 }
773 set_current_state(TASK_RUNNING);
774 }
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return 0;
777}
778
Jeff Laytonc359cf32007-11-16 22:22:06 +0000779/* extract the host portion of the UNC string */
780static char *
781extract_hostname(const char *unc)
782{
783 const char *src;
784 char *dst, *delim;
785 unsigned int len;
786
787 /* skip double chars at beginning of string */
788 /* BB: check validity of these bytes? */
789 src = unc + 2;
790
791 /* delimiter between hostname and sharename is always '\\' now */
792 delim = strchr(src, '\\');
793 if (!delim)
794 return ERR_PTR(-EINVAL);
795
796 len = delim - src;
797 dst = kmalloc((len + 1), GFP_KERNEL);
798 if (dst == NULL)
799 return ERR_PTR(-ENOMEM);
800
801 memcpy(dst, src, len);
802 dst[len] = '\0';
803
804 return dst;
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807static int
Steve French50c2f752007-07-13 00:33:32 +0000808cifs_parse_mount_options(char *options, const char *devname,
809 struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 char *value;
812 char *data;
813 unsigned int temp_len, i, j;
814 char separator[2];
815
816 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +0000817 separator[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Linus Torvalds12e36b22006-10-13 08:09:29 -0700819 if (Local_System_Name[0] != 0)
Steve French50c2f752007-07-13 00:33:32 +0000820 memcpy(vol->source_rfc1001_name, Local_System_Name, 15);
Steve French2cd646a2006-09-28 19:43:08 +0000821 else {
Linus Torvalds12e36b22006-10-13 08:09:29 -0700822 char *nodename = utsname()->nodename;
Steve French50c2f752007-07-13 00:33:32 +0000823 int n = strnlen(nodename, 15);
824 memset(vol->source_rfc1001_name, 0x20, 15);
825 for (i = 0; i < n; i++) {
Steve French2cd646a2006-09-28 19:43:08 +0000826 /* does not have to be perfect mapping since field is
827 informational, only used for servers that do not support
828 port 445 and it can be overridden at mount time */
Linus Torvalds12e36b22006-10-13 08:09:29 -0700829 vol->source_rfc1001_name[i] = toupper(nodename[i]);
Steve French2cd646a2006-09-28 19:43:08 +0000830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 vol->source_rfc1001_name[15] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -0700833 /* null target name indicates to use *SMBSERVR default called name
834 if we end up sending RFC1001 session initialize */
835 vol->target_rfc1001_name[0] = 0;
David Howellsa001e5b2008-11-14 10:38:47 +1100836 vol->linux_uid = current_uid(); /* use current_euid() instead? */
837 vol->linux_gid = current_gid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 vol->dir_mode = S_IRWXUGO;
839 /* 2767 perms indicate mandatory locking support */
Steve French7505e052007-11-01 18:03:01 +0000840 vol->file_mode = (S_IRWXUGO | S_ISGID) & (~S_IXGRP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Steve French4b18f2a2008-04-29 00:06:05 +0000843 vol->rw = true;
Jeremy Allisonac670552005-06-22 17:26:35 -0700844 /* default is always to request posix paths. */
845 vol->posix_paths = 1;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (!options)
848 return 1;
849
Steve French50c2f752007-07-13 00:33:32 +0000850 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000851 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 separator[0] = options[4];
853 options += 5;
854 } else {
Steve French467a8f82007-06-27 22:41:32 +0000855 cFYI(1, ("Null separator not allowed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857 }
Steve French50c2f752007-07-13 00:33:32 +0000858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 while ((data = strsep(&options, separator)) != NULL) {
860 if (!*data)
861 continue;
862 if ((value = strchr(data, '=')) != NULL)
863 *value++ = '\0';
864
Steve French50c2f752007-07-13 00:33:32 +0000865 /* Have to parse this before we parse for "user" */
866 if (strnicmp(data, "user_xattr", 10) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 vol->no_xattr = 0;
Steve French50c2f752007-07-13 00:33:32 +0000868 } else if (strnicmp(data, "nouser_xattr", 12) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 vol->no_xattr = 1;
870 } else if (strnicmp(data, "user", 4) == 0) {
Steve French4b952a92006-10-30 21:46:13 +0000871 if (!value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 printk(KERN_WARNING
873 "CIFS: invalid or missing username\n");
874 return 1; /* needs_arg; */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000875 } else if (!*value) {
Steve French4b952a92006-10-30 21:46:13 +0000876 /* null user, ie anonymous, authentication */
877 vol->nullauth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879 if (strnlen(value, 200) < 200) {
880 vol->username = value;
881 } else {
882 printk(KERN_WARNING "CIFS: username too long\n");
883 return 1;
884 }
885 } else if (strnicmp(data, "pass", 4) == 0) {
886 if (!value) {
887 vol->password = NULL;
888 continue;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000889 } else if (value[0] == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /* check if string begins with double comma
891 since that would mean the password really
892 does start with a comma, and would not
893 indicate an empty string */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000894 if (value[1] != separator[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 vol->password = NULL;
896 continue;
897 }
898 }
899 temp_len = strlen(value);
900 /* removed password length check, NTLM passwords
901 can be arbitrarily long */
902
Steve French50c2f752007-07-13 00:33:32 +0000903 /* if comma in password, the string will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 prematurely null terminated. Commas in password are
905 specified across the cifs mount interface by a double
906 comma ie ,, and a comma used as in other cases ie ','
907 as a parameter delimiter/separator is single and due
908 to the strsep above is temporarily zeroed. */
909
910 /* NB: password legally can have multiple commas and
911 the only illegal character in a password is null */
912
Steve French50c2f752007-07-13 00:33:32 +0000913 if ((value[temp_len] == 0) &&
Steve French09d1db52005-04-28 22:41:08 -0700914 (value[temp_len+1] == separator[0])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /* reinsert comma */
916 value[temp_len] = separator[0];
Steve French50c2f752007-07-13 00:33:32 +0000917 temp_len += 2; /* move after second comma */
918 while (value[temp_len] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (value[temp_len] == separator[0]) {
Steve French50c2f752007-07-13 00:33:32 +0000920 if (value[temp_len+1] ==
Steve French09d1db52005-04-28 22:41:08 -0700921 separator[0]) {
922 /* skip second comma */
923 temp_len++;
Steve French50c2f752007-07-13 00:33:32 +0000924 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /* single comma indicating start
926 of next parm */
927 break;
928 }
929 }
930 temp_len++;
931 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000932 if (value[temp_len] == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 options = NULL;
934 } else {
935 value[temp_len] = 0;
936 /* point option to start of next parm */
937 options = value + temp_len + 1;
938 }
Steve French50c2f752007-07-13 00:33:32 +0000939 /* go from value to value + temp_len condensing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 double commas to singles. Note that this ends up
941 allocating a few bytes too many, which is ok */
Pekka Enberge915fc42005-09-06 15:18:35 -0700942 vol->password = kzalloc(temp_len, GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000943 if (vol->password == NULL) {
Steve French50c2f752007-07-13 00:33:32 +0000944 printk(KERN_WARNING "CIFS: no memory "
945 "for password\n");
Steve French433dc242005-04-28 22:41:08 -0700946 return 1;
947 }
Steve French50c2f752007-07-13 00:33:32 +0000948 for (i = 0, j = 0; i < temp_len; i++, j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 vol->password[j] = value[i];
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000950 if (value[i] == separator[0]
Steve French09d1db52005-04-28 22:41:08 -0700951 && value[i+1] == separator[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* skip second comma */
953 i++;
954 }
955 }
956 vol->password[j] = 0;
957 } else {
Pekka Enberge915fc42005-09-06 15:18:35 -0700958 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000959 if (vol->password == NULL) {
Steve French50c2f752007-07-13 00:33:32 +0000960 printk(KERN_WARNING "CIFS: no memory "
961 "for password\n");
Steve French433dc242005-04-28 22:41:08 -0700962 return 1;
963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 strcpy(vol->password, value);
965 }
966 } else if (strnicmp(data, "ip", 2) == 0) {
967 if (!value || !*value) {
968 vol->UNCip = NULL;
969 } else if (strnlen(value, 35) < 35) {
970 vol->UNCip = value;
971 } else {
Steve French50c2f752007-07-13 00:33:32 +0000972 printk(KERN_WARNING "CIFS: ip address "
973 "too long\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return 1;
975 }
Steve French50c2f752007-07-13 00:33:32 +0000976 } else if (strnicmp(data, "sec", 3) == 0) {
977 if (!value || !*value) {
978 cERROR(1, ("no security value specified"));
979 continue;
980 } else if (strnicmp(value, "krb5i", 5) == 0) {
981 vol->secFlg |= CIFSSEC_MAY_KRB5 |
Steve French189acaa2006-06-23 02:33:48 +0000982 CIFSSEC_MUST_SIGN;
Steve Frenchbf820672005-12-01 22:32:42 -0800983 } else if (strnicmp(value, "krb5p", 5) == 0) {
Steve French50c2f752007-07-13 00:33:32 +0000984 /* vol->secFlg |= CIFSSEC_MUST_SEAL |
985 CIFSSEC_MAY_KRB5; */
986 cERROR(1, ("Krb5 cifs privacy not supported"));
Steve Frenchbf820672005-12-01 22:32:42 -0800987 return 1;
988 } else if (strnicmp(value, "krb5", 4) == 0) {
Steve French750d1152006-06-27 06:28:30 +0000989 vol->secFlg |= CIFSSEC_MAY_KRB5;
Steve Frenchbf820672005-12-01 22:32:42 -0800990 } else if (strnicmp(value, "ntlmv2i", 7) == 0) {
Steve French750d1152006-06-27 06:28:30 +0000991 vol->secFlg |= CIFSSEC_MAY_NTLMV2 |
Steve French189acaa2006-06-23 02:33:48 +0000992 CIFSSEC_MUST_SIGN;
Steve Frenchbf820672005-12-01 22:32:42 -0800993 } else if (strnicmp(value, "ntlmv2", 6) == 0) {
Steve French750d1152006-06-27 06:28:30 +0000994 vol->secFlg |= CIFSSEC_MAY_NTLMV2;
Steve Frenchbf820672005-12-01 22:32:42 -0800995 } else if (strnicmp(value, "ntlmi", 5) == 0) {
Steve French750d1152006-06-27 06:28:30 +0000996 vol->secFlg |= CIFSSEC_MAY_NTLM |
Steve French189acaa2006-06-23 02:33:48 +0000997 CIFSSEC_MUST_SIGN;
Steve Frenchbf820672005-12-01 22:32:42 -0800998 } else if (strnicmp(value, "ntlm", 4) == 0) {
999 /* ntlm is default so can be turned off too */
Steve French750d1152006-06-27 06:28:30 +00001000 vol->secFlg |= CIFSSEC_MAY_NTLM;
Steve Frenchbf820672005-12-01 22:32:42 -08001001 } else if (strnicmp(value, "nontlm", 6) == 0) {
Steve French189acaa2006-06-23 02:33:48 +00001002 /* BB is there a better way to do this? */
Steve French750d1152006-06-27 06:28:30 +00001003 vol->secFlg |= CIFSSEC_MAY_NTLMV2;
Steve French189acaa2006-06-23 02:33:48 +00001004#ifdef CONFIG_CIFS_WEAK_PW_HASH
1005 } else if (strnicmp(value, "lanman", 6) == 0) {
Steve French50c2f752007-07-13 00:33:32 +00001006 vol->secFlg |= CIFSSEC_MAY_LANMAN;
Steve French189acaa2006-06-23 02:33:48 +00001007#endif
Steve Frenchbf820672005-12-01 22:32:42 -08001008 } else if (strnicmp(value, "none", 4) == 0) {
Steve French189acaa2006-06-23 02:33:48 +00001009 vol->nullauth = 1;
Steve French50c2f752007-07-13 00:33:32 +00001010 } else {
1011 cERROR(1, ("bad security option: %s", value));
1012 return 1;
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 } else if ((strnicmp(data, "unc", 3) == 0)
1015 || (strnicmp(data, "target", 6) == 0)
1016 || (strnicmp(data, "path", 4) == 0)) {
1017 if (!value || !*value) {
Steve French50c2f752007-07-13 00:33:32 +00001018 printk(KERN_WARNING "CIFS: invalid path to "
1019 "network resource\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return 1; /* needs_arg; */
1021 }
1022 if ((temp_len = strnlen(value, 300)) < 300) {
Steve French50c2f752007-07-13 00:33:32 +00001023 vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
Steve French4523cc32007-04-30 20:13:06 +00001024 if (vol->UNC == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return 1;
Steve French50c2f752007-07-13 00:33:32 +00001026 strcpy(vol->UNC, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (strncmp(vol->UNC, "//", 2) == 0) {
1028 vol->UNC[0] = '\\';
1029 vol->UNC[1] = '\\';
Steve French50c2f752007-07-13 00:33:32 +00001030 } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 printk(KERN_WARNING
Steve French50c2f752007-07-13 00:33:32 +00001032 "CIFS: UNC Path does not begin "
1033 "with // or \\\\ \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return 1;
1035 }
1036 } else {
1037 printk(KERN_WARNING "CIFS: UNC name too long\n");
1038 return 1;
1039 }
1040 } else if ((strnicmp(data, "domain", 3) == 0)
1041 || (strnicmp(data, "workgroup", 5) == 0)) {
1042 if (!value || !*value) {
1043 printk(KERN_WARNING "CIFS: invalid domain name\n");
1044 return 1; /* needs_arg; */
1045 }
1046 /* BB are there cases in which a comma can be valid in
1047 a domain name and need special handling? */
Steve French39798772006-05-31 22:40:51 +00001048 if (strnlen(value, 256) < 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 vol->domainname = value;
1050 cFYI(1, ("Domain name set"));
1051 } else {
Steve French50c2f752007-07-13 00:33:32 +00001052 printk(KERN_WARNING "CIFS: domain name too "
1053 "long\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return 1;
1055 }
Steve French50c2f752007-07-13 00:33:32 +00001056 } else if (strnicmp(data, "prefixpath", 10) == 0) {
1057 if (!value || !*value) {
1058 printk(KERN_WARNING
1059 "CIFS: invalid path prefix\n");
1060 return 1; /* needs_argument */
1061 }
1062 if ((temp_len = strnlen(value, 1024)) < 1024) {
Steve French4523cc32007-04-30 20:13:06 +00001063 if (value[0] != '/')
Steve French2fe87f02006-09-21 07:02:52 +00001064 temp_len++; /* missing leading slash */
Steve French50c2f752007-07-13 00:33:32 +00001065 vol->prepath = kmalloc(temp_len+1, GFP_KERNEL);
1066 if (vol->prepath == NULL)
1067 return 1;
Steve French4523cc32007-04-30 20:13:06 +00001068 if (value[0] != '/') {
Steve French2fe87f02006-09-21 07:02:52 +00001069 vol->prepath[0] = '/';
Steve French50c2f752007-07-13 00:33:32 +00001070 strcpy(vol->prepath+1, value);
Steve French2fe87f02006-09-21 07:02:52 +00001071 } else
Steve French50c2f752007-07-13 00:33:32 +00001072 strcpy(vol->prepath, value);
1073 cFYI(1, ("prefix path %s", vol->prepath));
1074 } else {
1075 printk(KERN_WARNING "CIFS: prefix too long\n");
1076 return 1;
1077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 } else if (strnicmp(data, "iocharset", 9) == 0) {
1079 if (!value || !*value) {
Steve French63135e02007-07-17 17:34:02 +00001080 printk(KERN_WARNING "CIFS: invalid iocharset "
1081 "specified\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return 1; /* needs_arg; */
1083 }
1084 if (strnlen(value, 65) < 65) {
Steve French50c2f752007-07-13 00:33:32 +00001085 if (strnicmp(value, "default", 7))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 vol->iocharset = value;
Steve French50c2f752007-07-13 00:33:32 +00001087 /* if iocharset not set then load_nls_default
1088 is used by caller */
1089 cFYI(1, ("iocharset set to %s", value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 } else {
Steve French63135e02007-07-17 17:34:02 +00001091 printk(KERN_WARNING "CIFS: iocharset name "
1092 "too long.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return 1;
1094 }
1095 } else if (strnicmp(data, "uid", 3) == 0) {
1096 if (value && *value) {
1097 vol->linux_uid =
1098 simple_strtoul(value, &value, 0);
Steve French4523cc32007-04-30 20:13:06 +00001099 vol->override_uid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101 } else if (strnicmp(data, "gid", 3) == 0) {
1102 if (value && *value) {
1103 vol->linux_gid =
1104 simple_strtoul(value, &value, 0);
Steve French4523cc32007-04-30 20:13:06 +00001105 vol->override_gid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107 } else if (strnicmp(data, "file_mode", 4) == 0) {
1108 if (value && *value) {
1109 vol->file_mode =
1110 simple_strtoul(value, &value, 0);
1111 }
1112 } else if (strnicmp(data, "dir_mode", 4) == 0) {
1113 if (value && *value) {
1114 vol->dir_mode =
1115 simple_strtoul(value, &value, 0);
1116 }
1117 } else if (strnicmp(data, "dirmode", 4) == 0) {
1118 if (value && *value) {
1119 vol->dir_mode =
1120 simple_strtoul(value, &value, 0);
1121 }
1122 } else if (strnicmp(data, "port", 4) == 0) {
1123 if (value && *value) {
1124 vol->port =
1125 simple_strtoul(value, &value, 0);
1126 }
1127 } else if (strnicmp(data, "rsize", 5) == 0) {
1128 if (value && *value) {
1129 vol->rsize =
1130 simple_strtoul(value, &value, 0);
1131 }
1132 } else if (strnicmp(data, "wsize", 5) == 0) {
1133 if (value && *value) {
1134 vol->wsize =
1135 simple_strtoul(value, &value, 0);
1136 }
1137 } else if (strnicmp(data, "sockopt", 5) == 0) {
1138 if (value && *value) {
1139 vol->sockopt =
1140 simple_strtoul(value, &value, 0);
1141 }
1142 } else if (strnicmp(data, "netbiosname", 4) == 0) {
1143 if (!value || !*value || (*value == ' ')) {
Steve French63135e02007-07-17 17:34:02 +00001144 cFYI(1, ("invalid (empty) netbiosname"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 } else {
Steve French50c2f752007-07-13 00:33:32 +00001146 memset(vol->source_rfc1001_name, 0x20, 15);
1147 for (i = 0; i < 15; i++) {
1148 /* BB are there cases in which a comma can be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 valid in this workstation netbios name (and need
1150 special handling)? */
1151
1152 /* We do not uppercase netbiosname for user */
Steve French50c2f752007-07-13 00:33:32 +00001153 if (value[i] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 break;
Steve French50c2f752007-07-13 00:33:32 +00001155 else
1156 vol->source_rfc1001_name[i] =
1157 value[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159 /* The string has 16th byte zero still from
1160 set at top of the function */
Steve French50c2f752007-07-13 00:33:32 +00001161 if ((i == 15) && (value[i] != 0))
1162 printk(KERN_WARNING "CIFS: netbiosname"
1163 " longer than 15 truncated.\n");
Steve Frencha10faeb22005-08-22 21:38:31 -07001164 }
1165 } else if (strnicmp(data, "servern", 7) == 0) {
1166 /* servernetbiosname specified override *SMBSERVER */
1167 if (!value || !*value || (*value == ' ')) {
Steve French467a8f82007-06-27 22:41:32 +00001168 cFYI(1, ("empty server netbiosname specified"));
Steve Frencha10faeb22005-08-22 21:38:31 -07001169 } else {
1170 /* last byte, type, is 0x20 for servr type */
Steve French50c2f752007-07-13 00:33:32 +00001171 memset(vol->target_rfc1001_name, 0x20, 16);
Steve Frencha10faeb22005-08-22 21:38:31 -07001172
Steve French50c2f752007-07-13 00:33:32 +00001173 for (i = 0; i < 15; i++) {
Steve Frencha10faeb22005-08-22 21:38:31 -07001174 /* BB are there cases in which a comma can be
Steve French50c2f752007-07-13 00:33:32 +00001175 valid in this workstation netbios name
1176 (and need special handling)? */
Steve Frencha10faeb22005-08-22 21:38:31 -07001177
Steve French50c2f752007-07-13 00:33:32 +00001178 /* user or mount helper must uppercase
1179 the netbiosname */
1180 if (value[i] == 0)
Steve Frencha10faeb22005-08-22 21:38:31 -07001181 break;
1182 else
Steve French50c2f752007-07-13 00:33:32 +00001183 vol->target_rfc1001_name[i] =
1184 value[i];
Steve Frencha10faeb22005-08-22 21:38:31 -07001185 }
1186 /* The string has 16th byte zero still from
1187 set at top of the function */
Steve French50c2f752007-07-13 00:33:32 +00001188 if ((i == 15) && (value[i] != 0))
1189 printk(KERN_WARNING "CIFS: server net"
1190 "biosname longer than 15 truncated.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192 } else if (strnicmp(data, "credentials", 4) == 0) {
1193 /* ignore */
1194 } else if (strnicmp(data, "version", 3) == 0) {
1195 /* ignore */
Steve French50c2f752007-07-13 00:33:32 +00001196 } else if (strnicmp(data, "guest", 5) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 /* ignore */
1198 } else if (strnicmp(data, "rw", 2) == 0) {
Steve French4b18f2a2008-04-29 00:06:05 +00001199 vol->rw = true;
Steve Frenchedf1ae42008-10-29 00:47:57 +00001200 } else if (strnicmp(data, "noblocksend", 11) == 0) {
1201 vol->noblocksnd = 1;
1202 } else if (strnicmp(data, "noautotune", 10) == 0) {
1203 vol->noautotune = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 } else if ((strnicmp(data, "suid", 4) == 0) ||
1205 (strnicmp(data, "nosuid", 6) == 0) ||
1206 (strnicmp(data, "exec", 4) == 0) ||
1207 (strnicmp(data, "noexec", 6) == 0) ||
1208 (strnicmp(data, "nodev", 5) == 0) ||
1209 (strnicmp(data, "noauto", 6) == 0) ||
1210 (strnicmp(data, "dev", 3) == 0)) {
1211 /* The mount tool or mount.cifs helper (if present)
Steve French50c2f752007-07-13 00:33:32 +00001212 uses these opts to set flags, and the flags are read
1213 by the kernel vfs layer before we get here (ie
1214 before read super) so there is no point trying to
1215 parse these options again and set anything and it
1216 is ok to just ignore them */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 continue;
1218 } else if (strnicmp(data, "ro", 2) == 0) {
Steve French4b18f2a2008-04-29 00:06:05 +00001219 vol->rw = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 } else if (strnicmp(data, "hard", 4) == 0) {
1221 vol->retry = 1;
1222 } else if (strnicmp(data, "soft", 4) == 0) {
1223 vol->retry = 0;
1224 } else if (strnicmp(data, "perm", 4) == 0) {
1225 vol->noperm = 0;
1226 } else if (strnicmp(data, "noperm", 6) == 0) {
1227 vol->noperm = 1;
Steve French6a0b4822005-04-28 22:41:05 -07001228 } else if (strnicmp(data, "mapchars", 8) == 0) {
1229 vol->remap = 1;
1230 } else if (strnicmp(data, "nomapchars", 10) == 0) {
1231 vol->remap = 0;
Steve French50c2f752007-07-13 00:33:32 +00001232 } else if (strnicmp(data, "sfu", 3) == 0) {
1233 vol->sfu_emul = 1;
1234 } else if (strnicmp(data, "nosfu", 5) == 0) {
1235 vol->sfu_emul = 0;
Steve French2c1b8612008-10-16 18:35:21 +00001236 } else if (strnicmp(data, "nodfs", 5) == 0) {
1237 vol->nodfs = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001238 } else if (strnicmp(data, "posixpaths", 10) == 0) {
1239 vol->posix_paths = 1;
1240 } else if (strnicmp(data, "noposixpaths", 12) == 0) {
1241 vol->posix_paths = 0;
Steve Frenchc18c8422007-07-18 23:21:09 +00001242 } else if (strnicmp(data, "nounix", 6) == 0) {
1243 vol->no_linux_ext = 1;
1244 } else if (strnicmp(data, "nolinux", 7) == 0) {
1245 vol->no_linux_ext = 1;
Steve French50c2f752007-07-13 00:33:32 +00001246 } else if ((strnicmp(data, "nocase", 6) == 0) ||
Steve Frencha10faeb22005-08-22 21:38:31 -07001247 (strnicmp(data, "ignorecase", 10) == 0)) {
Steve French50c2f752007-07-13 00:33:32 +00001248 vol->nocase = 1;
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001249 } else if (strnicmp(data, "brl", 3) == 0) {
1250 vol->nobrl = 0;
Steve French50c2f752007-07-13 00:33:32 +00001251 } else if ((strnicmp(data, "nobrl", 5) == 0) ||
Steve French1c955182005-08-30 20:58:07 -07001252 (strnicmp(data, "nolock", 6) == 0)) {
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001253 vol->nobrl = 1;
Steve Frenchd3485d32005-08-19 11:04:29 -07001254 /* turn off mandatory locking in mode
1255 if remote locking is turned off since the
1256 local vfs will do advisory */
Steve French50c2f752007-07-13 00:33:32 +00001257 if (vol->file_mode ==
1258 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001259 vol->file_mode = S_IALLUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 } else if (strnicmp(data, "setuids", 7) == 0) {
1261 vol->setuids = 1;
1262 } else if (strnicmp(data, "nosetuids", 9) == 0) {
1263 vol->setuids = 0;
Jeff Laytond0a9c072008-05-12 22:23:49 +00001264 } else if (strnicmp(data, "dynperm", 7) == 0) {
1265 vol->dynperm = true;
1266 } else if (strnicmp(data, "nodynperm", 9) == 0) {
1267 vol->dynperm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 } else if (strnicmp(data, "nohard", 6) == 0) {
1269 vol->retry = 0;
1270 } else if (strnicmp(data, "nosoft", 6) == 0) {
1271 vol->retry = 1;
1272 } else if (strnicmp(data, "nointr", 6) == 0) {
1273 vol->intr = 0;
1274 } else if (strnicmp(data, "intr", 4) == 0) {
1275 vol->intr = 1;
Steve French50c2f752007-07-13 00:33:32 +00001276 } else if (strnicmp(data, "serverino", 7) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 vol->server_ino = 1;
Steve French50c2f752007-07-13 00:33:32 +00001278 } else if (strnicmp(data, "noserverino", 9) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 vol->server_ino = 0;
Steve French50c2f752007-07-13 00:33:32 +00001280 } else if (strnicmp(data, "cifsacl", 7) == 0) {
Steve French0a4b92c2006-01-12 15:44:21 -08001281 vol->cifs_acl = 1;
1282 } else if (strnicmp(data, "nocifsacl", 9) == 0) {
1283 vol->cifs_acl = 0;
Steve French50c2f752007-07-13 00:33:32 +00001284 } else if (strnicmp(data, "acl", 3) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 vol->no_psx_acl = 0;
Steve French50c2f752007-07-13 00:33:32 +00001286 } else if (strnicmp(data, "noacl", 5) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 vol->no_psx_acl = 1;
Steve French84210e92008-10-23 04:42:37 +00001288#ifdef CONFIG_CIFS_EXPERIMENTAL
1289 } else if (strnicmp(data, "locallease", 6) == 0) {
1290 vol->local_lease = 1;
1291#endif
Steve French50c2f752007-07-13 00:33:32 +00001292 } else if (strnicmp(data, "sign", 4) == 0) {
Steve French750d1152006-06-27 06:28:30 +00001293 vol->secFlg |= CIFSSEC_MUST_SIGN;
Steve French95b1cb92008-05-15 16:44:38 +00001294 } else if (strnicmp(data, "seal", 4) == 0) {
1295 /* we do not do the following in secFlags because seal
1296 is a per tree connection (mount) not a per socket
1297 or per-smb connection option in the protocol */
1298 /* vol->secFlg |= CIFSSEC_MUST_SEAL; */
1299 vol->seal = 1;
Steve French50c2f752007-07-13 00:33:32 +00001300 } else if (strnicmp(data, "direct", 6) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 vol->direct_io = 1;
Steve French50c2f752007-07-13 00:33:32 +00001302 } else if (strnicmp(data, "forcedirectio", 13) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 vol->direct_io = 1;
Steve French50c2f752007-07-13 00:33:32 +00001304 } else if (strnicmp(data, "in6_addr", 8) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (!value || !*value) {
1306 vol->in6_addr = NULL;
1307 } else if (strnlen(value, 49) == 48) {
1308 vol->in6_addr = value;
1309 } else {
Steve French50c2f752007-07-13 00:33:32 +00001310 printk(KERN_WARNING "CIFS: ip v6 address not "
1311 "48 characters long\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return 1;
1313 }
1314 } else if (strnicmp(data, "noac", 4) == 0) {
Steve French50c2f752007-07-13 00:33:32 +00001315 printk(KERN_WARNING "CIFS: Mount option noac not "
1316 "supported. Instead set "
1317 "/proc/fs/cifs/LookupCacheEnabled to 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 } else
Steve French50c2f752007-07-13 00:33:32 +00001319 printk(KERN_WARNING "CIFS: Unknown mount option %s\n",
1320 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322 if (vol->UNC == NULL) {
Steve French4523cc32007-04-30 20:13:06 +00001323 if (devname == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00001324 printk(KERN_WARNING "CIFS: Missing UNC name for mount "
1325 "target\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return 1;
1327 }
1328 if ((temp_len = strnlen(devname, 300)) < 300) {
Steve French50c2f752007-07-13 00:33:32 +00001329 vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
Steve French4523cc32007-04-30 20:13:06 +00001330 if (vol->UNC == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return 1;
Steve French50c2f752007-07-13 00:33:32 +00001332 strcpy(vol->UNC, devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (strncmp(vol->UNC, "//", 2) == 0) {
1334 vol->UNC[0] = '\\';
1335 vol->UNC[1] = '\\';
1336 } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
Steve French50c2f752007-07-13 00:33:32 +00001337 printk(KERN_WARNING "CIFS: UNC Path does not "
1338 "begin with // or \\\\ \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return 1;
1340 }
Igor Mammedov7c5e6282008-05-08 20:48:42 +00001341 value = strpbrk(vol->UNC+2, "/\\");
1342 if (value)
1343 *value = '\\';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 } else {
1345 printk(KERN_WARNING "CIFS: UNC name too long\n");
1346 return 1;
1347 }
1348 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001349 if (vol->UNCip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 vol->UNCip = &vol->UNC[2];
1351
1352 return 0;
1353}
1354
1355static struct cifsSesInfo *
Steve French50c2f752007-07-13 00:33:32 +00001356cifs_find_tcp_session(struct in_addr *target_ip_addr,
Cyrill Gorcunov1b20d672008-05-09 18:17:21 +00001357 struct in6_addr *target_ip6_addr,
1358 char *userName, struct TCP_Server_Info **psrvTcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 struct list_head *tmp;
1361 struct cifsSesInfo *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Cyrill Gorcunov1b20d672008-05-09 18:17:21 +00001363 *psrvTcp = NULL;
1364
1365 read_lock(&GlobalSMBSeslock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 list_for_each(tmp, &GlobalSMBSessionList) {
1367 ses = list_entry(tmp, struct cifsSesInfo, cifsSessionList);
Cyrill Gorcunov1b20d672008-05-09 18:17:21 +00001368 if (!ses->server)
1369 continue;
Steve French50c2f752007-07-13 00:33:32 +00001370
Cyrill Gorcunov1b20d672008-05-09 18:17:21 +00001371 if (target_ip_addr &&
1372 ses->server->addr.sockAddr.sin_addr.s_addr != target_ip_addr->s_addr)
1373 continue;
1374 else if (target_ip6_addr &&
1375 memcmp(&ses->server->addr.sockAddr6.sin6_addr,
1376 target_ip6_addr, sizeof(*target_ip6_addr)))
1377 continue;
Jeff Layton02eadef2008-05-09 21:26:11 +00001378 /* BB lock server and tcp session; increment use count here?? */
Steve French50c2f752007-07-13 00:33:32 +00001379
Cyrill Gorcunov1b20d672008-05-09 18:17:21 +00001380 /* found a match on the TCP session */
1381 *psrvTcp = ses->server;
1382
1383 /* BB check if reconnection needed */
1384 if (strncmp(ses->userName, userName, MAX_USERNAME_SIZE) == 0) {
1385 read_unlock(&GlobalSMBSeslock);
1386 /* Found exact match on both TCP and
1387 SMB sessions */
1388 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390 /* else tcp and smb sessions need reconnection */
1391 }
1392 read_unlock(&GlobalSMBSeslock);
Cyrill Gorcunov1b20d672008-05-09 18:17:21 +00001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return NULL;
1395}
1396
1397static struct cifsTconInfo *
1398find_unc(__be32 new_target_ip_addr, char *uncName, char *userName)
1399{
1400 struct list_head *tmp;
1401 struct cifsTconInfo *tcon;
Steve Frenchdea570e02008-05-06 22:05:51 +00001402 __be32 old_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 read_lock(&GlobalSMBSeslock);
Steve Frenchdea570e02008-05-06 22:05:51 +00001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 list_for_each(tmp, &GlobalTreeConnectionList) {
Steve Frenche466e482006-08-15 13:07:18 +00001407 cFYI(1, ("Next tcon"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 tcon = list_entry(tmp, struct cifsTconInfo, cifsConnectionList);
Steve Frenchdea570e02008-05-06 22:05:51 +00001409 if (!tcon->ses || !tcon->ses->server)
1410 continue;
1411
1412 old_ip = tcon->ses->server->addr.sockAddr.sin_addr.s_addr;
1413 cFYI(1, ("old ip addr: %x == new ip %x ?",
1414 old_ip, new_target_ip_addr));
1415
1416 if (old_ip != new_target_ip_addr)
1417 continue;
1418
1419 /* BB lock tcon, server, tcp session and increment use count? */
1420 /* found a match on the TCP session */
1421 /* BB check if reconnection needed */
1422 cFYI(1, ("IP match, old UNC: %s new: %s",
1423 tcon->treeName, uncName));
1424
1425 if (strncmp(tcon->treeName, uncName, MAX_TREE_SIZE))
1426 continue;
1427
1428 cFYI(1, ("and old usr: %s new: %s",
1429 tcon->treeName, uncName));
1430
1431 if (strncmp(tcon->ses->userName, userName, MAX_USERNAME_SIZE))
1432 continue;
1433
1434 /* matched smb session (user name) */
1435 read_unlock(&GlobalSMBSeslock);
1436 return tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
Steve Frenchdea570e02008-05-06 22:05:51 +00001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 read_unlock(&GlobalSMBSeslock);
1440 return NULL;
1441}
1442
1443int
Steve French50c2f752007-07-13 00:33:32 +00001444get_dfs_path(int xid, struct cifsSesInfo *pSesInfo, const char *old_path,
1445 const struct nls_table *nls_codepage, unsigned int *pnum_referrals,
Steve French366781c2008-01-25 10:12:41 +00001446 struct dfs_info3_param **preferrals, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 char *temp_unc;
1449 int rc = 0;
1450
1451 *pnum_referrals = 0;
Steve French366781c2008-01-25 10:12:41 +00001452 *preferrals = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 if (pSesInfo->ipc_tid == 0) {
1455 temp_unc = kmalloc(2 /* for slashes */ +
Steve French50c2f752007-07-13 00:33:32 +00001456 strnlen(pSesInfo->serverName,
1457 SERVER_NAME_LEN_WITH_NULL * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 + 1 + 4 /* slash IPC$ */ + 2,
1459 GFP_KERNEL);
1460 if (temp_unc == NULL)
1461 return -ENOMEM;
1462 temp_unc[0] = '\\';
1463 temp_unc[1] = '\\';
1464 strcpy(temp_unc + 2, pSesInfo->serverName);
1465 strcpy(temp_unc + 2 + strlen(pSesInfo->serverName), "\\IPC$");
1466 rc = CIFSTCon(xid, pSesInfo, temp_unc, NULL, nls_codepage);
1467 cFYI(1,
Steve French50c2f752007-07-13 00:33:32 +00001468 ("CIFS Tcon rc = %d ipc_tid = %d", rc, pSesInfo->ipc_tid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 kfree(temp_unc);
1470 }
1471 if (rc == 0)
Steve Frenchc2cf07d2008-05-15 06:20:02 +00001472 rc = CIFSGetDFSRefer(xid, pSesInfo, old_path, preferrals,
Steve French737b7582005-04-28 22:41:06 -07001473 pnum_referrals, nls_codepage, remap);
Steve French366781c2008-01-25 10:12:41 +00001474 /* BB map targetUNCs to dfs_info3 structures, here or
1475 in CIFSGetDFSRefer BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 return rc;
1478}
1479
Jeff Layton09e50d52008-07-23 10:11:19 -04001480#ifdef CONFIG_DEBUG_LOCK_ALLOC
1481static struct lock_class_key cifs_key[2];
1482static struct lock_class_key cifs_slock_key[2];
1483
1484static inline void
1485cifs_reclassify_socket4(struct socket *sock)
1486{
1487 struct sock *sk = sock->sk;
1488 BUG_ON(sock_owned_by_user(sk));
1489 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
1490 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
1491}
1492
1493static inline void
1494cifs_reclassify_socket6(struct socket *sock)
1495{
1496 struct sock *sk = sock->sk;
1497 BUG_ON(sock_owned_by_user(sk));
1498 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
1499 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
1500}
1501#else
1502static inline void
1503cifs_reclassify_socket4(struct socket *sock)
1504{
1505}
1506
1507static inline void
1508cifs_reclassify_socket6(struct socket *sock)
1509{
1510}
1511#endif
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00001514static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Steve French50c2f752007-07-13 00:33:32 +00001516 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Steve French50c2f752007-07-13 00:33:32 +00001518 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 /* mask a nibble at a time and encode */
1520 target[j] = 'A' + (0x0F & (source[i] >> 4));
1521 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00001522 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524
1525}
1526
1527
1528static int
Steve French50c2f752007-07-13 00:33:32 +00001529ipv4_connect(struct sockaddr_in *psin_server, struct socket **csocket,
Steve Frenchedf1ae42008-10-29 00:47:57 +00001530 char *netbios_name, char *target_name,
1531 bool noblocksnd, bool noautotune)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
1533 int rc = 0;
1534 int connected = 0;
1535 __be16 orig_port = 0;
1536
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001537 if (*csocket == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00001538 rc = sock_create_kern(PF_INET, SOCK_STREAM,
1539 IPPROTO_TCP, csocket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (rc < 0) {
Steve French50c2f752007-07-13 00:33:32 +00001541 cERROR(1, ("Error %d creating socket", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 *csocket = NULL;
1543 return rc;
1544 } else {
1545 /* BB other socket options to set KEEPALIVE, NODELAY? */
Steve French467a8f82007-06-27 22:41:32 +00001546 cFYI(1, ("Socket created"));
Steve French50c2f752007-07-13 00:33:32 +00001547 (*csocket)->sk->sk_allocation = GFP_NOFS;
Jeff Layton09e50d52008-07-23 10:11:19 -04001548 cifs_reclassify_socket4(*csocket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550 }
1551
1552 psin_server->sin_family = AF_INET;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001553 if (psin_server->sin_port) { /* user overrode default port */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 rc = (*csocket)->ops->connect(*csocket,
1555 (struct sockaddr *) psin_server,
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00001556 sizeof(struct sockaddr_in), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (rc >= 0)
1558 connected = 1;
Steve French50c2f752007-07-13 00:33:32 +00001559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001561 if (!connected) {
Steve French50c2f752007-07-13 00:33:32 +00001562 /* save original port so we can retry user specified port
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 later if fall back ports fail this time */
1564 orig_port = psin_server->sin_port;
1565
1566 /* do not retry on the same port we just failed on */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001567 if (psin_server->sin_port != htons(CIFS_PORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 psin_server->sin_port = htons(CIFS_PORT);
1569
1570 rc = (*csocket)->ops->connect(*csocket,
1571 (struct sockaddr *) psin_server,
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00001572 sizeof(struct sockaddr_in), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (rc >= 0)
1574 connected = 1;
1575 }
1576 }
1577 if (!connected) {
1578 psin_server->sin_port = htons(RFC1001_PORT);
1579 rc = (*csocket)->ops->connect(*csocket, (struct sockaddr *)
Steve French50c2f752007-07-13 00:33:32 +00001580 psin_server,
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00001581 sizeof(struct sockaddr_in), 0);
Steve French50c2f752007-07-13 00:33:32 +00001582 if (rc >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 connected = 1;
1584 }
1585
1586 /* give up here - unless we want to retry on different
1587 protocol families some day */
1588 if (!connected) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001589 if (orig_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 psin_server->sin_port = orig_port;
Steve French50c2f752007-07-13 00:33:32 +00001591 cFYI(1, ("Error %d connecting to server via ipv4", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 sock_release(*csocket);
1593 *csocket = NULL;
1594 return rc;
1595 }
Steve French50c2f752007-07-13 00:33:32 +00001596 /* Eventually check for other socket options to change from
1597 the default. sock_setsockopt not used because it expects
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 user space buffer */
Steve French50c2f752007-07-13 00:33:32 +00001599 cFYI(1, ("sndbuf %d rcvbuf %d rcvtimeo 0x%lx",
1600 (*csocket)->sk->sk_sndbuf,
Steve Frenchb387eae2005-10-10 14:21:15 -07001601 (*csocket)->sk->sk_rcvbuf, (*csocket)->sk->sk_rcvtimeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 (*csocket)->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchedf1ae42008-10-29 00:47:57 +00001603 if (!noblocksnd)
1604 (*csocket)->sk->sk_sndtimeo = 3 * HZ;
1605
Steve Frenchb387eae2005-10-10 14:21:15 -07001606 /* make the bufsizes depend on wsize/rsize and max requests */
Steve Frenchedf1ae42008-10-29 00:47:57 +00001607 if (noautotune) {
1608 if ((*csocket)->sk->sk_sndbuf < (200 * 1024))
1609 (*csocket)->sk->sk_sndbuf = 200 * 1024;
1610 if ((*csocket)->sk->sk_rcvbuf < (140 * 1024))
1611 (*csocket)->sk->sk_rcvbuf = 140 * 1024;
1612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 /* send RFC1001 sessinit */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001615 if (psin_server->sin_port == htons(RFC1001_PORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 /* some servers require RFC1001 sessinit before sending
Steve French50c2f752007-07-13 00:33:32 +00001617 negprot - BB check reconnection in case where second
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 sessinit is sent but no second negprot */
Steve French50c2f752007-07-13 00:33:32 +00001619 struct rfc1002_session_packet *ses_init_buf;
1620 struct smb_hdr *smb_buf;
1621 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
1622 GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001623 if (ses_init_buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 ses_init_buf->trailer.session_req.called_len = 32;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001625 if (target_name && (target_name[0] != 0)) {
Steve Frencha10faeb22005-08-22 21:38:31 -07001626 rfc1002mangle(ses_init_buf->trailer.session_req.called_name,
1627 target_name, 16);
1628 } else {
1629 rfc1002mangle(ses_init_buf->trailer.session_req.called_name,
Steve French50c2f752007-07-13 00:33:32 +00001630 DEFAULT_CIFS_CALLED_NAME, 16);
Steve Frencha10faeb22005-08-22 21:38:31 -07001631 }
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 ses_init_buf->trailer.session_req.calling_len = 32;
1634 /* calling name ends in null (byte 16) from old smb
1635 convention. */
Steve French50c2f752007-07-13 00:33:32 +00001636 if (netbios_name && (netbios_name[0] != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 rfc1002mangle(ses_init_buf->trailer.session_req.calling_name,
Steve French50c2f752007-07-13 00:33:32 +00001638 netbios_name, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 } else {
1640 rfc1002mangle(ses_init_buf->trailer.session_req.calling_name,
Steve French50c2f752007-07-13 00:33:32 +00001641 "LINUX_CIFS_CLNT", 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643 ses_init_buf->trailer.session_req.scope1 = 0;
1644 ses_init_buf->trailer.session_req.scope2 = 0;
1645 smb_buf = (struct smb_hdr *)ses_init_buf;
1646 /* sizeof RFC1002_SESSION_REQUEST with no scope */
1647 smb_buf->smb_buf_length = 0x81000044;
1648 rc = smb_send(*csocket, smb_buf, 0x44,
Steve Frenchedf1ae42008-10-29 00:47:57 +00001649 (struct sockaddr *)psin_server, noblocksnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 kfree(ses_init_buf);
Steve French50c2f752007-07-13 00:33:32 +00001651 msleep(1); /* RFC1001 layer in at least one server
Steve French083d3a22006-03-03 09:53:36 +00001652 requires very short break before negprot
1653 presumably because not expecting negprot
1654 to follow so fast. This is a simple
Steve French50c2f752007-07-13 00:33:32 +00001655 solution that works without
Steve French083d3a22006-03-03 09:53:36 +00001656 complicating the code and causes no
1657 significant slowing down on mount
1658 for everyone else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
Steve French50c2f752007-07-13 00:33:32 +00001660 /* else the negprot may still work without this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 even though malloc failed */
Steve French50c2f752007-07-13 00:33:32 +00001662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
Steve French50c2f752007-07-13 00:33:32 +00001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return rc;
1666}
1667
1668static int
Steve Frenchedf1ae42008-10-29 00:47:57 +00001669ipv6_connect(struct sockaddr_in6 *psin_server, struct socket **csocket,
1670 bool noblocksnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 int rc = 0;
1673 int connected = 0;
1674 __be16 orig_port = 0;
1675
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001676 if (*csocket == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00001677 rc = sock_create_kern(PF_INET6, SOCK_STREAM,
1678 IPPROTO_TCP, csocket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (rc < 0) {
Steve French50c2f752007-07-13 00:33:32 +00001680 cERROR(1, ("Error %d creating ipv6 socket", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 *csocket = NULL;
1682 return rc;
1683 } else {
1684 /* BB other socket options to set KEEPALIVE, NODELAY? */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001685 cFYI(1, ("ipv6 Socket created"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 (*csocket)->sk->sk_allocation = GFP_NOFS;
Jeff Layton09e50d52008-07-23 10:11:19 -04001687 cifs_reclassify_socket6(*csocket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689 }
1690
1691 psin_server->sin6_family = AF_INET6;
1692
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001693 if (psin_server->sin6_port) { /* user overrode default port */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 rc = (*csocket)->ops->connect(*csocket,
1695 (struct sockaddr *) psin_server,
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00001696 sizeof(struct sockaddr_in6), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (rc >= 0)
1698 connected = 1;
Steve French50c2f752007-07-13 00:33:32 +00001699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001701 if (!connected) {
Steve French50c2f752007-07-13 00:33:32 +00001702 /* save original port so we can retry user specified port
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 later if fall back ports fail this time */
1704
1705 orig_port = psin_server->sin6_port;
1706 /* do not retry on the same port we just failed on */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001707 if (psin_server->sin6_port != htons(CIFS_PORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 psin_server->sin6_port = htons(CIFS_PORT);
1709
1710 rc = (*csocket)->ops->connect(*csocket,
1711 (struct sockaddr *) psin_server,
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00001712 sizeof(struct sockaddr_in6), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (rc >= 0)
1714 connected = 1;
1715 }
1716 }
1717 if (!connected) {
1718 psin_server->sin6_port = htons(RFC1001_PORT);
1719 rc = (*csocket)->ops->connect(*csocket, (struct sockaddr *)
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00001720 psin_server, sizeof(struct sockaddr_in6), 0);
Steve French50c2f752007-07-13 00:33:32 +00001721 if (rc >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 connected = 1;
1723 }
1724
1725 /* give up here - unless we want to retry on different
1726 protocol families some day */
1727 if (!connected) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001728 if (orig_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 psin_server->sin6_port = orig_port;
Steve French50c2f752007-07-13 00:33:32 +00001730 cFYI(1, ("Error %d connecting to server via ipv6", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 sock_release(*csocket);
1732 *csocket = NULL;
1733 return rc;
1734 }
Steve French50c2f752007-07-13 00:33:32 +00001735 /* Eventually check for other socket options to change from
1736 the default. sock_setsockopt not used because it expects
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 user space buffer */
1738 (*csocket)->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchedf1ae42008-10-29 00:47:57 +00001739 if (!noblocksnd)
1740 (*csocket)->sk->sk_sndtimeo = 3 * HZ;
1741
Steve French50c2f752007-07-13 00:33:32 +00001742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return rc;
1744}
1745
Steve French50c2f752007-07-13 00:33:32 +00001746void reset_cifs_unix_caps(int xid, struct cifsTconInfo *tcon,
1747 struct super_block *sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00001748{
1749 /* if we are reconnecting then should we check to see if
1750 * any requested capabilities changed locally e.g. via
1751 * remount but we can not do much about it here
1752 * if they have (even if we could detect it by the following)
1753 * Perhaps we could add a backpointer to array of sb from tcon
1754 * or if we change to make all sb to same share the same
1755 * sb as NFS - then we only have one backpointer to sb.
1756 * What if we wanted to mount the server share twice once with
1757 * and once without posixacls or posix paths? */
1758 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00001759
Steve Frenchc18c8422007-07-18 23:21:09 +00001760 if (vol_info && vol_info->no_linux_ext) {
1761 tcon->fsUnixInfo.Capability = 0;
1762 tcon->unix_ext = 0; /* Unix Extensions disabled */
1763 cFYI(1, ("Linux protocol extensions disabled"));
1764 return;
1765 } else if (vol_info)
1766 tcon->unix_ext = 1; /* Unix Extensions supported */
1767
1768 if (tcon->unix_ext == 0) {
1769 cFYI(1, ("Unix extensions disabled so not set on reconnect"));
1770 return;
1771 }
Steve French50c2f752007-07-13 00:33:32 +00001772
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001773 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00001774 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00001775
Steve French8af18972007-02-14 04:42:51 +00001776 /* check for reconnect case in which we do not
1777 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001778 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00001779 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00001780 originally at mount time */
1781 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
1782 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00001783 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
1784 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
1785 cERROR(1, ("POSIXPATH support change"));
Steve French8af18972007-02-14 04:42:51 +00001786 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00001787 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
1788 cERROR(1, ("possible reconnect error"));
1789 cERROR(1,
1790 ("server disabled POSIX path support"));
1791 }
Steve French8af18972007-02-14 04:42:51 +00001792 }
Steve French50c2f752007-07-13 00:33:32 +00001793
Steve French8af18972007-02-14 04:42:51 +00001794 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00001795 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00001796 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00001797 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001798 cFYI(1, ("negotiated posix acl support"));
1799 if (sb)
Steve French8af18972007-02-14 04:42:51 +00001800 sb->s_flags |= MS_POSIXACL;
1801 }
1802
Steve French75865f8c2007-06-24 18:30:48 +00001803 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00001804 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00001805 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001806 cFYI(1, ("negotiate posix pathnames"));
Steve French75865f8c2007-06-24 18:30:48 +00001807 if (sb)
Steve French50c2f752007-07-13 00:33:32 +00001808 CIFS_SB(sb)->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00001809 CIFS_MOUNT_POSIX_PATHS;
1810 }
Steve French50c2f752007-07-13 00:33:32 +00001811
Steve French984acfe2007-04-26 16:42:50 +00001812 /* We might be setting the path sep back to a different
1813 form if we are reconnecting and the server switched its
Steve French50c2f752007-07-13 00:33:32 +00001814 posix path capability for this share */
Steve French75865f8c2007-06-24 18:30:48 +00001815 if (sb && (CIFS_SB(sb)->prepathlen > 0))
Steve French984acfe2007-04-26 16:42:50 +00001816 CIFS_SB(sb)->prepath[0] = CIFS_DIR_SEP(CIFS_SB(sb));
Steve French75865f8c2007-06-24 18:30:48 +00001817
1818 if (sb && (CIFS_SB(sb)->rsize > 127 * 1024)) {
1819 if ((cap & CIFS_UNIX_LARGE_READ_CAP) == 0) {
1820 CIFS_SB(sb)->rsize = 127 * 1024;
Steve French90c81e02008-02-12 20:32:36 +00001821 cFYI(DBG2,
1822 ("larger reads not supported by srv"));
Steve French75865f8c2007-06-24 18:30:48 +00001823 }
1824 }
Steve French50c2f752007-07-13 00:33:32 +00001825
1826
1827 cFYI(1, ("Negotiate caps 0x%x", (int)cap));
Steve French8af18972007-02-14 04:42:51 +00001828#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00001829 if (cap & CIFS_UNIX_FCNTL_CAP)
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001830 cFYI(1, ("FCNTL cap"));
Steve French75865f8c2007-06-24 18:30:48 +00001831 if (cap & CIFS_UNIX_EXTATTR_CAP)
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001832 cFYI(1, ("EXTATTR cap"));
Steve French75865f8c2007-06-24 18:30:48 +00001833 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001834 cFYI(1, ("POSIX path cap"));
Steve French75865f8c2007-06-24 18:30:48 +00001835 if (cap & CIFS_UNIX_XATTR_CAP)
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001836 cFYI(1, ("XATTR cap"));
Steve French75865f8c2007-06-24 18:30:48 +00001837 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001838 cFYI(1, ("POSIX ACL cap"));
Steve French75865f8c2007-06-24 18:30:48 +00001839 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001840 cFYI(1, ("very large read cap"));
Steve French75865f8c2007-06-24 18:30:48 +00001841 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001842 cFYI(1, ("very large write cap"));
Steve French8af18972007-02-14 04:42:51 +00001843#endif /* CIFS_DEBUG2 */
1844 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00001845 if (vol_info == NULL) {
Steve French5a44b312007-09-20 15:16:24 +00001846 cFYI(1, ("resetting capabilities failed"));
Steve French442aa312007-09-24 20:25:46 +00001847 } else
Steve French5a44b312007-09-20 15:16:24 +00001848 cERROR(1, ("Negotiating Unix capabilities "
1849 "with the server failed. Consider "
1850 "mounting with the Unix Extensions\n"
1851 "disabled, if problems are found, "
1852 "by specifying the nounix mount "
Steve French2224f4e2007-09-20 15:37:29 +00001853 "option."));
Steve French5a44b312007-09-20 15:16:24 +00001854
Steve French8af18972007-02-14 04:42:51 +00001855 }
1856 }
1857}
1858
Steve French03a143c2008-02-14 06:38:30 +00001859static void
1860convert_delimiter(char *path, char delim)
1861{
1862 int i;
Steve Frenchc2d68ea2008-02-15 19:20:18 +00001863 char old_delim;
Steve French03a143c2008-02-14 06:38:30 +00001864
1865 if (path == NULL)
1866 return;
1867
Steve French582d21e2008-05-13 04:54:12 +00001868 if (delim == '/')
Steve Frenchc2d68ea2008-02-15 19:20:18 +00001869 old_delim = '\\';
1870 else
1871 old_delim = '/';
1872
Steve French03a143c2008-02-14 06:38:30 +00001873 for (i = 0; path[i] != '\0'; i++) {
Steve Frenchc2d68ea2008-02-15 19:20:18 +00001874 if (path[i] == old_delim)
Steve French03a143c2008-02-14 06:38:30 +00001875 path[i] = delim;
1876 }
1877}
1878
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04001879static void
1880kill_cifsd(struct TCP_Server_Info *server)
1881{
1882 struct task_struct *task;
1883
1884 task = xchg(&server->tsk, NULL);
1885 if (task)
1886 force_sig(SIGKILL, task);
1887}
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889int
1890cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
1891 char *mount_data, const char *devname)
1892{
1893 int rc = 0;
1894 int xid;
1895 int address_type = AF_INET;
1896 struct socket *csocket = NULL;
1897 struct sockaddr_in sin_server;
1898 struct sockaddr_in6 sin_server6;
1899 struct smb_vol volume_info;
1900 struct cifsSesInfo *pSesInfo = NULL;
1901 struct cifsSesInfo *existingCifsSes = NULL;
1902 struct cifsTconInfo *tcon = NULL;
1903 struct TCP_Server_Info *srvTcp = NULL;
1904
1905 xid = GetXid();
1906
1907/* cFYI(1, ("Entering cifs_mount. Xid: %d with: %s", xid, mount_data)); */
Steve French50c2f752007-07-13 00:33:32 +00001908
1909 memset(&volume_info, 0, sizeof(struct smb_vol));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (cifs_parse_mount_options(mount_data, devname, &volume_info)) {
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001911 rc = -EINVAL;
1912 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914
Jeff Layton8426c392007-05-05 03:27:49 +00001915 if (volume_info.nullauth) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001916 cFYI(1, ("null user"));
Jeff Layton9b8f5f52007-11-09 23:25:04 +00001917 volume_info.username = "";
Jeff Layton8426c392007-05-05 03:27:49 +00001918 } else if (volume_info.username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 /* BB fixme parse for domain name here */
Steve French467a8f82007-06-27 22:41:32 +00001920 cFYI(1, ("Username: %s", volume_info.username));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 } else {
Steve Frenchbf820672005-12-01 22:32:42 -08001922 cifserror("No username specified");
Steve French50c2f752007-07-13 00:33:32 +00001923 /* In userspace mount helper we can get user name from alternate
1924 locations such as env variables and files on disk */
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001925 rc = -EINVAL;
1926 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928
1929 if (volume_info.UNCip && volume_info.UNC) {
Steve French50c2f752007-07-13 00:33:32 +00001930 rc = cifs_inet_pton(AF_INET, volume_info.UNCip,
1931 &sin_server.sin_addr.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001933 if (rc <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 /* not ipv4 address, try ipv6 */
Steve French50c2f752007-07-13 00:33:32 +00001935 rc = cifs_inet_pton(AF_INET6, volume_info.UNCip,
1936 &sin_server6.sin6_addr.in6_u);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001937 if (rc > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 address_type = AF_INET6;
1939 } else {
1940 address_type = AF_INET;
1941 }
Steve French50c2f752007-07-13 00:33:32 +00001942
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001943 if (rc <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 /* we failed translating address */
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001945 rc = -EINVAL;
1946 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948
1949 cFYI(1, ("UNC: %s ip: %s", volume_info.UNC, volume_info.UNCip));
1950 /* success */
1951 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00001952 } else if (volume_info.UNCip) {
1953 /* BB using ip addr as server name to connect to the
1954 DFS root below */
1955 cERROR(1, ("Connecting to DFS root not implemented yet"));
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001956 rc = -EINVAL;
1957 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 } else /* which servers DFS root would we conect to */ {
1959 cERROR(1,
Steve French50c2f752007-07-13 00:33:32 +00001960 ("CIFS mount error: No UNC path (e.g. -o "
1961 "unc=//192.168.1.100/public) specified"));
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001962 rc = -EINVAL;
1963 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965
1966 /* this is needed for ASCII cp to Unicode converts */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001967 if (volume_info.iocharset == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 cifs_sb->local_nls = load_nls_default();
1969 /* load_nls_default can not return null */
1970 } else {
1971 cifs_sb->local_nls = load_nls(volume_info.iocharset);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001972 if (cifs_sb->local_nls == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00001973 cERROR(1, ("CIFS mount error: iocharset %s not found",
1974 volume_info.iocharset));
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001975 rc = -ELIBACC;
1976 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978 }
1979
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001980 if (address_type == AF_INET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 existingCifsSes = cifs_find_tcp_session(&sin_server.sin_addr,
1982 NULL /* no ipv6 addr */,
1983 volume_info.username, &srvTcp);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001984 else if (address_type == AF_INET6) {
1985 cFYI(1, ("looking for ipv6 address"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 existingCifsSes = cifs_find_tcp_session(NULL /* no ipv4 addr */,
1987 &sin_server6.sin6_addr,
1988 volume_info.username, &srvTcp);
Steve French5858ae42007-04-25 11:59:10 +00001989 } else {
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001990 rc = -EINVAL;
1991 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (srvTcp) {
Steve French50c2f752007-07-13 00:33:32 +00001995 cFYI(1, ("Existing tcp session with server found"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 } else { /* create socket */
Steve French4523cc32007-04-30 20:13:06 +00001997 if (volume_info.port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 sin_server.sin_port = htons(volume_info.port);
1999 else
2000 sin_server.sin_port = 0;
Steve French5858ae42007-04-25 11:59:10 +00002001 if (address_type == AF_INET6) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002002 cFYI(1, ("attempting ipv6 connect"));
Steve French5858ae42007-04-25 11:59:10 +00002003 /* BB should we allow ipv6 on port 139? */
2004 /* other OS never observed in Wild doing 139 with v6 */
Steve Frenchedf1ae42008-10-29 00:47:57 +00002005 rc = ipv6_connect(&sin_server6, &csocket,
2006 volume_info.noblocksnd);
Steve French50c2f752007-07-13 00:33:32 +00002007 } else
2008 rc = ipv4_connect(&sin_server, &csocket,
Steve Frencha10faeb22005-08-22 21:38:31 -07002009 volume_info.source_rfc1001_name,
Steve Frenchedf1ae42008-10-29 00:47:57 +00002010 volume_info.target_rfc1001_name,
2011 volume_info.noblocksnd,
2012 volume_info.noautotune);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (rc < 0) {
Steve French50c2f752007-07-13 00:33:32 +00002014 cERROR(1, ("Error connecting to IPv4 socket. "
2015 "Aborting operation"));
Steve French4523cc32007-04-30 20:13:06 +00002016 if (csocket != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 sock_release(csocket);
Jeff Layton70fe7dc2007-11-16 22:21:07 +00002018 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00002021 srvTcp = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2022 if (!srvTcp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 rc = -ENOMEM;
2024 sock_release(csocket);
Jeff Layton70fe7dc2007-11-16 22:21:07 +00002025 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 } else {
Steve Frenchedf1ae42008-10-29 00:47:57 +00002027 srvTcp->noblocksnd = volume_info.noblocksnd;
2028 srvTcp->noautotune = volume_info.noautotune;
Steve French50c2f752007-07-13 00:33:32 +00002029 memcpy(&srvTcp->addr.sockAddr, &sin_server,
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00002030 sizeof(struct sockaddr_in));
Steve French50c2f752007-07-13 00:33:32 +00002031 atomic_set(&srvTcp->inFlight, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 /* BB Add code for ipv6 case too */
2033 srvTcp->ssocket = csocket;
2034 srvTcp->protocolType = IPV4;
Jeff Laytonc359cf32007-11-16 22:22:06 +00002035 srvTcp->hostname = extract_hostname(volume_info.UNC);
2036 if (IS_ERR(srvTcp->hostname)) {
2037 rc = PTR_ERR(srvTcp->hostname);
2038 sock_release(csocket);
2039 goto out;
2040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 init_waitqueue_head(&srvTcp->response_q);
2042 init_waitqueue_head(&srvTcp->request_q);
2043 INIT_LIST_HEAD(&srvTcp->pending_mid_q);
2044 /* at this point we are the only ones with the pointer
2045 to the struct since the kernel thread not created yet
2046 so no need to spinlock this init of tcpStatus */
2047 srvTcp->tcpStatus = CifsNew;
2048 init_MUTEX(&srvTcp->tcpSem);
Igor Mammedovaaf737a2007-04-03 19:16:43 +00002049 srvTcp->tsk = kthread_run((void *)(void *)cifs_demultiplex_thread, srvTcp, "cifsd");
Steve French8840dee2007-11-16 23:05:52 +00002050 if (IS_ERR(srvTcp->tsk)) {
Igor Mammedovaaf737a2007-04-03 19:16:43 +00002051 rc = PTR_ERR(srvTcp->tsk);
Steve French50c2f752007-07-13 00:33:32 +00002052 cERROR(1, ("error %d create cifsd thread", rc));
Igor Mammedovaaf737a2007-04-03 19:16:43 +00002053 srvTcp->tsk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 sock_release(csocket);
Jeff Laytonc359cf32007-11-16 22:22:06 +00002055 kfree(srvTcp->hostname);
Jeff Layton70fe7dc2007-11-16 22:21:07 +00002056 goto out;
Steve Frenchf1914012005-08-18 09:37:34 -07002057 }
Steve Frenchf1914012005-08-18 09:37:34 -07002058 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00002059 memcpy(srvTcp->workstation_RFC1001_name,
2060 volume_info.source_rfc1001_name, 16);
2061 memcpy(srvTcp->server_RFC1001_name,
2062 volume_info.target_rfc1001_name, 16);
Steve Frenchad009ac2005-04-28 22:41:05 -07002063 srvTcp->sequence_number = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065 }
2066
2067 if (existingCifsSes) {
2068 pSesInfo = existingCifsSes;
Jeff Layton1d9a8852007-12-31 01:37:11 +00002069 cFYI(1, ("Existing smb sess found (status=%d)",
2070 pSesInfo->status));
Steve French88e7d702008-01-03 17:37:09 +00002071 down(&pSesInfo->sesSem);
Jeff Layton1d9a8852007-12-31 01:37:11 +00002072 if (pSesInfo->status == CifsNeedReconnect) {
2073 cFYI(1, ("Session needs reconnect"));
Jeff Layton1d9a8852007-12-31 01:37:11 +00002074 rc = cifs_setup_session(xid, pSesInfo,
2075 cifs_sb->local_nls);
Jeff Layton1d9a8852007-12-31 01:37:11 +00002076 }
Steve French88e7d702008-01-03 17:37:09 +00002077 up(&pSesInfo->sesSem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 } else if (!rc) {
Steve Frenchbf820672005-12-01 22:32:42 -08002079 cFYI(1, ("Existing smb sess not found"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 pSesInfo = sesInfoAlloc();
2081 if (pSesInfo == NULL)
2082 rc = -ENOMEM;
2083 else {
2084 pSesInfo->server = srvTcp;
2085 sprintf(pSesInfo->serverName, "%u.%u.%u.%u",
2086 NIPQUAD(sin_server.sin_addr.s_addr));
2087 }
2088
Steve French50c2f752007-07-13 00:33:32 +00002089 if (!rc) {
2090 /* volume_info.password freed at unmount */
Jeff Layton70fe7dc2007-11-16 22:21:07 +00002091 if (volume_info.password) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 pSesInfo->password = volume_info.password;
Jeff Layton70fe7dc2007-11-16 22:21:07 +00002093 /* set to NULL to prevent freeing on exit */
2094 volume_info.password = NULL;
2095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (volume_info.username)
2097 strncpy(pSesInfo->userName,
Steve French50c2f752007-07-13 00:33:32 +00002098 volume_info.username,
2099 MAX_USERNAME_SIZE);
Steve French39798772006-05-31 22:40:51 +00002100 if (volume_info.domainname) {
2101 int len = strlen(volume_info.domainname);
Steve French50c2f752007-07-13 00:33:32 +00002102 pSesInfo->domainName =
Steve French39798772006-05-31 22:40:51 +00002103 kmalloc(len + 1, GFP_KERNEL);
Steve French4523cc32007-04-30 20:13:06 +00002104 if (pSesInfo->domainName)
Steve French39798772006-05-31 22:40:51 +00002105 strcpy(pSesInfo->domainName,
2106 volume_info.domainname);
2107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 pSesInfo->linux_uid = volume_info.linux_uid;
Steve French750d1152006-06-27 06:28:30 +00002109 pSesInfo->overrideSecFlg = volume_info.secFlg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 down(&pSesInfo->sesSem);
Steve French189acaa2006-06-23 02:33:48 +00002111 /* BB FIXME need to pass vol->secFlgs BB */
Steve French50c2f752007-07-13 00:33:32 +00002112 rc = cifs_setup_session(xid, pSesInfo,
2113 cifs_sb->local_nls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 up(&pSesInfo->sesSem);
Steve French4523cc32007-04-30 20:13:06 +00002115 if (!rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 atomic_inc(&srvTcp->socketUseCount);
Jeff Layton70fe7dc2007-11-16 22:21:07 +00002117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
Steve French50c2f752007-07-13 00:33:32 +00002119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 /* search for existing tcon to this server share */
2121 if (!rc) {
Steve French4523cc32007-04-30 20:13:06 +00002122 if (volume_info.rsize > CIFSMaxBufSize) {
Steve French50c2f752007-07-13 00:33:32 +00002123 cERROR(1, ("rsize %d too large, using MaxBufSize",
Steve French0ae0efa2005-10-10 10:57:19 -07002124 volume_info.rsize));
2125 cifs_sb->rsize = CIFSMaxBufSize;
Steve French75865f8c2007-06-24 18:30:48 +00002126 } else if ((volume_info.rsize) &&
2127 (volume_info.rsize <= CIFSMaxBufSize))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 cifs_sb->rsize = volume_info.rsize;
Steve French0ae0efa2005-10-10 10:57:19 -07002129 else /* default */
2130 cifs_sb->rsize = CIFSMaxBufSize;
2131
Steve French4523cc32007-04-30 20:13:06 +00002132 if (volume_info.wsize > PAGEVEC_SIZE * PAGE_CACHE_SIZE) {
Steve French50c2f752007-07-13 00:33:32 +00002133 cERROR(1, ("wsize %d too large, using 4096 instead",
Steve French0ae0efa2005-10-10 10:57:19 -07002134 volume_info.wsize));
2135 cifs_sb->wsize = 4096;
Steve French4523cc32007-04-30 20:13:06 +00002136 } else if (volume_info.wsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 cifs_sb->wsize = volume_info.wsize;
2138 else
Steve French50c2f752007-07-13 00:33:32 +00002139 cifs_sb->wsize =
Steve French1877c9e2006-01-27 18:36:11 -08002140 min_t(const int, PAGEVEC_SIZE * PAGE_CACHE_SIZE,
2141 127*1024);
Steve French17cbbaf2006-01-24 20:26:48 -08002142 /* old default of CIFSMaxBufSize was too small now
Steve French50c2f752007-07-13 00:33:32 +00002143 that SMB Write2 can send multiple pages in kvec.
Steve French17cbbaf2006-01-24 20:26:48 -08002144 RFC1001 does not describe what happens when frame
2145 bigger than 128K is sent so use that as max in
2146 conjunction with 52K kvec constraint on arch with 4K
2147 page size */
2148
Steve French4523cc32007-04-30 20:13:06 +00002149 if (cifs_sb->rsize < 2048) {
Steve French50c2f752007-07-13 00:33:32 +00002150 cifs_sb->rsize = 2048;
Steve French6cec2ae2006-02-22 17:31:52 -06002151 /* Windows ME may prefer this */
Steve French467a8f82007-06-27 22:41:32 +00002152 cFYI(1, ("readsize set to minimum: 2048"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
Steve French2fe87f02006-09-21 07:02:52 +00002154 /* calculate prepath */
2155 cifs_sb->prepath = volume_info.prepath;
Steve French4523cc32007-04-30 20:13:06 +00002156 if (cifs_sb->prepath) {
Steve French2fe87f02006-09-21 07:02:52 +00002157 cifs_sb->prepathlen = strlen(cifs_sb->prepath);
Steve French03a143c2008-02-14 06:38:30 +00002158 /* we can not convert the / to \ in the path
2159 separators in the prefixpath yet because we do not
2160 know (until reset_cifs_unix_caps is called later)
2161 whether POSIX PATH CAP is available. We normalize
2162 the / to \ after reset_cifs_unix_caps is called */
Steve French2fe87f02006-09-21 07:02:52 +00002163 volume_info.prepath = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002164 } else
Steve French2fe87f02006-09-21 07:02:52 +00002165 cifs_sb->prepathlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 cifs_sb->mnt_uid = volume_info.linux_uid;
2167 cifs_sb->mnt_gid = volume_info.linux_gid;
2168 cifs_sb->mnt_file_mode = volume_info.file_mode;
2169 cifs_sb->mnt_dir_mode = volume_info.dir_mode;
Steve French467a8f82007-06-27 22:41:32 +00002170 cFYI(1, ("file mode: 0x%x dir mode: 0x%x",
2171 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Steve French4523cc32007-04-30 20:13:06 +00002173 if (volume_info.noperm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
Steve French4523cc32007-04-30 20:13:06 +00002175 if (volume_info.setuids)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French4523cc32007-04-30 20:13:06 +00002177 if (volume_info.server_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
Steve French4523cc32007-04-30 20:13:06 +00002179 if (volume_info.remap)
Steve French6a0b4822005-04-28 22:41:05 -07002180 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
Steve French4523cc32007-04-30 20:13:06 +00002181 if (volume_info.no_xattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
Steve French4523cc32007-04-30 20:13:06 +00002183 if (volume_info.sfu_emul)
Steve Frenchd7245c22005-07-14 18:25:12 -05002184 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
Steve French4523cc32007-04-30 20:13:06 +00002185 if (volume_info.nobrl)
Steve Frenchc46fa8a2005-08-18 20:49:57 -07002186 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French4523cc32007-04-30 20:13:06 +00002187 if (volume_info.cifs_acl)
Steve French0a4b92c2006-01-12 15:44:21 -08002188 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Steve French4523cc32007-04-30 20:13:06 +00002189 if (volume_info.override_uid)
2190 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
2191 if (volume_info.override_gid)
2192 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
Jeff Laytond0a9c072008-05-12 22:23:49 +00002193 if (volume_info.dynperm)
2194 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Steve French4523cc32007-04-30 20:13:06 +00002195 if (volume_info.direct_io) {
Steve French467a8f82007-06-27 22:41:32 +00002196 cFYI(1, ("mounting share using direct i/o"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
2198 }
2199
Steve French27adb442008-05-23 19:43:29 +00002200 if ((volume_info.cifs_acl) && (volume_info.dynperm))
2201 cERROR(1, ("mount option dynperm ignored if cifsacl "
2202 "mount option supported"));
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 tcon =
2205 find_unc(sin_server.sin_addr.s_addr, volume_info.UNC,
2206 volume_info.username);
2207 if (tcon) {
Steve Frenchbf820672005-12-01 22:32:42 -08002208 cFYI(1, ("Found match on UNC path"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 /* we can have only one retry value for a connection
2210 to a share so for resources mounted more than once
Steve French50c2f752007-07-13 00:33:32 +00002211 to the same server share the last value passed in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 for the retry flag is used */
2213 tcon->retry = volume_info.retry;
Steve Frenchd3485d32005-08-19 11:04:29 -07002214 tcon->nocase = volume_info.nocase;
Steve French84210e92008-10-23 04:42:37 +00002215 tcon->local_lease = volume_info.local_lease;
Steve French95b1cb92008-05-15 16:44:38 +00002216 if (tcon->seal != volume_info.seal)
2217 cERROR(1, ("transport encryption setting "
2218 "conflicts with existing tid"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 } else {
2220 tcon = tconInfoAlloc();
2221 if (tcon == NULL)
2222 rc = -ENOMEM;
2223 else {
Steve French50c2f752007-07-13 00:33:32 +00002224 /* check for null share name ie connecting to
Steve French8af18972007-02-14 04:42:51 +00002225 * dfs root */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Steve French50c2f752007-07-13 00:33:32 +00002227 /* BB check if this works for exactly length
Steve French8af18972007-02-14 04:42:51 +00002228 * three strings */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if ((strchr(volume_info.UNC + 3, '\\') == NULL)
2230 && (strchr(volume_info.UNC + 3, '/') ==
2231 NULL)) {
Steve French646dd532008-05-15 01:50:56 +00002232/* rc = connect_to_dfs_path(xid, pSesInfo,
Steve French8af18972007-02-14 04:42:51 +00002233 "", cifs_sb->local_nls,
Steve French50c2f752007-07-13 00:33:32 +00002234 cifs_sb->mnt_cifs_flags &
Steve French646dd532008-05-15 01:50:56 +00002235 CIFS_MOUNT_MAP_SPECIAL_CHR);*/
2236 cFYI(1, ("DFS root not supported"));
Jeff Layton70fe7dc2007-11-16 22:21:07 +00002237 rc = -ENODEV;
2238 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 } else {
Steve French8af18972007-02-14 04:42:51 +00002240 /* BB Do we need to wrap sesSem around
2241 * this TCon call and Unix SetFS as
2242 * we do on SessSetup and reconnect? */
Steve French50c2f752007-07-13 00:33:32 +00002243 rc = CIFSTCon(xid, pSesInfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 volume_info.UNC,
2245 tcon, cifs_sb->local_nls);
2246 cFYI(1, ("CIFS Tcon rc = %d", rc));
Steve French2c1b8612008-10-16 18:35:21 +00002247 if (volume_info.nodfs) {
2248 tcon->Flags &=
2249 ~SMB_SHARE_IS_IN_DFS;
2250 cFYI(1, ("DFS disabled (%d)",
2251 tcon->Flags));
2252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
2254 if (!rc) {
2255 atomic_inc(&pSesInfo->inUse);
2256 tcon->retry = volume_info.retry;
Steve Frenchd3485d32005-08-19 11:04:29 -07002257 tcon->nocase = volume_info.nocase;
Steve French95b1cb92008-05-15 16:44:38 +00002258 tcon->seal = volume_info.seal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 }
2260 }
2261 }
2262 }
Steve French4523cc32007-04-30 20:13:06 +00002263 if (pSesInfo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (pSesInfo->capabilities & CAP_LARGE_FILES) {
2265 sb->s_maxbytes = (u64) 1 << 63;
2266 } else
2267 sb->s_maxbytes = (u64) 1 << 31; /* 2 GB */
2268 }
2269
Steve French8af18972007-02-14 04:42:51 +00002270 /* BB FIXME fix time_gran to be larger for LANMAN sessions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 sb->s_time_gran = 100;
2272
2273/* on error free sesinfo and tcon struct if needed */
2274 if (rc) {
2275 /* if session setup failed, use count is zero but
2276 we still need to free cifsd thread */
Steve French4523cc32007-04-30 20:13:06 +00002277 if (atomic_read(&srvTcp->socketUseCount) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 spin_lock(&GlobalMid_Lock);
2279 srvTcp->tcpStatus = CifsExiting;
2280 spin_unlock(&GlobalMid_Lock);
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04002281 kill_cifsd(srvTcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
2283 /* If find_unc succeeded then rc == 0 so we can not end */
2284 if (tcon) /* up accidently freeing someone elses tcon struct */
2285 tconInfoFree(tcon);
2286 if (existingCifsSes == NULL) {
2287 if (pSesInfo) {
Steve French50c2f752007-07-13 00:33:32 +00002288 if ((pSesInfo->server) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 (pSesInfo->status == CifsGood)) {
2290 int temp_rc;
2291 temp_rc = CIFSSMBLogoff(xid, pSesInfo);
2292 /* if the socketUseCount is now zero */
Steve French4523cc32007-04-30 20:13:06 +00002293 if ((temp_rc == -ESHUTDOWN) &&
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04002294 (pSesInfo->server))
2295 kill_cifsd(pSesInfo->server);
Steve Frencha0136892007-10-04 20:05:09 +00002296 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 cFYI(1, ("No session or bad tcon"));
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04002298 if (pSesInfo->server) {
Jeff Layton469ee612008-10-16 18:46:39 +00002299 spin_lock(&GlobalMid_Lock);
2300 srvTcp->tcpStatus = CifsExiting;
2301 spin_unlock(&GlobalMid_Lock);
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04002302 kill_cifsd(pSesInfo->server);
Steve Frencha0136892007-10-04 20:05:09 +00002303 }
2304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 sesInfoFree(pSesInfo);
2306 /* pSesInfo = NULL; */
2307 }
2308 }
2309 } else {
2310 atomic_inc(&tcon->useCount);
2311 cifs_sb->tcon = tcon;
2312 tcon->ses = pSesInfo;
2313
Steve French82940a42006-03-02 03:24:57 +00002314 /* do not care if following two calls succeed - informational */
Steve French7f8ed422007-09-28 22:28:55 +00002315 if (!tcon->ipc) {
2316 CIFSSMBQFSDeviceInfo(xid, tcon);
2317 CIFSSMBQFSAttributeInfo(xid, tcon);
2318 }
Steve French50c2f752007-07-13 00:33:32 +00002319
Steve French8af18972007-02-14 04:42:51 +00002320 /* tell server which Unix caps we support */
2321 if (tcon->ses->capabilities & CAP_UNIX)
Steve Frenchc18c8422007-07-18 23:21:09 +00002322 /* reset of caps checks mount to see if unix extensions
2323 disabled for just this mount */
Steve French8af18972007-02-14 04:42:51 +00002324 reset_cifs_unix_caps(xid, tcon, sb, &volume_info);
Steve Frenchc18c8422007-07-18 23:21:09 +00002325 else
2326 tcon->unix_ext = 0; /* server does not support them */
2327
Steve French03a143c2008-02-14 06:38:30 +00002328 /* convert forward to back slashes in prepath here if needed */
Igor Mammedov11b6d642008-02-15 19:06:04 +00002329 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) == 0)
2330 convert_delimiter(cifs_sb->prepath,
2331 CIFS_DIR_SEP(cifs_sb));
Steve French03a143c2008-02-14 06:38:30 +00002332
Steve Frenchc18c8422007-07-18 23:21:09 +00002333 if ((tcon->unix_ext == 0) && (cifs_sb->rsize > (1024 * 127))) {
Steve French75865f8c2007-06-24 18:30:48 +00002334 cifs_sb->rsize = 1024 * 127;
Steve French90c81e02008-02-12 20:32:36 +00002335 cFYI(DBG2,
2336 ("no very large read support, rsize now 127K"));
Steve French75865f8c2007-06-24 18:30:48 +00002337 }
Steve French3e844692005-10-03 13:37:24 -07002338 if (!(tcon->ses->capabilities & CAP_LARGE_WRITE_X))
2339 cifs_sb->wsize = min(cifs_sb->wsize,
2340 (tcon->ses->server->maxBuf -
2341 MAX_CIFS_HDR_SIZE));
Steve French0ae0efa2005-10-10 10:57:19 -07002342 if (!(tcon->ses->capabilities & CAP_LARGE_READ_X))
Steve French50c2f752007-07-13 00:33:32 +00002343 cifs_sb->rsize = min(cifs_sb->rsize,
2344 (tcon->ses->server->maxBuf -
2345 MAX_CIFS_HDR_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
2347
2348 /* volume_info.password is freed above when existing session found
2349 (in which case it is not needed anymore) but when new sesion is created
2350 the password ptr is put in the new session structure (in which case the
2351 password will be freed at unmount time) */
Jeff Layton70fe7dc2007-11-16 22:21:07 +00002352out:
2353 /* zero out password before freeing */
2354 if (volume_info.password != NULL) {
2355 memset(volume_info.password, 0, strlen(volume_info.password));
2356 kfree(volume_info.password);
2357 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08002358 kfree(volume_info.UNC);
Steve French2fe87f02006-09-21 07:02:52 +00002359 kfree(volume_info.prepath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 FreeXid(xid);
2361 return rc;
2362}
2363
2364static int
2365CIFSSessSetup(unsigned int xid, struct cifsSesInfo *ses,
Steve French7c7b25b2006-06-01 19:20:10 +00002366 char session_key[CIFS_SESS_KEY_SIZE],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 const struct nls_table *nls_codepage)
2368{
2369 struct smb_hdr *smb_buffer;
2370 struct smb_hdr *smb_buffer_response;
2371 SESSION_SETUP_ANDX *pSMB;
2372 SESSION_SETUP_ANDX *pSMBr;
2373 char *bcc_ptr;
2374 char *user;
2375 char *domain;
2376 int rc = 0;
2377 int remaining_words = 0;
2378 int bytes_returned = 0;
2379 int len;
2380 __u32 capabilities;
2381 __u16 count;
2382
Steve Frencheeac8042006-01-13 21:34:58 -08002383 cFYI(1, ("In sesssetup"));
Steve French4523cc32007-04-30 20:13:06 +00002384 if (ses == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 return -EINVAL;
2386 user = ses->userName;
2387 domain = ses->domainName;
2388 smb_buffer = cifs_buf_get();
Steve French582d21e2008-05-13 04:54:12 +00002389
2390 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return -ENOMEM;
Steve French582d21e2008-05-13 04:54:12 +00002392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 smb_buffer_response = smb_buffer;
2394 pSMBr = pSMB = (SESSION_SETUP_ANDX *) smb_buffer;
2395
2396 /* send SMBsessionSetup here */
2397 header_assemble(smb_buffer, SMB_COM_SESSION_SETUP_ANDX,
2398 NULL /* no tCon exists yet */ , 13 /* wct */ );
2399
Steve French1982c342005-08-17 12:38:22 -07002400 smb_buffer->Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 pSMB->req_no_secext.AndXCommand = 0xFF;
2402 pSMB->req_no_secext.MaxBufferSize = cpu_to_le16(ses->server->maxBuf);
2403 pSMB->req_no_secext.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
2404
Steve French50c2f752007-07-13 00:33:32 +00002405 if (ses->server->secMode &
2406 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
2408
2409 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
2410 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
2411 if (ses->capabilities & CAP_UNICODE) {
2412 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
2413 capabilities |= CAP_UNICODE;
2414 }
2415 if (ses->capabilities & CAP_STATUS32) {
2416 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
2417 capabilities |= CAP_STATUS32;
2418 }
2419 if (ses->capabilities & CAP_DFS) {
2420 smb_buffer->Flags2 |= SMBFLG2_DFS;
2421 capabilities |= CAP_DFS;
2422 }
2423 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
2424
Steve French50c2f752007-07-13 00:33:32 +00002425 pSMB->req_no_secext.CaseInsensitivePasswordLength =
Steve French7c7b25b2006-06-01 19:20:10 +00002426 cpu_to_le16(CIFS_SESS_KEY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 pSMB->req_no_secext.CaseSensitivePasswordLength =
Steve French7c7b25b2006-06-01 19:20:10 +00002429 cpu_to_le16(CIFS_SESS_KEY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 bcc_ptr = pByteArea(smb_buffer);
Steve French7c7b25b2006-06-01 19:20:10 +00002431 memcpy(bcc_ptr, (char *) session_key, CIFS_SESS_KEY_SIZE);
2432 bcc_ptr += CIFS_SESS_KEY_SIZE;
2433 memcpy(bcc_ptr, (char *) session_key, CIFS_SESS_KEY_SIZE);
2434 bcc_ptr += CIFS_SESS_KEY_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 if (ses->capabilities & CAP_UNICODE) {
2437 if ((long) bcc_ptr % 2) { /* must be word aligned for Unicode */
2438 *bcc_ptr = 0;
2439 bcc_ptr++;
2440 }
Steve French4523cc32007-04-30 20:13:06 +00002441 if (user == NULL)
Steve French39798772006-05-31 22:40:51 +00002442 bytes_returned = 0; /* skip null user */
Steve French50c2f752007-07-13 00:33:32 +00002443 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 bytes_returned =
Steve French50c2f752007-07-13 00:33:32 +00002445 cifs_strtoUCS((__le16 *) bcc_ptr, user, 100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 nls_codepage);
2447 /* convert number of 16 bit words to bytes */
2448 bcc_ptr += 2 * bytes_returned;
2449 bcc_ptr += 2; /* trailing null */
2450 if (domain == NULL)
2451 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08002452 cifs_strtoUCS((__le16 *) bcc_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 "CIFS_LINUX_DOM", 32, nls_codepage);
2454 else
2455 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08002456 cifs_strtoUCS((__le16 *) bcc_ptr, domain, 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 nls_codepage);
2458 bcc_ptr += 2 * bytes_returned;
2459 bcc_ptr += 2;
2460 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08002461 cifs_strtoUCS((__le16 *) bcc_ptr, "Linux version ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 32, nls_codepage);
2463 bcc_ptr += 2 * bytes_returned;
2464 bytes_returned =
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07002465 cifs_strtoUCS((__le16 *) bcc_ptr, utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 32, nls_codepage);
2467 bcc_ptr += 2 * bytes_returned;
2468 bcc_ptr += 2;
2469 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08002470 cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 64, nls_codepage);
2472 bcc_ptr += 2 * bytes_returned;
2473 bcc_ptr += 2;
2474 } else {
Steve French50c2f752007-07-13 00:33:32 +00002475 if (user != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 strncpy(bcc_ptr, user, 200);
2477 bcc_ptr += strnlen(user, 200);
2478 }
2479 *bcc_ptr = 0;
2480 bcc_ptr++;
2481 if (domain == NULL) {
2482 strcpy(bcc_ptr, "CIFS_LINUX_DOM");
2483 bcc_ptr += strlen("CIFS_LINUX_DOM") + 1;
2484 } else {
2485 strncpy(bcc_ptr, domain, 64);
2486 bcc_ptr += strnlen(domain, 64);
2487 *bcc_ptr = 0;
2488 bcc_ptr++;
2489 }
2490 strcpy(bcc_ptr, "Linux version ");
2491 bcc_ptr += strlen("Linux version ");
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07002492 strcpy(bcc_ptr, utsname()->release);
2493 bcc_ptr += strlen(utsname()->release) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
2495 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
2496 }
2497 count = (long) bcc_ptr - (long) pByteArea(smb_buffer);
2498 smb_buffer->smb_buf_length += count;
2499 pSMB->req_no_secext.ByteCount = cpu_to_le16(count);
2500
2501 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response,
Steve French133672e2007-11-13 22:41:37 +00002502 &bytes_returned, CIFS_LONG_OP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (rc) {
2504/* rc = map_smb_to_linux_error(smb_buffer_response); now done in SendReceive */
2505 } else if ((smb_buffer_response->WordCount == 3)
2506 || (smb_buffer_response->WordCount == 4)) {
2507 __u16 action = le16_to_cpu(pSMBr->resp.Action);
2508 __u16 blob_len = le16_to_cpu(pSMBr->resp.SecurityBlobLength);
2509 if (action & GUEST_LOGIN)
Steve French50c2f752007-07-13 00:33:32 +00002510 cFYI(1, (" Guest login")); /* BB mark SesInfo struct? */
2511 ses->Suid = smb_buffer_response->Uid; /* UID left in wire format
2512 (little endian) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 cFYI(1, ("UID = %d ", ses->Suid));
Steve French50c2f752007-07-13 00:33:32 +00002514 /* response can have either 3 or 4 word count - Samba sends 3 */
2515 bcc_ptr = pByteArea(smb_buffer_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if ((pSMBr->resp.hdr.WordCount == 3)
2517 || ((pSMBr->resp.hdr.WordCount == 4)
2518 && (blob_len < pSMBr->resp.ByteCount))) {
2519 if (pSMBr->resp.hdr.WordCount == 4)
2520 bcc_ptr += blob_len;
2521
2522 if (smb_buffer->Flags2 & SMBFLG2_UNICODE) {
2523 if ((long) (bcc_ptr) % 2) {
2524 remaining_words =
Steve French50c2f752007-07-13 00:33:32 +00002525 (BCC(smb_buffer_response) - 1) / 2;
2526 /* Unicode strings must be word
2527 aligned */
2528 bcc_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 } else {
2530 remaining_words =
2531 BCC(smb_buffer_response) / 2;
2532 }
2533 len =
2534 UniStrnlen((wchar_t *) bcc_ptr,
2535 remaining_words - 1);
2536/* We look for obvious messed up bcc or strings in response so we do not go off
2537 the end since (at least) WIN2K and Windows XP have a major bug in not null
2538 terminating last Unicode string in response */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002539 if (ses->serverOS)
Steve Frencha424f8b2006-05-30 18:06:04 +00002540 kfree(ses->serverOS);
Steve French50c2f752007-07-13 00:33:32 +00002541 ses->serverOS = kzalloc(2 * (len + 1),
2542 GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002543 if (ses->serverOS == NULL)
Steve French433dc242005-04-28 22:41:08 -07002544 goto sesssetup_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 cifs_strfromUCS_le(ses->serverOS,
Steve French50c2f752007-07-13 00:33:32 +00002546 (__le16 *)bcc_ptr,
2547 len, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 bcc_ptr += 2 * (len + 1);
2549 remaining_words -= len + 1;
2550 ses->serverOS[2 * len] = 0;
2551 ses->serverOS[1 + (2 * len)] = 0;
2552 if (remaining_words > 0) {
2553 len = UniStrnlen((wchar_t *)bcc_ptr,
2554 remaining_words-1);
Steve Frenchcd49b492006-06-26 04:22:36 +00002555 kfree(ses->serverNOS);
Steve French50c2f752007-07-13 00:33:32 +00002556 ses->serverNOS = kzalloc(2 * (len + 1),
2557 GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002558 if (ses->serverNOS == NULL)
Steve French433dc242005-04-28 22:41:08 -07002559 goto sesssetup_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 cifs_strfromUCS_le(ses->serverNOS,
Steve French50c2f752007-07-13 00:33:32 +00002561 (__le16 *)bcc_ptr,
2562 len, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 bcc_ptr += 2 * (len + 1);
2564 ses->serverNOS[2 * len] = 0;
2565 ses->serverNOS[1 + (2 * len)] = 0;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002566 if (strncmp(ses->serverNOS,
Steve French50c2f752007-07-13 00:33:32 +00002567 "NT LAN Manager 4", 16) == 0) {
Steve French467a8f82007-06-27 22:41:32 +00002568 cFYI(1, ("NT4 server"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 ses->flags |= CIFS_SES_NT4;
2570 }
2571 remaining_words -= len + 1;
2572 if (remaining_words > 0) {
Steve French433dc242005-04-28 22:41:08 -07002573 len = UniStrnlen((wchar_t *) bcc_ptr, remaining_words);
Steve French50c2f752007-07-13 00:33:32 +00002574 /* last string is not always null terminated
2575 (for e.g. for Windows XP & 2000) */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002576 if (ses->serverDomain)
Steve Frencha424f8b2006-05-30 18:06:04 +00002577 kfree(ses->serverDomain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 ses->serverDomain =
Steve French50c2f752007-07-13 00:33:32 +00002579 kzalloc(2*(len+1),
2580 GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002581 if (ses->serverDomain == NULL)
Steve French433dc242005-04-28 22:41:08 -07002582 goto sesssetup_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 cifs_strfromUCS_le(ses->serverDomain,
Steve French50c2f752007-07-13 00:33:32 +00002584 (__le16 *)bcc_ptr,
2585 len, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 bcc_ptr += 2 * (len + 1);
2587 ses->serverDomain[2*len] = 0;
2588 ses->serverDomain[1+(2*len)] = 0;
Steve French50c2f752007-07-13 00:33:32 +00002589 } else { /* else no more room so create
2590 dummy domain string */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002591 if (ses->serverDomain)
Steve Frencha424f8b2006-05-30 18:06:04 +00002592 kfree(ses->serverDomain);
Steve French50c2f752007-07-13 00:33:32 +00002593 ses->serverDomain =
Pekka Enberge915fc42005-09-06 15:18:35 -07002594 kzalloc(2, GFP_KERNEL);
Steve Frencha424f8b2006-05-30 18:06:04 +00002595 }
Steve French50c2f752007-07-13 00:33:32 +00002596 } else { /* no room so create dummy domain
2597 and NOS string */
2598
Steve French433dc242005-04-28 22:41:08 -07002599 /* if these kcallocs fail not much we
2600 can do, but better to not fail the
2601 sesssetup itself */
Steve Frenchcd49b492006-06-26 04:22:36 +00002602 kfree(ses->serverDomain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 ses->serverDomain =
Pekka Enberge915fc42005-09-06 15:18:35 -07002604 kzalloc(2, GFP_KERNEL);
Steve Frenchcd49b492006-06-26 04:22:36 +00002605 kfree(ses->serverNOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 ses->serverNOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07002607 kzalloc(2, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
2609 } else { /* ASCII */
2610 len = strnlen(bcc_ptr, 1024);
2611 if (((long) bcc_ptr + len) - (long)
2612 pByteArea(smb_buffer_response)
2613 <= BCC(smb_buffer_response)) {
Steve Frenchcd49b492006-06-26 04:22:36 +00002614 kfree(ses->serverOS);
Steve French50c2f752007-07-13 00:33:32 +00002615 ses->serverOS = kzalloc(len + 1,
2616 GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002617 if (ses->serverOS == NULL)
Steve French433dc242005-04-28 22:41:08 -07002618 goto sesssetup_nomem;
Steve French50c2f752007-07-13 00:33:32 +00002619 strncpy(ses->serverOS, bcc_ptr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
2621 bcc_ptr += len;
Steve French50c2f752007-07-13 00:33:32 +00002622 /* null terminate the string */
2623 bcc_ptr[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 bcc_ptr++;
2625
2626 len = strnlen(bcc_ptr, 1024);
Steve Frenchcd49b492006-06-26 04:22:36 +00002627 kfree(ses->serverNOS);
Steve French50c2f752007-07-13 00:33:32 +00002628 ses->serverNOS = kzalloc(len + 1,
2629 GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002630 if (ses->serverNOS == NULL)
Steve French433dc242005-04-28 22:41:08 -07002631 goto sesssetup_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 strncpy(ses->serverNOS, bcc_ptr, len);
2633 bcc_ptr += len;
2634 bcc_ptr[0] = 0;
2635 bcc_ptr++;
2636
2637 len = strnlen(bcc_ptr, 1024);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002638 if (ses->serverDomain)
Steve Frencha424f8b2006-05-30 18:06:04 +00002639 kfree(ses->serverDomain);
Steve French50c2f752007-07-13 00:33:32 +00002640 ses->serverDomain = kzalloc(len + 1,
2641 GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002642 if (ses->serverDomain == NULL)
Steve French433dc242005-04-28 22:41:08 -07002643 goto sesssetup_nomem;
Steve French50c2f752007-07-13 00:33:32 +00002644 strncpy(ses->serverDomain, bcc_ptr,
2645 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 bcc_ptr += len;
2647 bcc_ptr[0] = 0;
2648 bcc_ptr++;
2649 } else
2650 cFYI(1,
Steve French50c2f752007-07-13 00:33:32 +00002651 ("Variable field of length %d "
2652 "extends beyond end of smb ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 len));
2654 }
2655 } else {
2656 cERROR(1,
Steve French50c2f752007-07-13 00:33:32 +00002657 (" Security Blob Length extends beyond "
2658 "end of SMB"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
2660 } else {
2661 cERROR(1,
2662 (" Invalid Word count %d: ",
2663 smb_buffer_response->WordCount));
2664 rc = -EIO;
2665 }
Steve French433dc242005-04-28 22:41:08 -07002666sesssetup_nomem: /* do not return an error on nomem for the info strings,
2667 since that could make reconnection harder, and
2668 reconnection might be needed to free memory */
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00002669 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 return rc;
2672}
2673
2674static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675CIFSNTLMSSPNegotiateSessSetup(unsigned int xid,
Steve French4b18f2a2008-04-29 00:06:05 +00002676 struct cifsSesInfo *ses, bool *pNTLMv2_flag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 const struct nls_table *nls_codepage)
2678{
2679 struct smb_hdr *smb_buffer;
2680 struct smb_hdr *smb_buffer_response;
2681 SESSION_SETUP_ANDX *pSMB;
2682 SESSION_SETUP_ANDX *pSMBr;
2683 char *bcc_ptr;
2684 char *domain;
2685 int rc = 0;
2686 int remaining_words = 0;
2687 int bytes_returned = 0;
2688 int len;
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00002689 int SecurityBlobLength = sizeof(NEGOTIATE_MESSAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 PNEGOTIATE_MESSAGE SecurityBlob;
2691 PCHALLENGE_MESSAGE SecurityBlob2;
2692 __u32 negotiate_flags, capabilities;
2693 __u16 count;
2694
Steve French12b3b8f2006-02-09 21:12:47 +00002695 cFYI(1, ("In NTLMSSP sesssetup (negotiate)"));
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002696 if (ses == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 return -EINVAL;
2698 domain = ses->domainName;
Steve French4b18f2a2008-04-29 00:06:05 +00002699 *pNTLMv2_flag = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 smb_buffer = cifs_buf_get();
2701 if (smb_buffer == NULL) {
2702 return -ENOMEM;
2703 }
2704 smb_buffer_response = smb_buffer;
2705 pSMB = (SESSION_SETUP_ANDX *) smb_buffer;
2706 pSMBr = (SESSION_SETUP_ANDX *) smb_buffer_response;
2707
2708 /* send SMBsessionSetup here */
2709 header_assemble(smb_buffer, SMB_COM_SESSION_SETUP_ANDX,
2710 NULL /* no tCon exists yet */ , 12 /* wct */ );
Steve French1982c342005-08-17 12:38:22 -07002711
2712 smb_buffer->Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
2714 pSMB->req.hdr.Flags |= (SMBFLG_CASELESS | SMBFLG_CANONICAL_PATH_FORMAT);
2715
2716 pSMB->req.AndXCommand = 0xFF;
2717 pSMB->req.MaxBufferSize = cpu_to_le16(ses->server->maxBuf);
2718 pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
2719
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002720 if (ses->server->secMode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
2722
2723 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
2724 CAP_EXTENDED_SECURITY;
2725 if (ses->capabilities & CAP_UNICODE) {
2726 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
2727 capabilities |= CAP_UNICODE;
2728 }
2729 if (ses->capabilities & CAP_STATUS32) {
2730 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
2731 capabilities |= CAP_STATUS32;
2732 }
2733 if (ses->capabilities & CAP_DFS) {
2734 smb_buffer->Flags2 |= SMBFLG2_DFS;
2735 capabilities |= CAP_DFS;
2736 }
2737 pSMB->req.Capabilities = cpu_to_le32(capabilities);
2738
2739 bcc_ptr = (char *) &pSMB->req.SecurityBlob;
2740 SecurityBlob = (PNEGOTIATE_MESSAGE) bcc_ptr;
2741 strncpy(SecurityBlob->Signature, NTLMSSP_SIGNATURE, 8);
2742 SecurityBlob->MessageType = NtLmNegotiate;
2743 negotiate_flags =
2744 NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_OEM |
Steve French12b3b8f2006-02-09 21:12:47 +00002745 NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_NTLM |
2746 NTLMSSP_NEGOTIATE_56 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 /* NTLMSSP_NEGOTIATE_ALWAYS_SIGN | */ NTLMSSP_NEGOTIATE_128;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002748 if (sign_CIFS_PDUs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 negotiate_flags |= NTLMSSP_NEGOTIATE_SIGN;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002750/* if (ntlmv2_support)
Steve French39798772006-05-31 22:40:51 +00002751 negotiate_flags |= NTLMSSP_NEGOTIATE_NTLMV2;*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 /* setup pointers to domain name and workstation name */
2753 bcc_ptr += SecurityBlobLength;
2754
2755 SecurityBlob->WorkstationName.Buffer = 0;
2756 SecurityBlob->WorkstationName.Length = 0;
2757 SecurityBlob->WorkstationName.MaximumLength = 0;
2758
Steve French12b3b8f2006-02-09 21:12:47 +00002759 /* Domain not sent on first Sesssetup in NTLMSSP, instead it is sent
2760 along with username on auth request (ie the response to challenge) */
2761 SecurityBlob->DomainName.Buffer = 0;
2762 SecurityBlob->DomainName.Length = 0;
2763 SecurityBlob->DomainName.MaximumLength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 if (ses->capabilities & CAP_UNICODE) {
2765 if ((long) bcc_ptr % 2) {
2766 *bcc_ptr = 0;
2767 bcc_ptr++;
2768 }
2769
2770 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08002771 cifs_strtoUCS((__le16 *) bcc_ptr, "Linux version ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 32, nls_codepage);
2773 bcc_ptr += 2 * bytes_returned;
2774 bytes_returned =
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07002775 cifs_strtoUCS((__le16 *) bcc_ptr, utsname()->release, 32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 nls_codepage);
2777 bcc_ptr += 2 * bytes_returned;
2778 bcc_ptr += 2; /* null terminate Linux version */
2779 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08002780 cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 64, nls_codepage);
2782 bcc_ptr += 2 * bytes_returned;
2783 *(bcc_ptr + 1) = 0;
2784 *(bcc_ptr + 2) = 0;
2785 bcc_ptr += 2; /* null terminate network opsys string */
2786 *(bcc_ptr + 1) = 0;
2787 *(bcc_ptr + 2) = 0;
2788 bcc_ptr += 2; /* null domain */
2789 } else { /* ASCII */
2790 strcpy(bcc_ptr, "Linux version ");
2791 bcc_ptr += strlen("Linux version ");
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07002792 strcpy(bcc_ptr, utsname()->release);
2793 bcc_ptr += strlen(utsname()->release) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
2795 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
2796 bcc_ptr++; /* empty domain field */
2797 *bcc_ptr = 0;
2798 }
2799 SecurityBlob->NegotiateFlags = cpu_to_le32(negotiate_flags);
2800 pSMB->req.SecurityBlobLength = cpu_to_le16(SecurityBlobLength);
2801 count = (long) bcc_ptr - (long) pByteArea(smb_buffer);
2802 smb_buffer->smb_buf_length += count;
2803 pSMB->req.ByteCount = cpu_to_le16(count);
2804
2805 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response,
Steve French133672e2007-11-13 22:41:37 +00002806 &bytes_returned, CIFS_LONG_OP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
2808 if (smb_buffer_response->Status.CifsError ==
2809 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
2810 rc = 0;
2811
2812 if (rc) {
2813/* rc = map_smb_to_linux_error(smb_buffer_response); *//* done in SendReceive now */
2814 } else if ((smb_buffer_response->WordCount == 3)
2815 || (smb_buffer_response->WordCount == 4)) {
2816 __u16 action = le16_to_cpu(pSMBr->resp.Action);
2817 __u16 blob_len = le16_to_cpu(pSMBr->resp.SecurityBlobLength);
2818
2819 if (action & GUEST_LOGIN)
Steve French50c2f752007-07-13 00:33:32 +00002820 cFYI(1, (" Guest login"));
2821 /* Do we want to set anything in SesInfo struct when guest login? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Steve French50c2f752007-07-13 00:33:32 +00002823 bcc_ptr = pByteArea(smb_buffer_response);
2824 /* response can have either 3 or 4 word count - Samba sends 3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826 SecurityBlob2 = (PCHALLENGE_MESSAGE) bcc_ptr;
2827 if (SecurityBlob2->MessageType != NtLmChallenge) {
2828 cFYI(1,
2829 ("Unexpected NTLMSSP message type received %d",
2830 SecurityBlob2->MessageType));
2831 } else if (ses) {
Steve French50c2f752007-07-13 00:33:32 +00002832 ses->Suid = smb_buffer_response->Uid; /* UID left in le format */
Steve French12b3b8f2006-02-09 21:12:47 +00002833 cFYI(1, ("UID = %d", ses->Suid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 if ((pSMBr->resp.hdr.WordCount == 3)
2835 || ((pSMBr->resp.hdr.WordCount == 4)
2836 && (blob_len <
2837 pSMBr->resp.ByteCount))) {
2838
2839 if (pSMBr->resp.hdr.WordCount == 4) {
2840 bcc_ptr += blob_len;
Steve French12b3b8f2006-02-09 21:12:47 +00002841 cFYI(1, ("Security Blob Length %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 blob_len));
2843 }
2844
Steve French12b3b8f2006-02-09 21:12:47 +00002845 cFYI(1, ("NTLMSSP Challenge rcvd"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
2847 memcpy(ses->server->cryptKey,
2848 SecurityBlob2->Challenge,
2849 CIFS_CRYPTO_KEY_SIZE);
Steve French50c2f752007-07-13 00:33:32 +00002850 if (SecurityBlob2->NegotiateFlags &
Steve French12b3b8f2006-02-09 21:12:47 +00002851 cpu_to_le32(NTLMSSP_NEGOTIATE_NTLMV2))
Steve French4b18f2a2008-04-29 00:06:05 +00002852 *pNTLMv2_flag = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
Steve French50c2f752007-07-13 00:33:32 +00002854 if ((SecurityBlob2->NegotiateFlags &
2855 cpu_to_le32(NTLMSSP_NEGOTIATE_ALWAYS_SIGN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 || (sign_CIFS_PDUs > 1))
Steve French50c2f752007-07-13 00:33:32 +00002857 ses->server->secMode |=
2858 SECMODE_SIGN_REQUIRED;
2859 if ((SecurityBlob2->NegotiateFlags &
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 cpu_to_le32(NTLMSSP_NEGOTIATE_SIGN)) && (sign_CIFS_PDUs))
Steve French50c2f752007-07-13 00:33:32 +00002861 ses->server->secMode |=
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 SECMODE_SIGN_ENABLED;
2863
2864 if (smb_buffer->Flags2 & SMBFLG2_UNICODE) {
2865 if ((long) (bcc_ptr) % 2) {
2866 remaining_words =
2867 (BCC(smb_buffer_response)
2868 - 1) / 2;
Steve French50c2f752007-07-13 00:33:32 +00002869 /* Must word align unicode strings */
2870 bcc_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 } else {
2872 remaining_words =
2873 BCC
2874 (smb_buffer_response) / 2;
2875 }
2876 len =
2877 UniStrnlen((wchar_t *) bcc_ptr,
2878 remaining_words - 1);
2879/* We look for obvious messed up bcc or strings in response so we do not go off
2880 the end since (at least) WIN2K and Windows XP have a major bug in not null
2881 terminating last Unicode string in response */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002882 if (ses->serverOS)
Steve Frencha424f8b2006-05-30 18:06:04 +00002883 kfree(ses->serverOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 ses->serverOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07002885 kzalloc(2 * (len + 1), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 cifs_strfromUCS_le(ses->serverOS,
Steve Frenche89dc922005-11-11 15:18:19 -08002887 (__le16 *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 bcc_ptr, len,
2889 nls_codepage);
2890 bcc_ptr += 2 * (len + 1);
2891 remaining_words -= len + 1;
2892 ses->serverOS[2 * len] = 0;
2893 ses->serverOS[1 + (2 * len)] = 0;
2894 if (remaining_words > 0) {
2895 len = UniStrnlen((wchar_t *)
2896 bcc_ptr,
2897 remaining_words
2898 - 1);
Steve Frenchcd49b492006-06-26 04:22:36 +00002899 kfree(ses->serverNOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 ses->serverNOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07002901 kzalloc(2 * (len + 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 GFP_KERNEL);
2903 cifs_strfromUCS_le(ses->
2904 serverNOS,
Steve Frenche89dc922005-11-11 15:18:19 -08002905 (__le16 *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 bcc_ptr,
2907 len,
2908 nls_codepage);
2909 bcc_ptr += 2 * (len + 1);
2910 ses->serverNOS[2 * len] = 0;
2911 ses->serverNOS[1 +
2912 (2 * len)] = 0;
2913 remaining_words -= len + 1;
2914 if (remaining_words > 0) {
Steve French50c2f752007-07-13 00:33:32 +00002915 len = UniStrnlen((wchar_t *) bcc_ptr, remaining_words);
2916 /* last string not always null terminated
2917 (for e.g. for Windows XP & 2000) */
Steve Frenchcd49b492006-06-26 04:22:36 +00002918 kfree(ses->serverDomain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 ses->serverDomain =
Pekka Enberge915fc42005-09-06 15:18:35 -07002920 kzalloc(2 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 (len +
2922 1),
2923 GFP_KERNEL);
2924 cifs_strfromUCS_le
Steve Frenche89dc922005-11-11 15:18:19 -08002925 (ses->serverDomain,
2926 (__le16 *)bcc_ptr,
2927 len, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 bcc_ptr +=
2929 2 * (len + 1);
Steve Frenche89dc922005-11-11 15:18:19 -08002930 ses->serverDomain[2*len]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 = 0;
Steve Frenche89dc922005-11-11 15:18:19 -08002932 ses->serverDomain
2933 [1 + (2 * len)]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 = 0;
2935 } /* else no more room so create dummy domain string */
Steve Frencha424f8b2006-05-30 18:06:04 +00002936 else {
Steve Frenchcd49b492006-06-26 04:22:36 +00002937 kfree(ses->serverDomain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 ses->serverDomain =
Pekka Enberge915fc42005-09-06 15:18:35 -07002939 kzalloc(2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 GFP_KERNEL);
Steve Frencha424f8b2006-05-30 18:06:04 +00002941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 } else { /* no room so create dummy domain and NOS string */
Steve Frenchcd49b492006-06-26 04:22:36 +00002943 kfree(ses->serverDomain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 ses->serverDomain =
Pekka Enberge915fc42005-09-06 15:18:35 -07002945 kzalloc(2, GFP_KERNEL);
Steve Frenchcd49b492006-06-26 04:22:36 +00002946 kfree(ses->serverNOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 ses->serverNOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07002948 kzalloc(2, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
2950 } else { /* ASCII */
2951 len = strnlen(bcc_ptr, 1024);
2952 if (((long) bcc_ptr + len) - (long)
2953 pByteArea(smb_buffer_response)
2954 <= BCC(smb_buffer_response)) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002955 if (ses->serverOS)
Steve Frencha424f8b2006-05-30 18:06:04 +00002956 kfree(ses->serverOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 ses->serverOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07002958 kzalloc(len + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 GFP_KERNEL);
2960 strncpy(ses->serverOS,
2961 bcc_ptr, len);
2962
2963 bcc_ptr += len;
2964 bcc_ptr[0] = 0; /* null terminate string */
2965 bcc_ptr++;
2966
2967 len = strnlen(bcc_ptr, 1024);
Steve Frenchcd49b492006-06-26 04:22:36 +00002968 kfree(ses->serverNOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 ses->serverNOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07002970 kzalloc(len + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 GFP_KERNEL);
2972 strncpy(ses->serverNOS, bcc_ptr, len);
2973 bcc_ptr += len;
2974 bcc_ptr[0] = 0;
2975 bcc_ptr++;
2976
2977 len = strnlen(bcc_ptr, 1024);
Steve Frenchcd49b492006-06-26 04:22:36 +00002978 kfree(ses->serverDomain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 ses->serverDomain =
Pekka Enberge915fc42005-09-06 15:18:35 -07002980 kzalloc(len + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 GFP_KERNEL);
Steve French50c2f752007-07-13 00:33:32 +00002982 strncpy(ses->serverDomain,
2983 bcc_ptr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 bcc_ptr += len;
2985 bcc_ptr[0] = 0;
2986 bcc_ptr++;
2987 } else
2988 cFYI(1,
Steve French63135e02007-07-17 17:34:02 +00002989 ("field of length %d "
2990 "extends beyond end of smb",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 len));
2992 }
2993 } else {
Steve French50c2f752007-07-13 00:33:32 +00002994 cERROR(1, ("Security Blob Length extends beyond"
2995 " end of SMB"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 }
2997 } else {
2998 cERROR(1, ("No session structure passed in."));
2999 }
3000 } else {
3001 cERROR(1,
Steve French5815449d2006-02-14 01:36:20 +00003002 (" Invalid Word count %d:",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 smb_buffer_response->WordCount));
3004 rc = -EIO;
3005 }
3006
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00003007 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009 return rc;
3010}
3011static int
3012CIFSNTLMSSPAuthSessSetup(unsigned int xid, struct cifsSesInfo *ses,
Steve French4b18f2a2008-04-29 00:06:05 +00003013 char *ntlm_session_key, bool ntlmv2_flag,
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003014 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
3016 struct smb_hdr *smb_buffer;
3017 struct smb_hdr *smb_buffer_response;
3018 SESSION_SETUP_ANDX *pSMB;
3019 SESSION_SETUP_ANDX *pSMBr;
3020 char *bcc_ptr;
3021 char *user;
3022 char *domain;
3023 int rc = 0;
3024 int remaining_words = 0;
3025 int bytes_returned = 0;
3026 int len;
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003027 int SecurityBlobLength = sizeof(AUTHENTICATE_MESSAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 PAUTHENTICATE_MESSAGE SecurityBlob;
3029 __u32 negotiate_flags, capabilities;
3030 __u16 count;
3031
3032 cFYI(1, ("In NTLMSSPSessSetup (Authenticate)"));
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003033 if (ses == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return -EINVAL;
3035 user = ses->userName;
3036 domain = ses->domainName;
3037 smb_buffer = cifs_buf_get();
3038 if (smb_buffer == NULL) {
3039 return -ENOMEM;
3040 }
3041 smb_buffer_response = smb_buffer;
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003042 pSMB = (SESSION_SETUP_ANDX *)smb_buffer;
3043 pSMBr = (SESSION_SETUP_ANDX *)smb_buffer_response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045 /* send SMBsessionSetup here */
3046 header_assemble(smb_buffer, SMB_COM_SESSION_SETUP_ANDX,
3047 NULL /* no tCon exists yet */ , 12 /* wct */ );
Steve French1982c342005-08-17 12:38:22 -07003048
3049 smb_buffer->Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 pSMB->req.hdr.Flags |= (SMBFLG_CASELESS | SMBFLG_CANONICAL_PATH_FORMAT);
3051 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
3052 pSMB->req.AndXCommand = 0xFF;
3053 pSMB->req.MaxBufferSize = cpu_to_le16(ses->server->maxBuf);
3054 pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
3055
3056 pSMB->req.hdr.Uid = ses->Suid;
3057
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003058 if (ses->server->secMode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3060
3061 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003062 CAP_EXTENDED_SECURITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 if (ses->capabilities & CAP_UNICODE) {
3064 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3065 capabilities |= CAP_UNICODE;
3066 }
3067 if (ses->capabilities & CAP_STATUS32) {
3068 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3069 capabilities |= CAP_STATUS32;
3070 }
3071 if (ses->capabilities & CAP_DFS) {
3072 smb_buffer->Flags2 |= SMBFLG2_DFS;
3073 capabilities |= CAP_DFS;
3074 }
3075 pSMB->req.Capabilities = cpu_to_le32(capabilities);
3076
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003077 bcc_ptr = (char *)&pSMB->req.SecurityBlob;
3078 SecurityBlob = (PAUTHENTICATE_MESSAGE)bcc_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 strncpy(SecurityBlob->Signature, NTLMSSP_SIGNATURE, 8);
3080 SecurityBlob->MessageType = NtLmAuthenticate;
3081 bcc_ptr += SecurityBlobLength;
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003082 negotiate_flags = NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_REQUEST_TARGET |
3083 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_TARGET_INFO |
3084 0x80000000 | NTLMSSP_NEGOTIATE_128;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003085 if (sign_CIFS_PDUs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 negotiate_flags |= /* NTLMSSP_NEGOTIATE_ALWAYS_SIGN |*/ NTLMSSP_NEGOTIATE_SIGN;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003087 if (ntlmv2_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 negotiate_flags |= NTLMSSP_NEGOTIATE_NTLMV2;
3089
3090/* setup pointers to domain name and workstation name */
3091
3092 SecurityBlob->WorkstationName.Buffer = 0;
3093 SecurityBlob->WorkstationName.Length = 0;
3094 SecurityBlob->WorkstationName.MaximumLength = 0;
3095 SecurityBlob->SessionKey.Length = 0;
3096 SecurityBlob->SessionKey.MaximumLength = 0;
3097 SecurityBlob->SessionKey.Buffer = 0;
3098
3099 SecurityBlob->LmChallengeResponse.Length = 0;
3100 SecurityBlob->LmChallengeResponse.MaximumLength = 0;
3101 SecurityBlob->LmChallengeResponse.Buffer = 0;
3102
3103 SecurityBlob->NtChallengeResponse.Length =
Steve French7c7b25b2006-06-01 19:20:10 +00003104 cpu_to_le16(CIFS_SESS_KEY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 SecurityBlob->NtChallengeResponse.MaximumLength =
Steve French7c7b25b2006-06-01 19:20:10 +00003106 cpu_to_le16(CIFS_SESS_KEY_SIZE);
3107 memcpy(bcc_ptr, ntlm_session_key, CIFS_SESS_KEY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 SecurityBlob->NtChallengeResponse.Buffer =
3109 cpu_to_le32(SecurityBlobLength);
Steve French7c7b25b2006-06-01 19:20:10 +00003110 SecurityBlobLength += CIFS_SESS_KEY_SIZE;
3111 bcc_ptr += CIFS_SESS_KEY_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113 if (ses->capabilities & CAP_UNICODE) {
3114 if (domain == NULL) {
3115 SecurityBlob->DomainName.Buffer = 0;
3116 SecurityBlob->DomainName.Length = 0;
3117 SecurityBlob->DomainName.MaximumLength = 0;
3118 } else {
Steve French77159b42007-08-31 01:10:17 +00003119 __u16 ln = cifs_strtoUCS((__le16 *) bcc_ptr, domain, 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 nls_codepage);
Steve French77159b42007-08-31 01:10:17 +00003121 ln *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 SecurityBlob->DomainName.MaximumLength =
Steve French77159b42007-08-31 01:10:17 +00003123 cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 SecurityBlob->DomainName.Buffer =
3125 cpu_to_le32(SecurityBlobLength);
Steve French77159b42007-08-31 01:10:17 +00003126 bcc_ptr += ln;
3127 SecurityBlobLength += ln;
3128 SecurityBlob->DomainName.Length = cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 }
3130 if (user == NULL) {
3131 SecurityBlob->UserName.Buffer = 0;
3132 SecurityBlob->UserName.Length = 0;
3133 SecurityBlob->UserName.MaximumLength = 0;
3134 } else {
Steve French77159b42007-08-31 01:10:17 +00003135 __u16 ln = cifs_strtoUCS((__le16 *) bcc_ptr, user, 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 nls_codepage);
Steve French77159b42007-08-31 01:10:17 +00003137 ln *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 SecurityBlob->UserName.MaximumLength =
Steve French77159b42007-08-31 01:10:17 +00003139 cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 SecurityBlob->UserName.Buffer =
3141 cpu_to_le32(SecurityBlobLength);
Steve French77159b42007-08-31 01:10:17 +00003142 bcc_ptr += ln;
3143 SecurityBlobLength += ln;
3144 SecurityBlob->UserName.Length = cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 }
3146
Steve French63135e02007-07-17 17:34:02 +00003147 /* SecurityBlob->WorkstationName.Length =
3148 cifs_strtoUCS((__le16 *) bcc_ptr, "AMACHINE",64, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 SecurityBlob->WorkstationName.Length *= 2;
Steve French63135e02007-07-17 17:34:02 +00003150 SecurityBlob->WorkstationName.MaximumLength =
3151 cpu_to_le16(SecurityBlob->WorkstationName.Length);
3152 SecurityBlob->WorkstationName.Buffer =
3153 cpu_to_le32(SecurityBlobLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 bcc_ptr += SecurityBlob->WorkstationName.Length;
3155 SecurityBlobLength += SecurityBlob->WorkstationName.Length;
Steve French63135e02007-07-17 17:34:02 +00003156 SecurityBlob->WorkstationName.Length =
3157 cpu_to_le16(SecurityBlob->WorkstationName.Length); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
3159 if ((long) bcc_ptr % 2) {
3160 *bcc_ptr = 0;
3161 bcc_ptr++;
3162 }
3163 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08003164 cifs_strtoUCS((__le16 *) bcc_ptr, "Linux version ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 32, nls_codepage);
3166 bcc_ptr += 2 * bytes_returned;
3167 bytes_returned =
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07003168 cifs_strtoUCS((__le16 *) bcc_ptr, utsname()->release, 32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 nls_codepage);
3170 bcc_ptr += 2 * bytes_returned;
3171 bcc_ptr += 2; /* null term version string */
3172 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08003173 cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 64, nls_codepage);
3175 bcc_ptr += 2 * bytes_returned;
3176 *(bcc_ptr + 1) = 0;
3177 *(bcc_ptr + 2) = 0;
3178 bcc_ptr += 2; /* null terminate network opsys string */
3179 *(bcc_ptr + 1) = 0;
3180 *(bcc_ptr + 2) = 0;
3181 bcc_ptr += 2; /* null domain */
3182 } else { /* ASCII */
3183 if (domain == NULL) {
3184 SecurityBlob->DomainName.Buffer = 0;
3185 SecurityBlob->DomainName.Length = 0;
3186 SecurityBlob->DomainName.MaximumLength = 0;
3187 } else {
Steve French77159b42007-08-31 01:10:17 +00003188 __u16 ln;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 negotiate_flags |= NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED;
3190 strncpy(bcc_ptr, domain, 63);
Steve French77159b42007-08-31 01:10:17 +00003191 ln = strnlen(domain, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 SecurityBlob->DomainName.MaximumLength =
Steve French77159b42007-08-31 01:10:17 +00003193 cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 SecurityBlob->DomainName.Buffer =
3195 cpu_to_le32(SecurityBlobLength);
Steve French77159b42007-08-31 01:10:17 +00003196 bcc_ptr += ln;
3197 SecurityBlobLength += ln;
3198 SecurityBlob->DomainName.Length = cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 }
3200 if (user == NULL) {
3201 SecurityBlob->UserName.Buffer = 0;
3202 SecurityBlob->UserName.Length = 0;
3203 SecurityBlob->UserName.MaximumLength = 0;
3204 } else {
Steve French77159b42007-08-31 01:10:17 +00003205 __u16 ln;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 strncpy(bcc_ptr, user, 63);
Steve French77159b42007-08-31 01:10:17 +00003207 ln = strnlen(user, 64);
3208 SecurityBlob->UserName.MaximumLength = cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 SecurityBlob->UserName.Buffer =
Steve French77159b42007-08-31 01:10:17 +00003210 cpu_to_le32(SecurityBlobLength);
3211 bcc_ptr += ln;
3212 SecurityBlobLength += ln;
3213 SecurityBlob->UserName.Length = cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 }
3215 /* BB fill in our workstation name if known BB */
3216
3217 strcpy(bcc_ptr, "Linux version ");
3218 bcc_ptr += strlen("Linux version ");
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07003219 strcpy(bcc_ptr, utsname()->release);
3220 bcc_ptr += strlen(utsname()->release) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
3222 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
3223 bcc_ptr++; /* null domain */
3224 *bcc_ptr = 0;
3225 }
3226 SecurityBlob->NegotiateFlags = cpu_to_le32(negotiate_flags);
3227 pSMB->req.SecurityBlobLength = cpu_to_le16(SecurityBlobLength);
3228 count = (long) bcc_ptr - (long) pByteArea(smb_buffer);
3229 smb_buffer->smb_buf_length += count;
3230 pSMB->req.ByteCount = cpu_to_le16(count);
3231
3232 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response,
Steve French133672e2007-11-13 22:41:37 +00003233 &bytes_returned, CIFS_LONG_OP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 if (rc) {
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003235/* rc = map_smb_to_linux_error(smb_buffer_response) done in SendReceive now */
3236 } else if ((smb_buffer_response->WordCount == 3) ||
3237 (smb_buffer_response->WordCount == 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 __u16 action = le16_to_cpu(pSMBr->resp.Action);
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003239 __u16 blob_len = le16_to_cpu(pSMBr->resp.SecurityBlobLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if (action & GUEST_LOGIN)
Steve French50c2f752007-07-13 00:33:32 +00003241 cFYI(1, (" Guest login")); /* BB Should we set anything
3242 in SesInfo struct ? */
3243/* if (SecurityBlob2->MessageType != NtLm??) {
3244 cFYI("Unexpected message type on auth response is %d"));
3245 } */
3246
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 if (ses) {
3248 cFYI(1,
Steve French50c2f752007-07-13 00:33:32 +00003249 ("Check challenge UID %d vs auth response UID %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 ses->Suid, smb_buffer_response->Uid));
Steve French50c2f752007-07-13 00:33:32 +00003251 /* UID left in wire format */
3252 ses->Suid = smb_buffer_response->Uid;
3253 bcc_ptr = pByteArea(smb_buffer_response);
3254 /* response can have either 3 or 4 word count - Samba sends 3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 if ((pSMBr->resp.hdr.WordCount == 3)
3256 || ((pSMBr->resp.hdr.WordCount == 4)
3257 && (blob_len <
3258 pSMBr->resp.ByteCount))) {
3259 if (pSMBr->resp.hdr.WordCount == 4) {
3260 bcc_ptr +=
3261 blob_len;
3262 cFYI(1,
3263 ("Security Blob Length %d ",
3264 blob_len));
3265 }
3266
3267 cFYI(1,
3268 ("NTLMSSP response to Authenticate "));
3269
3270 if (smb_buffer->Flags2 & SMBFLG2_UNICODE) {
3271 if ((long) (bcc_ptr) % 2) {
3272 remaining_words =
3273 (BCC(smb_buffer_response)
3274 - 1) / 2;
3275 bcc_ptr++; /* Unicode strings must be word aligned */
3276 } else {
3277 remaining_words = BCC(smb_buffer_response) / 2;
3278 }
Steve French77159b42007-08-31 01:10:17 +00003279 len = UniStrnlen((wchar_t *) bcc_ptr,
3280 remaining_words - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281/* We look for obvious messed up bcc or strings in response so we do not go off
3282 the end since (at least) WIN2K and Windows XP have a major bug in not null
3283 terminating last Unicode string in response */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003284 if (ses->serverOS)
Steve French08775832006-05-30 18:08:26 +00003285 kfree(ses->serverOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 ses->serverOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07003287 kzalloc(2 * (len + 1), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 cifs_strfromUCS_le(ses->serverOS,
Steve Frenche89dc922005-11-11 15:18:19 -08003289 (__le16 *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 bcc_ptr, len,
3291 nls_codepage);
3292 bcc_ptr += 2 * (len + 1);
3293 remaining_words -= len + 1;
3294 ses->serverOS[2 * len] = 0;
3295 ses->serverOS[1 + (2 * len)] = 0;
3296 if (remaining_words > 0) {
3297 len = UniStrnlen((wchar_t *)
3298 bcc_ptr,
3299 remaining_words
3300 - 1);
Steve Frenchcd49b492006-06-26 04:22:36 +00003301 kfree(ses->serverNOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 ses->serverNOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07003303 kzalloc(2 * (len + 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 GFP_KERNEL);
3305 cifs_strfromUCS_le(ses->
3306 serverNOS,
Steve Frenche89dc922005-11-11 15:18:19 -08003307 (__le16 *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 bcc_ptr,
3309 len,
3310 nls_codepage);
3311 bcc_ptr += 2 * (len + 1);
3312 ses->serverNOS[2 * len] = 0;
3313 ses->serverNOS[1+(2*len)] = 0;
3314 remaining_words -= len + 1;
3315 if (remaining_words > 0) {
Steve French50c2f752007-07-13 00:33:32 +00003316 len = UniStrnlen((wchar_t *) bcc_ptr, remaining_words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 /* last string not always null terminated (e.g. for Windows XP & 2000) */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003318 if (ses->serverDomain)
Steve Frencha424f8b2006-05-30 18:06:04 +00003319 kfree(ses->serverDomain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 ses->serverDomain =
Pekka Enberge915fc42005-09-06 15:18:35 -07003321 kzalloc(2 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 (len +
3323 1),
3324 GFP_KERNEL);
3325 cifs_strfromUCS_le
3326 (ses->
3327 serverDomain,
Steve Frenche89dc922005-11-11 15:18:19 -08003328 (__le16 *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 bcc_ptr, len,
3330 nls_codepage);
3331 bcc_ptr +=
3332 2 * (len + 1);
3333 ses->
3334 serverDomain[2
3335 * len]
3336 = 0;
3337 ses->
3338 serverDomain[1
3339 +
3340 (2
3341 *
3342 len)]
3343 = 0;
3344 } /* else no more room so create dummy domain string */
Steve Frencha424f8b2006-05-30 18:06:04 +00003345 else {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003346 if (ses->serverDomain)
Steve Frencha424f8b2006-05-30 18:06:04 +00003347 kfree(ses->serverDomain);
Pekka Enberge915fc42005-09-06 15:18:35 -07003348 ses->serverDomain = kzalloc(2,GFP_KERNEL);
Steve Frencha424f8b2006-05-30 18:06:04 +00003349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 } else { /* no room so create dummy domain and NOS string */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003351 if (ses->serverDomain)
Steve Frencha424f8b2006-05-30 18:06:04 +00003352 kfree(ses->serverDomain);
Pekka Enberge915fc42005-09-06 15:18:35 -07003353 ses->serverDomain = kzalloc(2, GFP_KERNEL);
Steve Frenchcd49b492006-06-26 04:22:36 +00003354 kfree(ses->serverNOS);
Pekka Enberge915fc42005-09-06 15:18:35 -07003355 ses->serverNOS = kzalloc(2, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 }
3357 } else { /* ASCII */
3358 len = strnlen(bcc_ptr, 1024);
Steve French50c2f752007-07-13 00:33:32 +00003359 if (((long) bcc_ptr + len) -
3360 (long) pByteArea(smb_buffer_response)
Steve French63135e02007-07-17 17:34:02 +00003361 <= BCC(smb_buffer_response)) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003362 if (ses->serverOS)
Steve Frencha424f8b2006-05-30 18:06:04 +00003363 kfree(ses->serverOS);
Steve French77159b42007-08-31 01:10:17 +00003364 ses->serverOS = kzalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 strncpy(ses->serverOS,bcc_ptr, len);
3366
3367 bcc_ptr += len;
3368 bcc_ptr[0] = 0; /* null terminate the string */
3369 bcc_ptr++;
3370
3371 len = strnlen(bcc_ptr, 1024);
Steve Frenchcd49b492006-06-26 04:22:36 +00003372 kfree(ses->serverNOS);
Steve French50c2f752007-07-13 00:33:32 +00003373 ses->serverNOS = kzalloc(len+1,
3374 GFP_KERNEL);
Steve French63135e02007-07-17 17:34:02 +00003375 strncpy(ses->serverNOS,
3376 bcc_ptr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 bcc_ptr += len;
3378 bcc_ptr[0] = 0;
3379 bcc_ptr++;
3380
3381 len = strnlen(bcc_ptr, 1024);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003382 if (ses->serverDomain)
Steve Frencha424f8b2006-05-30 18:06:04 +00003383 kfree(ses->serverDomain);
Steve French63135e02007-07-17 17:34:02 +00003384 ses->serverDomain =
3385 kzalloc(len+1,
3386 GFP_KERNEL);
3387 strncpy(ses->serverDomain,
3388 bcc_ptr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 bcc_ptr += len;
3390 bcc_ptr[0] = 0;
3391 bcc_ptr++;
3392 } else
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003393 cFYI(1, ("field of length %d "
Steve French63135e02007-07-17 17:34:02 +00003394 "extends beyond end of smb ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 len));
3396 }
3397 } else {
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003398 cERROR(1, ("Security Blob extends beyond end "
Steve French63135e02007-07-17 17:34:02 +00003399 "of SMB"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 }
3401 } else {
3402 cERROR(1, ("No session structure passed in."));
3403 }
3404 } else {
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003405 cERROR(1, ("Invalid Word count %d: ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 smb_buffer_response->WordCount));
3407 rc = -EIO;
3408 }
3409
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00003410 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
3412 return rc;
3413}
3414
3415int
3416CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
3417 const char *tree, struct cifsTconInfo *tcon,
3418 const struct nls_table *nls_codepage)
3419{
3420 struct smb_hdr *smb_buffer;
3421 struct smb_hdr *smb_buffer_response;
3422 TCONX_REQ *pSMB;
3423 TCONX_RSP *pSMBr;
3424 unsigned char *bcc_ptr;
3425 int rc = 0;
3426 int length;
3427 __u16 count;
3428
3429 if (ses == NULL)
3430 return -EIO;
3431
3432 smb_buffer = cifs_buf_get();
3433 if (smb_buffer == NULL) {
3434 return -ENOMEM;
3435 }
3436 smb_buffer_response = smb_buffer;
3437
3438 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3439 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07003440
3441 smb_buffer->Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 smb_buffer->Uid = ses->Suid;
3443 pSMB = (TCONX_REQ *) smb_buffer;
3444 pSMBr = (TCONX_RSP *) smb_buffer_response;
3445
3446 pSMB->AndXCommand = 0xFF;
3447 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 bcc_ptr = &pSMB->Password[0];
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003449 if ((ses->server->secMode) & SECMODE_USER) {
Steve Frencheeac8042006-01-13 21:34:58 -08003450 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00003451 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08003452 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00003453 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08003454 } else {
Steve French7c7b25b2006-06-01 19:20:10 +00003455 pSMB->PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08003456 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
3457 specified as required (when that support is added to
3458 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00003459 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08003460 by Samba (not sure whether other servers allow
3461 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00003462#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French50c2f752007-07-13 00:33:32 +00003463 if ((extended_security & CIFSSEC_MAY_LANMAN) &&
Steve French7c7b25b2006-06-01 19:20:10 +00003464 (ses->server->secType == LANMAN))
3465 calc_lanman_hash(ses, bcc_ptr);
3466 else
3467#endif /* CIFS_WEAK_PW_HASH */
Steve Frencheeac8042006-01-13 21:34:58 -08003468 SMBNTencrypt(ses->password,
3469 ses->server->cryptKey,
3470 bcc_ptr);
3471
Steve French7c7b25b2006-06-01 19:20:10 +00003472 bcc_ptr += CIFS_SESS_KEY_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003473 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00003474 /* must align unicode strings */
3475 *bcc_ptr = 0; /* null byte password */
3476 bcc_ptr++;
3477 }
Steve Frencheeac8042006-01-13 21:34:58 -08003478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Steve French50c2f752007-07-13 00:33:32 +00003480 if (ses->server->secMode &
Steve Frencha878fb22006-05-30 18:04:19 +00003481 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3483
3484 if (ses->capabilities & CAP_STATUS32) {
3485 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3486 }
3487 if (ses->capabilities & CAP_DFS) {
3488 smb_buffer->Flags2 |= SMBFLG2_DFS;
3489 }
3490 if (ses->capabilities & CAP_UNICODE) {
3491 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3492 length =
Steve French50c2f752007-07-13 00:33:32 +00003493 cifs_strtoUCS((__le16 *) bcc_ptr, tree,
3494 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00003495 (/* server len*/ + 256 /* share len */), nls_codepage);
3496 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 bcc_ptr += 2; /* skip trailing null */
3498 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 strcpy(bcc_ptr, tree);
3500 bcc_ptr += strlen(tree) + 1;
3501 }
3502 strcpy(bcc_ptr, "?????");
3503 bcc_ptr += strlen("?????");
3504 bcc_ptr += 1;
3505 count = bcc_ptr - &pSMB->Password[0];
3506 pSMB->hdr.smb_buf_length += count;
3507 pSMB->ByteCount = cpu_to_le16(count);
3508
Steve French133672e2007-11-13 22:41:37 +00003509 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
3510 CIFS_STD_OP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
3512 /* if (rc) rc = map_smb_to_linux_error(smb_buffer_response); */
3513 /* above now done in SendReceive */
3514 if ((rc == 0) && (tcon != NULL)) {
3515 tcon->tidStatus = CifsGood;
3516 tcon->tid = smb_buffer_response->Tid;
3517 bcc_ptr = pByteArea(smb_buffer_response);
3518 length = strnlen(bcc_ptr, BCC(smb_buffer_response) - 2);
Steve French50c2f752007-07-13 00:33:32 +00003519 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00003520 if (length == 3) {
3521 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3522 (bcc_ptr[2] == 'C')) {
3523 cFYI(1, ("IPC connection"));
3524 tcon->ipc = 1;
3525 }
3526 } else if (length == 2) {
3527 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3528 /* the most common case */
3529 cFYI(1, ("disk share connection"));
3530 }
3531 }
Steve French50c2f752007-07-13 00:33:32 +00003532 bcc_ptr += length + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 strncpy(tcon->treeName, tree, MAX_TREE_SIZE);
3534 if (smb_buffer->Flags2 & SMBFLG2_UNICODE) {
3535 length = UniStrnlen((wchar_t *) bcc_ptr, 512);
3536 if ((bcc_ptr + (2 * length)) -
3537 pByteArea(smb_buffer_response) <=
3538 BCC(smb_buffer_response)) {
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003539 kfree(tcon->nativeFileSystem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 tcon->nativeFileSystem =
Pekka Enberge915fc42005-09-06 15:18:35 -07003541 kzalloc(length + 2, GFP_KERNEL);
Steve French88f370a2007-09-15 03:01:17 +00003542 if (tcon->nativeFileSystem)
3543 cifs_strfromUCS_le(
3544 tcon->nativeFileSystem,
3545 (__le16 *) bcc_ptr,
3546 length, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 bcc_ptr += 2 * length;
3548 bcc_ptr[0] = 0; /* null terminate the string */
3549 bcc_ptr[1] = 0;
3550 bcc_ptr += 2;
3551 }
Steve French50c2f752007-07-13 00:33:32 +00003552 /* else do not bother copying these information fields*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 } else {
3554 length = strnlen(bcc_ptr, 1024);
3555 if ((bcc_ptr + length) -
3556 pByteArea(smb_buffer_response) <=
3557 BCC(smb_buffer_response)) {
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003558 kfree(tcon->nativeFileSystem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 tcon->nativeFileSystem =
Pekka Enberge915fc42005-09-06 15:18:35 -07003560 kzalloc(length + 1, GFP_KERNEL);
Steve French88f370a2007-09-15 03:01:17 +00003561 if (tcon->nativeFileSystem)
3562 strncpy(tcon->nativeFileSystem, bcc_ptr,
3563 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 }
Steve French50c2f752007-07-13 00:33:32 +00003565 /* else do not bother copying these information fields*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003567 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00003568 (smb_buffer_response->WordCount == 7))
3569 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00003570 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
3571 else
3572 tcon->Flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 cFYI(1, ("Tcon flags: 0x%x ", tcon->Flags));
3574 } else if ((rc == 0) && tcon == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003575 /* all we need to save for IPC$ connection */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 ses->ipc_tid = smb_buffer_response->Tid;
3577 }
3578
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00003579 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 return rc;
3581}
3582
3583int
3584cifs_umount(struct super_block *sb, struct cifs_sb_info *cifs_sb)
3585{
3586 int rc = 0;
3587 int xid;
3588 struct cifsSesInfo *ses = NULL;
Steve French50c2f752007-07-13 00:33:32 +00003589 char *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
3591 xid = GetXid();
3592
3593 if (cifs_sb->tcon) {
3594 ses = cifs_sb->tcon->ses; /* save ptr to ses before delete tcon!*/
3595 rc = CIFSSMBTDis(xid, cifs_sb->tcon);
3596 if (rc == -EBUSY) {
3597 FreeXid(xid);
3598 return 0;
3599 }
Steve French5d941ca2008-04-15 18:40:48 +00003600 DeleteTconOplockQEntries(cifs_sb->tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 tconInfoFree(cifs_sb->tcon);
3602 if ((ses) && (ses->server)) {
3603 /* save off task so we do not refer to ses later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 cFYI(1, ("About to do SMBLogoff "));
3605 rc = CIFSSMBLogoff(xid, ses);
3606 if (rc == -EBUSY) {
3607 FreeXid(xid);
3608 return 0;
3609 } else if (rc == -ESHUTDOWN) {
Steve French467a8f82007-06-27 22:41:32 +00003610 cFYI(1, ("Waking up socket by sending signal"));
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003611 if (ses->server)
3612 kill_cifsd(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 rc = 0;
3614 } /* else - we have an smb session
3615 left on this socket do not kill cifsd */
3616 } else
3617 cFYI(1, ("No session or bad tcon"));
3618 }
Steve French50c2f752007-07-13 00:33:32 +00003619
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 cifs_sb->tcon = NULL;
Steve French2fe87f02006-09-21 07:02:52 +00003621 tmp = cifs_sb->prepath;
3622 cifs_sb->prepathlen = 0;
3623 cifs_sb->prepath = NULL;
3624 kfree(tmp);
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07003625 if (ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 sesInfoFree(ses);
3627
3628 FreeXid(xid);
Steve French88e7d702008-01-03 17:37:09 +00003629 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003630}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
3632int cifs_setup_session(unsigned int xid, struct cifsSesInfo *pSesInfo,
Steve French50c2f752007-07-13 00:33:32 +00003633 struct nls_table *nls_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
3635 int rc = 0;
Steve French7c7b25b2006-06-01 19:20:10 +00003636 char ntlm_session_key[CIFS_SESS_KEY_SIZE];
Steve French4b18f2a2008-04-29 00:06:05 +00003637 bool ntlmv2_flag = false;
Steve Frenchad009ac2005-04-28 22:41:05 -07003638 int first_time = 0;
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003639 struct TCP_Server_Info *server = pSesInfo->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
3641 /* what if server changes its buffer size after dropping the session? */
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003642 if (server->maxBuf == 0) /* no need to send on reconnect */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 rc = CIFSSMBNegotiate(xid, pSesInfo);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003644 if (rc == -EAGAIN) {
3645 /* retry only once on 1st time connection */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 rc = CIFSSMBNegotiate(xid, pSesInfo);
Steve French50c2f752007-07-13 00:33:32 +00003647 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 rc = -EHOSTDOWN;
3649 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003650 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 spin_lock(&GlobalMid_Lock);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003652 if (server->tcpStatus != CifsExiting)
3653 server->tcpStatus = CifsGood;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 else
3655 rc = -EHOSTDOWN;
3656 spin_unlock(&GlobalMid_Lock);
3657
3658 }
Steve Frenchad009ac2005-04-28 22:41:05 -07003659 first_time = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 }
Steve French26b994f2008-08-06 05:11:33 +00003661
3662 if (rc)
3663 goto ss_err_exit;
3664
3665 pSesInfo->flags = 0;
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003666 pSesInfo->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00003667 if (linuxExtEnabled == 0)
3668 pSesInfo->capabilities &= (~CAP_UNIX);
Steve Frenchad009ac2005-04-28 22:41:05 -07003669 /* pSesInfo->sequence_number = 0;*/
Steve French26b994f2008-08-06 05:11:33 +00003670 cFYI(1, ("Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d",
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003671 server->secMode, server->capabilities, server->timeAdj));
3672
Steve French26b994f2008-08-06 05:11:33 +00003673 if (experimEnabled < 2)
3674 rc = CIFS_SessSetup(xid, pSesInfo, first_time, nls_info);
3675 else if (extended_security
3676 && (pSesInfo->capabilities & CAP_EXTENDED_SECURITY)
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003677 && (server->secType == NTLMSSP)) {
Steve French26b994f2008-08-06 05:11:33 +00003678 rc = -EOPNOTSUPP;
3679 } else if (extended_security
3680 && (pSesInfo->capabilities & CAP_EXTENDED_SECURITY)
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003681 && (server->secType == RawNTLMSSP)) {
Steve French26b994f2008-08-06 05:11:33 +00003682 cFYI(1, ("NTLMSSP sesssetup"));
3683 rc = CIFSNTLMSSPNegotiateSessSetup(xid, pSesInfo, &ntlmv2_flag,
3684 nls_info);
3685 if (!rc) {
3686 if (ntlmv2_flag) {
3687 char *v2_response;
3688 cFYI(1, ("more secure NTLM ver2 hash"));
3689 if (CalcNTLMv2_partial_mac_key(pSesInfo,
3690 nls_info)) {
3691 rc = -ENOMEM;
3692 goto ss_err_exit;
3693 } else
3694 v2_response = kmalloc(16 + 64 /* blob*/,
3695 GFP_KERNEL);
3696 if (v2_response) {
3697 CalcNTLMv2_response(pSesInfo,
3698 v2_response);
3699 /* if (first_time)
3700 cifs_calculate_ntlmv2_mac_key */
3701 kfree(v2_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 /* BB Put dummy sig in SessSetup PDU? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 } else {
Steve French26b994f2008-08-06 05:11:33 +00003704 rc = -ENOMEM;
3705 goto ss_err_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 }
Steve French26b994f2008-08-06 05:11:33 +00003707
3708 } else {
3709 SMBNTencrypt(pSesInfo->password,
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003710 server->cryptKey,
Steve French26b994f2008-08-06 05:11:33 +00003711 ntlm_session_key);
3712
3713 if (first_time)
3714 cifs_calculate_mac_key(
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003715 &server->mac_signing_key,
Steve French26b994f2008-08-06 05:11:33 +00003716 ntlm_session_key,
3717 pSesInfo->password);
3718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 /* for better security the weaker lanman hash not sent
3720 in AuthSessSetup so we no longer calculate it */
3721
Steve French26b994f2008-08-06 05:11:33 +00003722 rc = CIFSNTLMSSPAuthSessSetup(xid, pSesInfo,
3723 ntlm_session_key,
3724 ntlmv2_flag,
3725 nls_info);
3726 }
3727 } else { /* old style NTLM 0.12 session setup */
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003728 SMBNTencrypt(pSesInfo->password, server->cryptKey,
Steve French26b994f2008-08-06 05:11:33 +00003729 ntlm_session_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Steve French26b994f2008-08-06 05:11:33 +00003731 if (first_time)
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003732 cifs_calculate_mac_key(&server->mac_signing_key,
3733 ntlm_session_key,
3734 pSesInfo->password);
Steve Frenchad009ac2005-04-28 22:41:05 -07003735
Steve French26b994f2008-08-06 05:11:33 +00003736 rc = CIFSSessSetup(xid, pSesInfo, ntlm_session_key, nls_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 }
Steve French26b994f2008-08-06 05:11:33 +00003738 if (rc) {
3739 cERROR(1, ("Send error in SessSetup = %d", rc));
3740 } else {
3741 cFYI(1, ("CIFS Session Established successfully"));
Jeff Layton469ee612008-10-16 18:46:39 +00003742 spin_lock(&GlobalMid_Lock);
Steve French26b994f2008-08-06 05:11:33 +00003743 pSesInfo->status = CifsGood;
Jeff Layton469ee612008-10-16 18:46:39 +00003744 spin_unlock(&GlobalMid_Lock);
Steve French26b994f2008-08-06 05:11:33 +00003745 }
3746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747ss_err_exit:
3748 return rc;
3749}
3750