blob: 1126f7ab4606083635546ba8f485b7ae7940615b [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 French2c1b8612008-10-16 18:35:21 +000093 bool nodfs:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 unsigned int rsize;
95 unsigned int wsize;
96 unsigned int sockopt;
97 unsigned short int port;
Steve Frenchfb8c4b12007-07-10 01:16:18 +000098 char *prepath;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000101static int ipv4_connect(struct sockaddr_in *psin_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct socket **csocket,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000103 char *netb_name,
104 char *server_netb_name);
105static int ipv6_connect(struct sockaddr_in6 *psin_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct socket **csocket);
107
108
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000109 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * cifs tcp session reconnection
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000111 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * mark tcp session as reconnecting so temporarily locked
113 * mark all smb sessions as reconnecting for tcp session
114 * reconnect tcp session
115 * wake up waiters on reconnection? - (not needed currently)
116 */
117
Steve French2cd646a2006-09-28 19:43:08 +0000118static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119cifs_reconnect(struct TCP_Server_Info *server)
120{
121 int rc = 0;
122 struct list_head *tmp;
123 struct cifsSesInfo *ses;
124 struct cifsTconInfo *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000125 struct mid_q_entry *mid_entry;
Steve French50c2f752007-07-13 00:33:32 +0000126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000128 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000129 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 next time through the loop */
131 spin_unlock(&GlobalMid_Lock);
132 return rc;
133 } else
134 server->tcpStatus = CifsNeedReconnect;
135 spin_unlock(&GlobalMid_Lock);
136 server->maxBuf = 0;
137
Steve Frenche4eb2952005-04-28 22:41:09 -0700138 cFYI(1, ("Reconnecting tcp session"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /* before reconnecting the tcp session, mark the smb session (uid)
141 and the tid bad so they are not used until reconnected */
142 read_lock(&GlobalSMBSeslock);
143 list_for_each(tmp, &GlobalSMBSessionList) {
144 ses = list_entry(tmp, struct cifsSesInfo, cifsSessionList);
145 if (ses->server) {
146 if (ses->server == server) {
147 ses->status = CifsNeedReconnect;
148 ses->ipc_tid = 0;
149 }
150 }
151 /* else tcp and smb sessions need reconnection */
152 }
153 list_for_each(tmp, &GlobalTreeConnectionList) {
154 tcon = list_entry(tmp, struct cifsTconInfo, cifsConnectionList);
Steve Frenchad8b15f2008-08-08 21:10:16 +0000155 if ((tcon->ses) && (tcon->ses->server == server))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 tcon->tidStatus = CifsNeedReconnect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158 read_unlock(&GlobalSMBSeslock);
159 /* do not want to be sending data on a socket we are freeing */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000160 down(&server->tcpSem);
161 if (server->ssocket) {
Steve French467a8f82007-06-27 22:41:32 +0000162 cFYI(1, ("State: 0x%x Flags: 0x%lx", server->ssocket->state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 server->ssocket->flags));
Trond Myklebust91cf45f2007-11-12 18:10:39 -0800164 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000165 cFYI(1, ("Post shutdown state: 0x%x Flags: 0x%lx",
Steve French467a8f82007-06-27 22:41:32 +0000166 server->ssocket->state,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 server->ssocket->flags));
168 sock_release(server->ssocket);
169 server->ssocket = NULL;
170 }
171
172 spin_lock(&GlobalMid_Lock);
173 list_for_each(tmp, &server->pending_mid_q) {
174 mid_entry = list_entry(tmp, struct
175 mid_q_entry,
176 qhead);
Steve Frenchad8b15f2008-08-08 21:10:16 +0000177 if (mid_entry->midState == MID_REQUEST_SUBMITTED) {
Steve French09d1db52005-04-28 22:41:08 -0700178 /* Mark other intransit requests as needing
179 retry so we do not immediately mark the
180 session bad again (ie after we reconnect
181 below) as they timeout too */
Steve Frenchad8b15f2008-08-08 21:10:16 +0000182 mid_entry->midState = MID_RETRY_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184 }
185 spin_unlock(&GlobalMid_Lock);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000186 up(&server->tcpSem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Jeff Layton469ee612008-10-16 18:46:39 +0000188 while ((server->tcpStatus != CifsExiting) &&
189 (server->tcpStatus != CifsGood)) {
Steve French6c3d8902006-07-31 22:46:20 +0000190 try_to_freeze();
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000191 if (server->protocolType == IPV6) {
192 rc = ipv6_connect(&server->addr.sockAddr6,
193 &server->ssocket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 } else {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000195 rc = ipv4_connect(&server->addr.sockAddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 &server->ssocket,
Steve Frencha10faeb22005-08-22 21:38:31 -0700197 server->workstation_RFC1001_name,
198 server->server_RFC1001_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000200 if (rc) {
201 cFYI(1, ("reconnect error %d", rc));
Steve French0cb766a2005-04-28 22:41:11 -0700202 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 } else {
204 atomic_inc(&tcpSesReconnectCount);
205 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000206 if (server->tcpStatus != CifsExiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 server->tcpStatus = CifsGood;
Steve Frenchad009ac2005-04-28 22:41:05 -0700208 server->sequence_number = 0;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000209 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* atomic_set(&server->inFlight,0);*/
211 wake_up(&server->response_q);
212 }
213 }
214 return rc;
215}
216
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000217/*
Steve Frenche4eb2952005-04-28 22:41:09 -0700218 return codes:
219 0 not a transact2, or all data present
220 >0 transact2 with that much data missing
221 -EINVAL = invalid transact2
222
223 */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000224static int check2ndT2(struct smb_hdr *pSMB, unsigned int maxBufSize)
Steve Frenche4eb2952005-04-28 22:41:09 -0700225{
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000226 struct smb_t2_rsp *pSMBt;
227 int total_data_size;
Steve Frenche4eb2952005-04-28 22:41:09 -0700228 int data_in_this_rsp;
229 int remaining;
230
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000231 if (pSMB->Command != SMB_COM_TRANSACTION2)
Steve Frenche4eb2952005-04-28 22:41:09 -0700232 return 0;
233
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000234 /* check for plausible wct, bcc and t2 data and parm sizes */
235 /* check for parm and data offset going beyond end of smb */
236 if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */
Steve French467a8f82007-06-27 22:41:32 +0000237 cFYI(1, ("invalid transact2 word count"));
Steve Frenche4eb2952005-04-28 22:41:09 -0700238 return -EINVAL;
239 }
240
241 pSMBt = (struct smb_t2_rsp *)pSMB;
242
243 total_data_size = le16_to_cpu(pSMBt->t2_rsp.TotalDataCount);
244 data_in_this_rsp = le16_to_cpu(pSMBt->t2_rsp.DataCount);
245
246 remaining = total_data_size - data_in_this_rsp;
247
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000248 if (remaining == 0)
Steve Frenche4eb2952005-04-28 22:41:09 -0700249 return 0;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000250 else if (remaining < 0) {
Steve French467a8f82007-06-27 22:41:32 +0000251 cFYI(1, ("total data %d smaller than data in frame %d",
Steve Frenche4eb2952005-04-28 22:41:09 -0700252 total_data_size, data_in_this_rsp));
253 return -EINVAL;
254 } else {
Steve French467a8f82007-06-27 22:41:32 +0000255 cFYI(1, ("missing %d bytes from transact2, check next response",
Steve Frenche4eb2952005-04-28 22:41:09 -0700256 remaining));
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000257 if (total_data_size > maxBufSize) {
258 cERROR(1, ("TotalDataSize %d is over maximum buffer %d",
259 total_data_size, maxBufSize));
260 return -EINVAL;
Steve Frenche4eb2952005-04-28 22:41:09 -0700261 }
262 return remaining;
263 }
264}
265
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000266static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
Steve Frenche4eb2952005-04-28 22:41:09 -0700267{
268 struct smb_t2_rsp *pSMB2 = (struct smb_t2_rsp *)psecond;
269 struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)pTargetSMB;
270 int total_data_size;
271 int total_in_buf;
272 int remaining;
273 int total_in_buf2;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000274 char *data_area_of_target;
275 char *data_area_of_buf2;
Steve Frenche4eb2952005-04-28 22:41:09 -0700276 __u16 byte_count;
277
278 total_data_size = le16_to_cpu(pSMBt->t2_rsp.TotalDataCount);
279
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000280 if (total_data_size != le16_to_cpu(pSMB2->t2_rsp.TotalDataCount)) {
Steve French63135e02007-07-17 17:34:02 +0000281 cFYI(1, ("total data size of primary and secondary t2 differ"));
Steve Frenche4eb2952005-04-28 22:41:09 -0700282 }
283
284 total_in_buf = le16_to_cpu(pSMBt->t2_rsp.DataCount);
285
286 remaining = total_data_size - total_in_buf;
Steve French50c2f752007-07-13 00:33:32 +0000287
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000288 if (remaining < 0)
Steve Frenche4eb2952005-04-28 22:41:09 -0700289 return -EINVAL;
290
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000291 if (remaining == 0) /* nothing to do, ignore */
Steve Frenche4eb2952005-04-28 22:41:09 -0700292 return 0;
Steve French50c2f752007-07-13 00:33:32 +0000293
Steve Frenche4eb2952005-04-28 22:41:09 -0700294 total_in_buf2 = le16_to_cpu(pSMB2->t2_rsp.DataCount);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000295 if (remaining < total_in_buf2) {
Steve French467a8f82007-06-27 22:41:32 +0000296 cFYI(1, ("transact2 2nd response contains too much data"));
Steve Frenche4eb2952005-04-28 22:41:09 -0700297 }
298
299 /* find end of first SMB data area */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000300 data_area_of_target = (char *)&pSMBt->hdr.Protocol +
Steve Frenche4eb2952005-04-28 22:41:09 -0700301 le16_to_cpu(pSMBt->t2_rsp.DataOffset);
302 /* validate target area */
303
304 data_area_of_buf2 = (char *) &pSMB2->hdr.Protocol +
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000305 le16_to_cpu(pSMB2->t2_rsp.DataOffset);
Steve Frenche4eb2952005-04-28 22:41:09 -0700306
307 data_area_of_target += total_in_buf;
308
309 /* copy second buffer into end of first buffer */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000310 memcpy(data_area_of_target, data_area_of_buf2, total_in_buf2);
Steve Frenche4eb2952005-04-28 22:41:09 -0700311 total_in_buf += total_in_buf2;
312 pSMBt->t2_rsp.DataCount = cpu_to_le16(total_in_buf);
313 byte_count = le16_to_cpu(BCC_LE(pTargetSMB));
314 byte_count += total_in_buf2;
315 BCC_LE(pTargetSMB) = cpu_to_le16(byte_count);
316
Steve French70ca7342005-09-22 16:32:06 -0700317 byte_count = pTargetSMB->smb_buf_length;
Steve Frenche4eb2952005-04-28 22:41:09 -0700318 byte_count += total_in_buf2;
319
320 /* BB also add check that we are not beyond maximum buffer size */
Steve French50c2f752007-07-13 00:33:32 +0000321
Steve French70ca7342005-09-22 16:32:06 -0700322 pTargetSMB->smb_buf_length = byte_count;
Steve Frenche4eb2952005-04-28 22:41:09 -0700323
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000324 if (remaining == total_in_buf2) {
Steve French467a8f82007-06-27 22:41:32 +0000325 cFYI(1, ("found the last secondary response"));
Steve Frenche4eb2952005-04-28 22:41:09 -0700326 return 0; /* we are done */
327 } else /* more responses to go */
328 return 1;
329
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static int
333cifs_demultiplex_thread(struct TCP_Server_Info *server)
334{
335 int length;
336 unsigned int pdu_length, total_read;
337 struct smb_hdr *smb_buffer = NULL;
Steve Frenchb8643e12005-04-28 22:41:07 -0700338 struct smb_hdr *bigbuf = NULL;
339 struct smb_hdr *smallbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct msghdr smb_msg;
341 struct kvec iov;
342 struct socket *csocket = server->ssocket;
343 struct list_head *tmp;
344 struct cifsSesInfo *ses;
345 struct task_struct *task_to_wake = NULL;
346 struct mid_q_entry *mid_entry;
Steve French70ca7342005-09-22 16:32:06 -0700347 char temp;
Steve French4b18f2a2008-04-29 00:06:05 +0000348 bool isLargeBuf = false;
349 bool isMultiRsp;
Steve Frenche4eb2952005-04-28 22:41:09 -0700350 int reconnect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 current->flags |= PF_MEMALLOC;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700353 cFYI(1, ("Demultiplex PID: %d", task_pid_nr(current)));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400354
355 length = atomic_inc_return(&tcpSesAllocCount);
356 if (length > 1)
Steve French26f57362007-08-30 22:09:15 +0000357 mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
358 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700360 set_freezable();
Jeff Layton469ee612008-10-16 18:46:39 +0000361 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700362 if (try_to_freeze())
363 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700364 if (bigbuf == NULL) {
365 bigbuf = cifs_buf_get();
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000366 if (!bigbuf) {
367 cERROR(1, ("No memory for large SMB response"));
Steve Frenchb8643e12005-04-28 22:41:07 -0700368 msleep(3000);
369 /* retry will check if exiting */
370 continue;
371 }
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000372 } else if (isLargeBuf) {
373 /* we are reusing a dirty large buf, clear its start */
Steve French26f57362007-08-30 22:09:15 +0000374 memset(bigbuf, 0, sizeof(struct smb_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Steve Frenchb8643e12005-04-28 22:41:07 -0700376
377 if (smallbuf == NULL) {
378 smallbuf = cifs_small_buf_get();
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000379 if (!smallbuf) {
380 cERROR(1, ("No memory for SMB response"));
Steve Frenchb8643e12005-04-28 22:41:07 -0700381 msleep(1000);
382 /* retry will check if exiting */
383 continue;
384 }
385 /* beginning of smb buffer is cleared in our buf_get */
386 } else /* if existing small buf clear beginning */
Steve French26f57362007-08-30 22:09:15 +0000387 memset(smallbuf, 0, sizeof(struct smb_hdr));
Steve Frenchb8643e12005-04-28 22:41:07 -0700388
Steve French4b18f2a2008-04-29 00:06:05 +0000389 isLargeBuf = false;
390 isMultiRsp = false;
Steve Frenchb8643e12005-04-28 22:41:07 -0700391 smb_buffer = smallbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 iov.iov_base = smb_buffer;
393 iov.iov_len = 4;
394 smb_msg.msg_control = NULL;
395 smb_msg.msg_controllen = 0;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000396 pdu_length = 4; /* enough to get RFC1001 header */
397incomplete_rcv:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 length =
399 kernel_recvmsg(csocket, &smb_msg,
Steve Frenchf01d5e12007-08-30 21:13:31 +0000400 &iov, 1, pdu_length, 0 /* BB other flags? */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Jeff Layton469ee612008-10-16 18:46:39 +0000402 if (server->tcpStatus == CifsExiting) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 break;
404 } else if (server->tcpStatus == CifsNeedReconnect) {
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000405 cFYI(1, ("Reconnect after server stopped responding"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 cifs_reconnect(server);
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000407 cFYI(1, ("call to reconnect done"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 csocket = server->ssocket;
409 continue;
410 } else if ((length == -ERESTARTSYS) || (length == -EAGAIN)) {
Steve Frenchb8643e12005-04-28 22:41:07 -0700411 msleep(1); /* minimum sleep to prevent looping
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 allowing socket to clear and app threads to set
413 tcpStatus CifsNeedReconnect if server hung */
Steve Frenchc18c7322007-10-17 18:01:11 +0000414 if (pdu_length < 4)
415 goto incomplete_rcv;
416 else
417 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 } else if (length <= 0) {
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000419 if (server->tcpStatus == CifsNew) {
420 cFYI(1, ("tcp session abend after SMBnegprot"));
Steve French09d1db52005-04-28 22:41:08 -0700421 /* some servers kill the TCP session rather than
422 returning an SMB negprot error, in which
423 case reconnecting here is not going to help,
424 and so simply return error to mount */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 break;
426 }
Pavel Machek0fd1ffe2006-06-13 21:31:39 +0000427 if (!try_to_freeze() && (length == -EINTR)) {
Steve French467a8f82007-06-27 22:41:32 +0000428 cFYI(1, ("cifsd thread killed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 break;
430 }
Steve French467a8f82007-06-27 22:41:32 +0000431 cFYI(1, ("Reconnect after unexpected peek error %d",
Steve French57337e42005-04-28 22:41:10 -0700432 length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 cifs_reconnect(server);
434 csocket = server->ssocket;
435 wake_up(&server->response_q);
436 continue;
Petr Tesarik2a974682007-11-20 02:24:08 +0000437 } else if (length < pdu_length) {
438 cFYI(1, ("requested %d bytes but only got %d bytes",
439 pdu_length, length));
Steve Frenchf01d5e12007-08-30 21:13:31 +0000440 pdu_length -= length;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000441 msleep(1);
442 goto incomplete_rcv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
Steve French67010fb2005-04-28 22:41:09 -0700444
Steve French70ca7342005-09-22 16:32:06 -0700445 /* The right amount was read from socket - 4 bytes */
446 /* so we can now interpret the length field */
Steve French46810cb2005-04-28 22:41:09 -0700447
Steve French70ca7342005-09-22 16:32:06 -0700448 /* the first byte big endian of the length field,
449 is actually not part of the length but the type
450 with the most common, zero, as regular data */
451 temp = *((char *) smb_buffer);
452
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000453 /* Note that FC 1001 length is big endian on the wire,
Steve French70ca7342005-09-22 16:32:06 -0700454 but we convert it here so it is always manipulated
455 as host byte order */
Harvey Harrison5ca33c62008-07-23 17:45:58 -0700456 pdu_length = be32_to_cpu((__force __be32)smb_buffer->smb_buf_length);
Steve French70ca7342005-09-22 16:32:06 -0700457 smb_buffer->smb_buf_length = pdu_length;
Steve French46810cb2005-04-28 22:41:09 -0700458
Steve French467a8f82007-06-27 22:41:32 +0000459 cFYI(1, ("rfc1002 length 0x%x", pdu_length+4));
Steve French70ca7342005-09-22 16:32:06 -0700460
461 if (temp == (char) RFC1002_SESSION_KEEP_ALIVE) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000462 continue;
Steve French70ca7342005-09-22 16:32:06 -0700463 } else if (temp == (char)RFC1002_POSITIVE_SESSION_RESPONSE) {
Steve French467a8f82007-06-27 22:41:32 +0000464 cFYI(1, ("Good RFC 1002 session rsp"));
Steve Frenche4eb2952005-04-28 22:41:09 -0700465 continue;
Steve French70ca7342005-09-22 16:32:06 -0700466 } else if (temp == (char)RFC1002_NEGATIVE_SESSION_RESPONSE) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000467 /* we get this from Windows 98 instead of
Steve French46810cb2005-04-28 22:41:09 -0700468 an error on SMB negprot response */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000469 cFYI(1, ("Negative RFC1002 Session Response Error 0x%x)",
Steve French70ca7342005-09-22 16:32:06 -0700470 pdu_length));
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000471 if (server->tcpStatus == CifsNew) {
472 /* if nack on negprot (rather than
Steve French46810cb2005-04-28 22:41:09 -0700473 ret of smb negprot error) reconnecting
474 not going to help, ret error to mount */
475 break;
476 } else {
477 /* give server a second to
478 clean up before reconnect attempt */
479 msleep(1000);
480 /* always try 445 first on reconnect
481 since we get NACK on some if we ever
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000482 connected to port 139 (the NACK is
Steve French46810cb2005-04-28 22:41:09 -0700483 since we do not begin with RFC1001
484 session initialize frame) */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000485 server->addr.sockAddr.sin_port =
Steve French46810cb2005-04-28 22:41:09 -0700486 htons(CIFS_PORT);
487 cifs_reconnect(server);
488 csocket = server->ssocket;
489 wake_up(&server->response_q);
490 continue;
491 }
Steve French70ca7342005-09-22 16:32:06 -0700492 } else if (temp != (char) 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000493 cERROR(1, ("Unknown RFC 1002 frame"));
Steve French70ca7342005-09-22 16:32:06 -0700494 cifs_dump_mem(" Received Data: ", (char *)smb_buffer,
495 length);
Steve French46810cb2005-04-28 22:41:09 -0700496 cifs_reconnect(server);
497 csocket = server->ssocket;
498 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700499 }
500
501 /* else we have an SMB response */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000502 if ((pdu_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) ||
Steve French26f57362007-08-30 22:09:15 +0000503 (pdu_length < sizeof(struct smb_hdr) - 1 - 4)) {
Steve Frenche4eb2952005-04-28 22:41:09 -0700504 cERROR(1, ("Invalid size SMB length %d pdu_length %d",
Steve French46810cb2005-04-28 22:41:09 -0700505 length, pdu_length+4));
Steve Frenche4eb2952005-04-28 22:41:09 -0700506 cifs_reconnect(server);
507 csocket = server->ssocket;
508 wake_up(&server->response_q);
509 continue;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000510 }
Steve Frenche4eb2952005-04-28 22:41:09 -0700511
512 /* else length ok */
513 reconnect = 0;
514
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000515 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
Steve French4b18f2a2008-04-29 00:06:05 +0000516 isLargeBuf = true;
Steve Frenche4eb2952005-04-28 22:41:09 -0700517 memcpy(bigbuf, smallbuf, 4);
518 smb_buffer = bigbuf;
519 }
520 length = 0;
521 iov.iov_base = 4 + (char *)smb_buffer;
522 iov.iov_len = pdu_length;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000523 for (total_read = 0; total_read < pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -0700524 total_read += length) {
525 length = kernel_recvmsg(csocket, &smb_msg, &iov, 1,
526 pdu_length - total_read, 0);
Jeff Layton469ee612008-10-16 18:46:39 +0000527 if ((server->tcpStatus == CifsExiting) ||
Steve Frenche4eb2952005-04-28 22:41:09 -0700528 (length == -EINTR)) {
529 /* then will exit */
530 reconnect = 2;
531 break;
532 } else if (server->tcpStatus == CifsNeedReconnect) {
Steve French46810cb2005-04-28 22:41:09 -0700533 cifs_reconnect(server);
534 csocket = server->ssocket;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000535 /* Reconnect wakes up rspns q */
Steve Frenche4eb2952005-04-28 22:41:09 -0700536 /* Now we will reread sock */
537 reconnect = 1;
538 break;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000539 } else if ((length == -ERESTARTSYS) ||
Steve Frenche4eb2952005-04-28 22:41:09 -0700540 (length == -EAGAIN)) {
541 msleep(1); /* minimum sleep to prevent looping,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000542 allowing socket to clear and app
Steve Frenche4eb2952005-04-28 22:41:09 -0700543 threads to set tcpStatus
544 CifsNeedReconnect if server hung*/
Steve Frenchc18c7322007-10-17 18:01:11 +0000545 length = 0;
Steve French46810cb2005-04-28 22:41:09 -0700546 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700547 } else if (length <= 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000548 cERROR(1, ("Received no data, expecting %d",
Steve Frenche4eb2952005-04-28 22:41:09 -0700549 pdu_length - total_read));
550 cifs_reconnect(server);
551 csocket = server->ssocket;
552 reconnect = 1;
553 break;
Steve French46810cb2005-04-28 22:41:09 -0700554 }
555 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000556 if (reconnect == 2)
Steve Frenche4eb2952005-04-28 22:41:09 -0700557 break;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000558 else if (reconnect == 1)
Steve Frenche4eb2952005-04-28 22:41:09 -0700559 continue;
560
561 length += 4; /* account for rfc1002 hdr */
Steve French50c2f752007-07-13 00:33:32 +0000562
Steve Frenche4eb2952005-04-28 22:41:09 -0700563
564 dump_smb(smb_buffer, length);
Steve French184ed212006-02-24 06:15:11 +0000565 if (checkSMB(smb_buffer, smb_buffer->Mid, total_read+4)) {
Steve Frenchb387eae2005-10-10 14:21:15 -0700566 cifs_dump_mem("Bad SMB: ", smb_buffer, 48);
Steve Frenche4eb2952005-04-28 22:41:09 -0700567 continue;
568 }
569
570
571 task_to_wake = NULL;
572 spin_lock(&GlobalMid_Lock);
573 list_for_each(tmp, &server->pending_mid_q) {
574 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
575
Steve French50c2f752007-07-13 00:33:32 +0000576 if ((mid_entry->mid == smb_buffer->Mid) &&
Steve Frenche4eb2952005-04-28 22:41:09 -0700577 (mid_entry->midState == MID_REQUEST_SUBMITTED) &&
578 (mid_entry->command == smb_buffer->Command)) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000579 if (check2ndT2(smb_buffer,server->maxBuf) > 0) {
Steve Frenche4eb2952005-04-28 22:41:09 -0700580 /* We have a multipart transact2 resp */
Steve French4b18f2a2008-04-29 00:06:05 +0000581 isMultiRsp = true;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000582 if (mid_entry->resp_buf) {
Steve Frenche4eb2952005-04-28 22:41:09 -0700583 /* merge response - fix up 1st*/
Steve French50c2f752007-07-13 00:33:32 +0000584 if (coalesce_t2(smb_buffer,
Steve Frenche4eb2952005-04-28 22:41:09 -0700585 mid_entry->resp_buf)) {
Steve French4b18f2a2008-04-29 00:06:05 +0000586 mid_entry->multiRsp =
587 true;
Steve Frenche4eb2952005-04-28 22:41:09 -0700588 break;
589 } else {
590 /* all parts received */
Steve French4b18f2a2008-04-29 00:06:05 +0000591 mid_entry->multiEnd =
592 true;
Steve French50c2f752007-07-13 00:33:32 +0000593 goto multi_t2_fnd;
Steve Frenche4eb2952005-04-28 22:41:09 -0700594 }
595 } else {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000596 if (!isLargeBuf) {
Steve Frenche4eb2952005-04-28 22:41:09 -0700597 cERROR(1,("1st trans2 resp needs bigbuf"));
598 /* BB maybe we can fix this up, switch
Steve French50c2f752007-07-13 00:33:32 +0000599 to already allocated large buffer? */
Steve Frenche4eb2952005-04-28 22:41:09 -0700600 } else {
Steve Frenchcd634992005-04-28 22:41:10 -0700601 /* Have first buffer */
Steve Frenche4eb2952005-04-28 22:41:09 -0700602 mid_entry->resp_buf =
603 smb_buffer;
Steve French4b18f2a2008-04-29 00:06:05 +0000604 mid_entry->largeBuf =
605 true;
Steve Frenche4eb2952005-04-28 22:41:09 -0700606 bigbuf = NULL;
607 }
608 }
609 break;
Steve French50c2f752007-07-13 00:33:32 +0000610 }
Steve Frenche4eb2952005-04-28 22:41:09 -0700611 mid_entry->resp_buf = smb_buffer;
Steve French4b18f2a2008-04-29 00:06:05 +0000612 mid_entry->largeBuf = isLargeBuf;
Steve Frenche4eb2952005-04-28 22:41:09 -0700613multi_t2_fnd:
614 task_to_wake = mid_entry->tsk;
615 mid_entry->midState = MID_RESPONSE_RECEIVED;
Steve French1047abc2005-10-11 19:58:06 -0700616#ifdef CONFIG_CIFS_STATS2
617 mid_entry->when_received = jiffies;
618#endif
Steve French3a5ff612006-07-14 22:37:11 +0000619 /* so we do not time out requests to server
620 which is still responding (since server could
621 be busy but not dead) */
622 server->lstrp = jiffies;
Steve Frenche4eb2952005-04-28 22:41:09 -0700623 break;
624 }
625 }
626 spin_unlock(&GlobalMid_Lock);
627 if (task_to_wake) {
Steve Frenchcd634992005-04-28 22:41:10 -0700628 /* Was previous buf put in mpx struct for multi-rsp? */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000629 if (!isMultiRsp) {
Steve Frenchcd634992005-04-28 22:41:10 -0700630 /* smb buffer will be freed by user thread */
Steve French26f57362007-08-30 22:09:15 +0000631 if (isLargeBuf)
Steve Frenchcd634992005-04-28 22:41:10 -0700632 bigbuf = NULL;
Steve French26f57362007-08-30 22:09:15 +0000633 else
Steve Frenchcd634992005-04-28 22:41:10 -0700634 smallbuf = NULL;
635 }
Steve Frenche4eb2952005-04-28 22:41:09 -0700636 wake_up_process(task_to_wake);
Steve French4b18f2a2008-04-29 00:06:05 +0000637 } else if (!is_valid_oplock_break(smb_buffer, server) &&
638 !isMultiRsp) {
Steve French50c2f752007-07-13 00:33:32 +0000639 cERROR(1, ("No task to wake, unknown frame received! "
640 "NumMids %d", midCount.counter));
641 cifs_dump_mem("Received Data is: ", (char *)smb_buffer,
Steve French70ca7342005-09-22 16:32:06 -0700642 sizeof(struct smb_hdr));
Steve French39798772006-05-31 22:40:51 +0000643#ifdef CONFIG_CIFS_DEBUG2
644 cifs_dump_detail(smb_buffer);
645 cifs_dump_mids(server);
646#endif /* CIFS_DEBUG2 */
Steve French50c2f752007-07-13 00:33:32 +0000647
Steve Frenche4eb2952005-04-28 22:41:09 -0700648 }
649 } /* end while !EXITING */
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 spin_lock(&GlobalMid_Lock);
652 server->tcpStatus = CifsExiting;
Steve Frenche691b9d2008-05-11 15:53:33 +0000653 spin_unlock(&GlobalMid_Lock);
Steve Frenchdbdbb872008-06-10 21:21:56 +0000654 wake_up_all(&server->response_q);
Steve Frenche691b9d2008-05-11 15:53:33 +0000655
Steve French31ca3bc2005-04-28 22:41:11 -0700656 /* check if we have blocked requests that need to free */
657 /* Note that cifs_max_pending is normally 50, but
658 can be set at module install time to as little as two */
Steve Frenche691b9d2008-05-11 15:53:33 +0000659 spin_lock(&GlobalMid_Lock);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000660 if (atomic_read(&server->inFlight) >= cifs_max_pending)
Steve French31ca3bc2005-04-28 22:41:11 -0700661 atomic_set(&server->inFlight, cifs_max_pending - 1);
662 /* We do not want to set the max_pending too low or we
663 could end up with the counter going negative */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 spin_unlock(&GlobalMid_Lock);
Steve French50c2f752007-07-13 00:33:32 +0000665 /* Although there should not be any requests blocked on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 this queue it can not hurt to be paranoid and try to wake up requests
Steve French09d1db52005-04-28 22:41:08 -0700667 that may haven been blocked when more than 50 at time were on the wire
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 to the same server - they now will see the session is in exit state
669 and get out of SendReceive. */
670 wake_up_all(&server->request_q);
671 /* give those requests time to exit */
Steve Frenchb8643e12005-04-28 22:41:07 -0700672 msleep(125);
Steve French50c2f752007-07-13 00:33:32 +0000673
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000674 if (server->ssocket) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 sock_release(csocket);
676 server->ssocket = NULL;
677 }
Steve Frenchb8643e12005-04-28 22:41:07 -0700678 /* buffer usuallly freed in free_mid - need to free it here on exit */
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +0000679 cifs_buf_release(bigbuf);
680 if (smallbuf) /* no sense logging a debug message if NULL */
Steve Frenchb8643e12005-04-28 22:41:07 -0700681 cifs_small_buf_release(smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 read_lock(&GlobalSMBSeslock);
684 if (list_empty(&server->pending_mid_q)) {
Steve French09d1db52005-04-28 22:41:08 -0700685 /* loop through server session structures attached to this and
686 mark them dead */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 list_for_each(tmp, &GlobalSMBSessionList) {
688 ses =
689 list_entry(tmp, struct cifsSesInfo,
690 cifsSessionList);
691 if (ses->server == server) {
692 ses->status = CifsExiting;
693 ses->server = NULL;
694 }
695 }
696 read_unlock(&GlobalSMBSeslock);
697 } else {
Steve French31ca3bc2005-04-28 22:41:11 -0700698 /* although we can not zero the server struct pointer yet,
699 since there are active requests which may depnd on them,
700 mark the corresponding SMB sessions as exiting too */
701 list_for_each(tmp, &GlobalSMBSessionList) {
702 ses = list_entry(tmp, struct cifsSesInfo,
703 cifsSessionList);
Steve French26f57362007-08-30 22:09:15 +0000704 if (ses->server == server)
Steve French31ca3bc2005-04-28 22:41:11 -0700705 ses->status = CifsExiting;
Steve French31ca3bc2005-04-28 22:41:11 -0700706 }
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 spin_lock(&GlobalMid_Lock);
709 list_for_each(tmp, &server->pending_mid_q) {
710 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
711 if (mid_entry->midState == MID_REQUEST_SUBMITTED) {
Steve French50c2f752007-07-13 00:33:32 +0000712 cFYI(1, ("Clearing Mid 0x%x - waking up ",
713 mid_entry->mid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 task_to_wake = mid_entry->tsk;
Steve French26f57362007-08-30 22:09:15 +0000715 if (task_to_wake)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 wake_up_process(task_to_wake);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 }
719 spin_unlock(&GlobalMid_Lock);
720 read_unlock(&GlobalSMBSeslock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* 1/8th of sec is more than enough time for them to exit */
Steve Frenchb8643e12005-04-28 22:41:07 -0700722 msleep(125);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
Steve Frenchf1914012005-08-18 09:37:34 -0700725 if (!list_empty(&server->pending_mid_q)) {
Steve French50c2f752007-07-13 00:33:32 +0000726 /* mpx threads have not exited yet give them
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 at least the smb send timeout time for long ops */
Steve French31ca3bc2005-04-28 22:41:11 -0700728 /* due to delays on oplock break requests, we need
729 to wait at least 45 seconds before giving up
730 on a request getting a response and going ahead
731 and killing cifsd */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 cFYI(1, ("Wait for exit from demultiplex thread"));
Steve French31ca3bc2005-04-28 22:41:11 -0700733 msleep(46000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /* if threads still have not exited they are probably never
735 coming home not much else we can do but free the memory */
736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Steve French31ca3bc2005-04-28 22:41:11 -0700738 /* last chance to mark ses pointers invalid
739 if there are any pointing to this (e.g
Steve French50c2f752007-07-13 00:33:32 +0000740 if a crazy root user tried to kill cifsd
Steve French31ca3bc2005-04-28 22:41:11 -0700741 kernel thread explicitly this might happen) */
Jeff Layton93d0ec82008-08-02 08:00:48 -0400742 write_lock(&GlobalSMBSeslock);
Steve French31ca3bc2005-04-28 22:41:11 -0700743 list_for_each(tmp, &GlobalSMBSessionList) {
744 ses = list_entry(tmp, struct cifsSesInfo,
745 cifsSessionList);
Steve French26f57362007-08-30 22:09:15 +0000746 if (ses->server == server)
Steve French31ca3bc2005-04-28 22:41:11 -0700747 ses->server = NULL;
Steve French31ca3bc2005-04-28 22:41:11 -0700748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 write_unlock(&GlobalSMBSeslock);
Steve French31ca3bc2005-04-28 22:41:11 -0700750
Jeff Laytonc359cf32007-11-16 22:22:06 +0000751 kfree(server->hostname);
Steve French31ca3bc2005-04-28 22:41:11 -0700752 kfree(server);
Jeff Layton93d0ec82008-08-02 08:00:48 -0400753
754 length = atomic_dec_return(&tcpSesAllocCount);
Steve French26f57362007-08-30 22:09:15 +0000755 if (length > 0)
756 mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
757 GFP_KERNEL);
Steve French50c2f752007-07-13 00:33:32 +0000758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return 0;
760}
761
Jeff Laytonc359cf32007-11-16 22:22:06 +0000762/* extract the host portion of the UNC string */
763static char *
764extract_hostname(const char *unc)
765{
766 const char *src;
767 char *dst, *delim;
768 unsigned int len;
769
770 /* skip double chars at beginning of string */
771 /* BB: check validity of these bytes? */
772 src = unc + 2;
773
774 /* delimiter between hostname and sharename is always '\\' now */
775 delim = strchr(src, '\\');
776 if (!delim)
777 return ERR_PTR(-EINVAL);
778
779 len = delim - src;
780 dst = kmalloc((len + 1), GFP_KERNEL);
781 if (dst == NULL)
782 return ERR_PTR(-ENOMEM);
783
784 memcpy(dst, src, len);
785 dst[len] = '\0';
786
787 return dst;
788}
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790static int
Steve French50c2f752007-07-13 00:33:32 +0000791cifs_parse_mount_options(char *options, const char *devname,
792 struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 char *value;
795 char *data;
796 unsigned int temp_len, i, j;
797 char separator[2];
798
799 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +0000800 separator[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Linus Torvalds12e36b22006-10-13 08:09:29 -0700802 if (Local_System_Name[0] != 0)
Steve French50c2f752007-07-13 00:33:32 +0000803 memcpy(vol->source_rfc1001_name, Local_System_Name, 15);
Steve French2cd646a2006-09-28 19:43:08 +0000804 else {
Linus Torvalds12e36b22006-10-13 08:09:29 -0700805 char *nodename = utsname()->nodename;
Steve French50c2f752007-07-13 00:33:32 +0000806 int n = strnlen(nodename, 15);
807 memset(vol->source_rfc1001_name, 0x20, 15);
808 for (i = 0; i < n; i++) {
Steve French2cd646a2006-09-28 19:43:08 +0000809 /* does not have to be perfect mapping since field is
810 informational, only used for servers that do not support
811 port 445 and it can be overridden at mount time */
Linus Torvalds12e36b22006-10-13 08:09:29 -0700812 vol->source_rfc1001_name[i] = toupper(nodename[i]);
Steve French2cd646a2006-09-28 19:43:08 +0000813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815 vol->source_rfc1001_name[15] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -0700816 /* null target name indicates to use *SMBSERVR default called name
817 if we end up sending RFC1001 session initialize */
818 vol->target_rfc1001_name[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 vol->linux_uid = current->uid; /* current->euid instead? */
820 vol->linux_gid = current->gid;
821 vol->dir_mode = S_IRWXUGO;
822 /* 2767 perms indicate mandatory locking support */
Steve French7505e052007-11-01 18:03:01 +0000823 vol->file_mode = (S_IRWXUGO | S_ISGID) & (~S_IXGRP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Steve French4b18f2a2008-04-29 00:06:05 +0000826 vol->rw = true;
Jeremy Allisonac670552005-06-22 17:26:35 -0700827 /* default is always to request posix paths. */
828 vol->posix_paths = 1;
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (!options)
831 return 1;
832
Steve French50c2f752007-07-13 00:33:32 +0000833 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000834 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 separator[0] = options[4];
836 options += 5;
837 } else {
Steve French467a8f82007-06-27 22:41:32 +0000838 cFYI(1, ("Null separator not allowed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840 }
Steve French50c2f752007-07-13 00:33:32 +0000841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 while ((data = strsep(&options, separator)) != NULL) {
843 if (!*data)
844 continue;
845 if ((value = strchr(data, '=')) != NULL)
846 *value++ = '\0';
847
Steve French50c2f752007-07-13 00:33:32 +0000848 /* Have to parse this before we parse for "user" */
849 if (strnicmp(data, "user_xattr", 10) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 vol->no_xattr = 0;
Steve French50c2f752007-07-13 00:33:32 +0000851 } else if (strnicmp(data, "nouser_xattr", 12) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 vol->no_xattr = 1;
853 } else if (strnicmp(data, "user", 4) == 0) {
Steve French4b952a92006-10-30 21:46:13 +0000854 if (!value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 printk(KERN_WARNING
856 "CIFS: invalid or missing username\n");
857 return 1; /* needs_arg; */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000858 } else if (!*value) {
Steve French4b952a92006-10-30 21:46:13 +0000859 /* null user, ie anonymous, authentication */
860 vol->nullauth = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862 if (strnlen(value, 200) < 200) {
863 vol->username = value;
864 } else {
865 printk(KERN_WARNING "CIFS: username too long\n");
866 return 1;
867 }
868 } else if (strnicmp(data, "pass", 4) == 0) {
869 if (!value) {
870 vol->password = NULL;
871 continue;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000872 } else if (value[0] == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 /* check if string begins with double comma
874 since that would mean the password really
875 does start with a comma, and would not
876 indicate an empty string */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000877 if (value[1] != separator[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 vol->password = NULL;
879 continue;
880 }
881 }
882 temp_len = strlen(value);
883 /* removed password length check, NTLM passwords
884 can be arbitrarily long */
885
Steve French50c2f752007-07-13 00:33:32 +0000886 /* if comma in password, the string will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 prematurely null terminated. Commas in password are
888 specified across the cifs mount interface by a double
889 comma ie ,, and a comma used as in other cases ie ','
890 as a parameter delimiter/separator is single and due
891 to the strsep above is temporarily zeroed. */
892
893 /* NB: password legally can have multiple commas and
894 the only illegal character in a password is null */
895
Steve French50c2f752007-07-13 00:33:32 +0000896 if ((value[temp_len] == 0) &&
Steve French09d1db52005-04-28 22:41:08 -0700897 (value[temp_len+1] == separator[0])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /* reinsert comma */
899 value[temp_len] = separator[0];
Steve French50c2f752007-07-13 00:33:32 +0000900 temp_len += 2; /* move after second comma */
901 while (value[temp_len] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (value[temp_len] == separator[0]) {
Steve French50c2f752007-07-13 00:33:32 +0000903 if (value[temp_len+1] ==
Steve French09d1db52005-04-28 22:41:08 -0700904 separator[0]) {
905 /* skip second comma */
906 temp_len++;
Steve French50c2f752007-07-13 00:33:32 +0000907 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 /* single comma indicating start
909 of next parm */
910 break;
911 }
912 }
913 temp_len++;
914 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000915 if (value[temp_len] == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 options = NULL;
917 } else {
918 value[temp_len] = 0;
919 /* point option to start of next parm */
920 options = value + temp_len + 1;
921 }
Steve French50c2f752007-07-13 00:33:32 +0000922 /* go from value to value + temp_len condensing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 double commas to singles. Note that this ends up
924 allocating a few bytes too many, which is ok */
Pekka Enberge915fc42005-09-06 15:18:35 -0700925 vol->password = kzalloc(temp_len, GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000926 if (vol->password == NULL) {
Steve French50c2f752007-07-13 00:33:32 +0000927 printk(KERN_WARNING "CIFS: no memory "
928 "for password\n");
Steve French433dc242005-04-28 22:41:08 -0700929 return 1;
930 }
Steve French50c2f752007-07-13 00:33:32 +0000931 for (i = 0, j = 0; i < temp_len; i++, j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 vol->password[j] = value[i];
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000933 if (value[i] == separator[0]
Steve French09d1db52005-04-28 22:41:08 -0700934 && value[i+1] == separator[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /* skip second comma */
936 i++;
937 }
938 }
939 vol->password[j] = 0;
940 } else {
Pekka Enberge915fc42005-09-06 15:18:35 -0700941 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000942 if (vol->password == NULL) {
Steve French50c2f752007-07-13 00:33:32 +0000943 printk(KERN_WARNING "CIFS: no memory "
944 "for password\n");
Steve French433dc242005-04-28 22:41:08 -0700945 return 1;
946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 strcpy(vol->password, value);
948 }
949 } else if (strnicmp(data, "ip", 2) == 0) {
950 if (!value || !*value) {
951 vol->UNCip = NULL;
952 } else if (strnlen(value, 35) < 35) {
953 vol->UNCip = value;
954 } else {
Steve French50c2f752007-07-13 00:33:32 +0000955 printk(KERN_WARNING "CIFS: ip address "
956 "too long\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return 1;
958 }
Steve French50c2f752007-07-13 00:33:32 +0000959 } else if (strnicmp(data, "sec", 3) == 0) {
960 if (!value || !*value) {
961 cERROR(1, ("no security value specified"));
962 continue;
963 } else if (strnicmp(value, "krb5i", 5) == 0) {
964 vol->secFlg |= CIFSSEC_MAY_KRB5 |
Steve French189acaa2006-06-23 02:33:48 +0000965 CIFSSEC_MUST_SIGN;
Steve Frenchbf820672005-12-01 22:32:42 -0800966 } else if (strnicmp(value, "krb5p", 5) == 0) {
Steve French50c2f752007-07-13 00:33:32 +0000967 /* vol->secFlg |= CIFSSEC_MUST_SEAL |
968 CIFSSEC_MAY_KRB5; */
969 cERROR(1, ("Krb5 cifs privacy not supported"));
Steve Frenchbf820672005-12-01 22:32:42 -0800970 return 1;
971 } else if (strnicmp(value, "krb5", 4) == 0) {
Steve French750d1152006-06-27 06:28:30 +0000972 vol->secFlg |= CIFSSEC_MAY_KRB5;
Steve Frenchbf820672005-12-01 22:32:42 -0800973 } else if (strnicmp(value, "ntlmv2i", 7) == 0) {
Steve French750d1152006-06-27 06:28:30 +0000974 vol->secFlg |= CIFSSEC_MAY_NTLMV2 |
Steve French189acaa2006-06-23 02:33:48 +0000975 CIFSSEC_MUST_SIGN;
Steve Frenchbf820672005-12-01 22:32:42 -0800976 } else if (strnicmp(value, "ntlmv2", 6) == 0) {
Steve French750d1152006-06-27 06:28:30 +0000977 vol->secFlg |= CIFSSEC_MAY_NTLMV2;
Steve Frenchbf820672005-12-01 22:32:42 -0800978 } else if (strnicmp(value, "ntlmi", 5) == 0) {
Steve French750d1152006-06-27 06:28:30 +0000979 vol->secFlg |= CIFSSEC_MAY_NTLM |
Steve French189acaa2006-06-23 02:33:48 +0000980 CIFSSEC_MUST_SIGN;
Steve Frenchbf820672005-12-01 22:32:42 -0800981 } else if (strnicmp(value, "ntlm", 4) == 0) {
982 /* ntlm is default so can be turned off too */
Steve French750d1152006-06-27 06:28:30 +0000983 vol->secFlg |= CIFSSEC_MAY_NTLM;
Steve Frenchbf820672005-12-01 22:32:42 -0800984 } else if (strnicmp(value, "nontlm", 6) == 0) {
Steve French189acaa2006-06-23 02:33:48 +0000985 /* BB is there a better way to do this? */
Steve French750d1152006-06-27 06:28:30 +0000986 vol->secFlg |= CIFSSEC_MAY_NTLMV2;
Steve French189acaa2006-06-23 02:33:48 +0000987#ifdef CONFIG_CIFS_WEAK_PW_HASH
988 } else if (strnicmp(value, "lanman", 6) == 0) {
Steve French50c2f752007-07-13 00:33:32 +0000989 vol->secFlg |= CIFSSEC_MAY_LANMAN;
Steve French189acaa2006-06-23 02:33:48 +0000990#endif
Steve Frenchbf820672005-12-01 22:32:42 -0800991 } else if (strnicmp(value, "none", 4) == 0) {
Steve French189acaa2006-06-23 02:33:48 +0000992 vol->nullauth = 1;
Steve French50c2f752007-07-13 00:33:32 +0000993 } else {
994 cERROR(1, ("bad security option: %s", value));
995 return 1;
996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 } else if ((strnicmp(data, "unc", 3) == 0)
998 || (strnicmp(data, "target", 6) == 0)
999 || (strnicmp(data, "path", 4) == 0)) {
1000 if (!value || !*value) {
Steve French50c2f752007-07-13 00:33:32 +00001001 printk(KERN_WARNING "CIFS: invalid path to "
1002 "network resource\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return 1; /* needs_arg; */
1004 }
1005 if ((temp_len = strnlen(value, 300)) < 300) {
Steve French50c2f752007-07-13 00:33:32 +00001006 vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
Steve French4523cc32007-04-30 20:13:06 +00001007 if (vol->UNC == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return 1;
Steve French50c2f752007-07-13 00:33:32 +00001009 strcpy(vol->UNC, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (strncmp(vol->UNC, "//", 2) == 0) {
1011 vol->UNC[0] = '\\';
1012 vol->UNC[1] = '\\';
Steve French50c2f752007-07-13 00:33:32 +00001013 } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 printk(KERN_WARNING
Steve French50c2f752007-07-13 00:33:32 +00001015 "CIFS: UNC Path does not begin "
1016 "with // or \\\\ \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return 1;
1018 }
1019 } else {
1020 printk(KERN_WARNING "CIFS: UNC name too long\n");
1021 return 1;
1022 }
1023 } else if ((strnicmp(data, "domain", 3) == 0)
1024 || (strnicmp(data, "workgroup", 5) == 0)) {
1025 if (!value || !*value) {
1026 printk(KERN_WARNING "CIFS: invalid domain name\n");
1027 return 1; /* needs_arg; */
1028 }
1029 /* BB are there cases in which a comma can be valid in
1030 a domain name and need special handling? */
Steve French39798772006-05-31 22:40:51 +00001031 if (strnlen(value, 256) < 256) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 vol->domainname = value;
1033 cFYI(1, ("Domain name set"));
1034 } else {
Steve French50c2f752007-07-13 00:33:32 +00001035 printk(KERN_WARNING "CIFS: domain name too "
1036 "long\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return 1;
1038 }
Steve French50c2f752007-07-13 00:33:32 +00001039 } else if (strnicmp(data, "prefixpath", 10) == 0) {
1040 if (!value || !*value) {
1041 printk(KERN_WARNING
1042 "CIFS: invalid path prefix\n");
1043 return 1; /* needs_argument */
1044 }
1045 if ((temp_len = strnlen(value, 1024)) < 1024) {
Steve French4523cc32007-04-30 20:13:06 +00001046 if (value[0] != '/')
Steve French2fe87f02006-09-21 07:02:52 +00001047 temp_len++; /* missing leading slash */
Steve French50c2f752007-07-13 00:33:32 +00001048 vol->prepath = kmalloc(temp_len+1, GFP_KERNEL);
1049 if (vol->prepath == NULL)
1050 return 1;
Steve French4523cc32007-04-30 20:13:06 +00001051 if (value[0] != '/') {
Steve French2fe87f02006-09-21 07:02:52 +00001052 vol->prepath[0] = '/';
Steve French50c2f752007-07-13 00:33:32 +00001053 strcpy(vol->prepath+1, value);
Steve French2fe87f02006-09-21 07:02:52 +00001054 } else
Steve French50c2f752007-07-13 00:33:32 +00001055 strcpy(vol->prepath, value);
1056 cFYI(1, ("prefix path %s", vol->prepath));
1057 } else {
1058 printk(KERN_WARNING "CIFS: prefix too long\n");
1059 return 1;
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 } else if (strnicmp(data, "iocharset", 9) == 0) {
1062 if (!value || !*value) {
Steve French63135e02007-07-17 17:34:02 +00001063 printk(KERN_WARNING "CIFS: invalid iocharset "
1064 "specified\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return 1; /* needs_arg; */
1066 }
1067 if (strnlen(value, 65) < 65) {
Steve French50c2f752007-07-13 00:33:32 +00001068 if (strnicmp(value, "default", 7))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 vol->iocharset = value;
Steve French50c2f752007-07-13 00:33:32 +00001070 /* if iocharset not set then load_nls_default
1071 is used by caller */
1072 cFYI(1, ("iocharset set to %s", value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 } else {
Steve French63135e02007-07-17 17:34:02 +00001074 printk(KERN_WARNING "CIFS: iocharset name "
1075 "too long.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return 1;
1077 }
1078 } else if (strnicmp(data, "uid", 3) == 0) {
1079 if (value && *value) {
1080 vol->linux_uid =
1081 simple_strtoul(value, &value, 0);
Steve French4523cc32007-04-30 20:13:06 +00001082 vol->override_uid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084 } else if (strnicmp(data, "gid", 3) == 0) {
1085 if (value && *value) {
1086 vol->linux_gid =
1087 simple_strtoul(value, &value, 0);
Steve French4523cc32007-04-30 20:13:06 +00001088 vol->override_gid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090 } else if (strnicmp(data, "file_mode", 4) == 0) {
1091 if (value && *value) {
1092 vol->file_mode =
1093 simple_strtoul(value, &value, 0);
1094 }
1095 } else if (strnicmp(data, "dir_mode", 4) == 0) {
1096 if (value && *value) {
1097 vol->dir_mode =
1098 simple_strtoul(value, &value, 0);
1099 }
1100 } else if (strnicmp(data, "dirmode", 4) == 0) {
1101 if (value && *value) {
1102 vol->dir_mode =
1103 simple_strtoul(value, &value, 0);
1104 }
1105 } else if (strnicmp(data, "port", 4) == 0) {
1106 if (value && *value) {
1107 vol->port =
1108 simple_strtoul(value, &value, 0);
1109 }
1110 } else if (strnicmp(data, "rsize", 5) == 0) {
1111 if (value && *value) {
1112 vol->rsize =
1113 simple_strtoul(value, &value, 0);
1114 }
1115 } else if (strnicmp(data, "wsize", 5) == 0) {
1116 if (value && *value) {
1117 vol->wsize =
1118 simple_strtoul(value, &value, 0);
1119 }
1120 } else if (strnicmp(data, "sockopt", 5) == 0) {
1121 if (value && *value) {
1122 vol->sockopt =
1123 simple_strtoul(value, &value, 0);
1124 }
1125 } else if (strnicmp(data, "netbiosname", 4) == 0) {
1126 if (!value || !*value || (*value == ' ')) {
Steve French63135e02007-07-17 17:34:02 +00001127 cFYI(1, ("invalid (empty) netbiosname"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 } else {
Steve French50c2f752007-07-13 00:33:32 +00001129 memset(vol->source_rfc1001_name, 0x20, 15);
1130 for (i = 0; i < 15; i++) {
1131 /* BB are there cases in which a comma can be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 valid in this workstation netbios name (and need
1133 special handling)? */
1134
1135 /* We do not uppercase netbiosname for user */
Steve French50c2f752007-07-13 00:33:32 +00001136 if (value[i] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 break;
Steve French50c2f752007-07-13 00:33:32 +00001138 else
1139 vol->source_rfc1001_name[i] =
1140 value[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142 /* The string has 16th byte zero still from
1143 set at top of the function */
Steve French50c2f752007-07-13 00:33:32 +00001144 if ((i == 15) && (value[i] != 0))
1145 printk(KERN_WARNING "CIFS: netbiosname"
1146 " longer than 15 truncated.\n");
Steve Frencha10faeb22005-08-22 21:38:31 -07001147 }
1148 } else if (strnicmp(data, "servern", 7) == 0) {
1149 /* servernetbiosname specified override *SMBSERVER */
1150 if (!value || !*value || (*value == ' ')) {
Steve French467a8f82007-06-27 22:41:32 +00001151 cFYI(1, ("empty server netbiosname specified"));
Steve Frencha10faeb22005-08-22 21:38:31 -07001152 } else {
1153 /* last byte, type, is 0x20 for servr type */
Steve French50c2f752007-07-13 00:33:32 +00001154 memset(vol->target_rfc1001_name, 0x20, 16);
Steve Frencha10faeb22005-08-22 21:38:31 -07001155
Steve French50c2f752007-07-13 00:33:32 +00001156 for (i = 0; i < 15; i++) {
Steve Frencha10faeb22005-08-22 21:38:31 -07001157 /* BB are there cases in which a comma can be
Steve French50c2f752007-07-13 00:33:32 +00001158 valid in this workstation netbios name
1159 (and need special handling)? */
Steve Frencha10faeb22005-08-22 21:38:31 -07001160
Steve French50c2f752007-07-13 00:33:32 +00001161 /* user or mount helper must uppercase
1162 the netbiosname */
1163 if (value[i] == 0)
Steve Frencha10faeb22005-08-22 21:38:31 -07001164 break;
1165 else
Steve French50c2f752007-07-13 00:33:32 +00001166 vol->target_rfc1001_name[i] =
1167 value[i];
Steve Frencha10faeb22005-08-22 21:38:31 -07001168 }
1169 /* The string has 16th byte zero still from
1170 set at top of the function */
Steve French50c2f752007-07-13 00:33:32 +00001171 if ((i == 15) && (value[i] != 0))
1172 printk(KERN_WARNING "CIFS: server net"
1173 "biosname longer than 15 truncated.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175 } else if (strnicmp(data, "credentials", 4) == 0) {
1176 /* ignore */
1177 } else if (strnicmp(data, "version", 3) == 0) {
1178 /* ignore */
Steve French50c2f752007-07-13 00:33:32 +00001179 } else if (strnicmp(data, "guest", 5) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /* ignore */
1181 } else if (strnicmp(data, "rw", 2) == 0) {
Steve French4b18f2a2008-04-29 00:06:05 +00001182 vol->rw = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 } else if ((strnicmp(data, "suid", 4) == 0) ||
1184 (strnicmp(data, "nosuid", 6) == 0) ||
1185 (strnicmp(data, "exec", 4) == 0) ||
1186 (strnicmp(data, "noexec", 6) == 0) ||
1187 (strnicmp(data, "nodev", 5) == 0) ||
1188 (strnicmp(data, "noauto", 6) == 0) ||
1189 (strnicmp(data, "dev", 3) == 0)) {
1190 /* The mount tool or mount.cifs helper (if present)
Steve French50c2f752007-07-13 00:33:32 +00001191 uses these opts to set flags, and the flags are read
1192 by the kernel vfs layer before we get here (ie
1193 before read super) so there is no point trying to
1194 parse these options again and set anything and it
1195 is ok to just ignore them */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 continue;
1197 } else if (strnicmp(data, "ro", 2) == 0) {
Steve French4b18f2a2008-04-29 00:06:05 +00001198 vol->rw = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 } else if (strnicmp(data, "hard", 4) == 0) {
1200 vol->retry = 1;
1201 } else if (strnicmp(data, "soft", 4) == 0) {
1202 vol->retry = 0;
1203 } else if (strnicmp(data, "perm", 4) == 0) {
1204 vol->noperm = 0;
1205 } else if (strnicmp(data, "noperm", 6) == 0) {
1206 vol->noperm = 1;
Steve French6a0b4822005-04-28 22:41:05 -07001207 } else if (strnicmp(data, "mapchars", 8) == 0) {
1208 vol->remap = 1;
1209 } else if (strnicmp(data, "nomapchars", 10) == 0) {
1210 vol->remap = 0;
Steve French50c2f752007-07-13 00:33:32 +00001211 } else if (strnicmp(data, "sfu", 3) == 0) {
1212 vol->sfu_emul = 1;
1213 } else if (strnicmp(data, "nosfu", 5) == 0) {
1214 vol->sfu_emul = 0;
Steve French2c1b8612008-10-16 18:35:21 +00001215 } else if (strnicmp(data, "nodfs", 5) == 0) {
1216 vol->nodfs = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001217 } else if (strnicmp(data, "posixpaths", 10) == 0) {
1218 vol->posix_paths = 1;
1219 } else if (strnicmp(data, "noposixpaths", 12) == 0) {
1220 vol->posix_paths = 0;
Steve Frenchc18c8422007-07-18 23:21:09 +00001221 } else if (strnicmp(data, "nounix", 6) == 0) {
1222 vol->no_linux_ext = 1;
1223 } else if (strnicmp(data, "nolinux", 7) == 0) {
1224 vol->no_linux_ext = 1;
Steve French50c2f752007-07-13 00:33:32 +00001225 } else if ((strnicmp(data, "nocase", 6) == 0) ||
Steve Frencha10faeb22005-08-22 21:38:31 -07001226 (strnicmp(data, "ignorecase", 10) == 0)) {
Steve French50c2f752007-07-13 00:33:32 +00001227 vol->nocase = 1;
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001228 } else if (strnicmp(data, "brl", 3) == 0) {
1229 vol->nobrl = 0;
Steve French50c2f752007-07-13 00:33:32 +00001230 } else if ((strnicmp(data, "nobrl", 5) == 0) ||
Steve French1c955182005-08-30 20:58:07 -07001231 (strnicmp(data, "nolock", 6) == 0)) {
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001232 vol->nobrl = 1;
Steve Frenchd3485d32005-08-19 11:04:29 -07001233 /* turn off mandatory locking in mode
1234 if remote locking is turned off since the
1235 local vfs will do advisory */
Steve French50c2f752007-07-13 00:33:32 +00001236 if (vol->file_mode ==
1237 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001238 vol->file_mode = S_IALLUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 } else if (strnicmp(data, "setuids", 7) == 0) {
1240 vol->setuids = 1;
1241 } else if (strnicmp(data, "nosetuids", 9) == 0) {
1242 vol->setuids = 0;
Jeff Laytond0a9c072008-05-12 22:23:49 +00001243 } else if (strnicmp(data, "dynperm", 7) == 0) {
1244 vol->dynperm = true;
1245 } else if (strnicmp(data, "nodynperm", 9) == 0) {
1246 vol->dynperm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 } else if (strnicmp(data, "nohard", 6) == 0) {
1248 vol->retry = 0;
1249 } else if (strnicmp(data, "nosoft", 6) == 0) {
1250 vol->retry = 1;
1251 } else if (strnicmp(data, "nointr", 6) == 0) {
1252 vol->intr = 0;
1253 } else if (strnicmp(data, "intr", 4) == 0) {
1254 vol->intr = 1;
Steve French50c2f752007-07-13 00:33:32 +00001255 } else if (strnicmp(data, "serverino", 7) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 vol->server_ino = 1;
Steve French50c2f752007-07-13 00:33:32 +00001257 } else if (strnicmp(data, "noserverino", 9) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 vol->server_ino = 0;
Steve French50c2f752007-07-13 00:33:32 +00001259 } else if (strnicmp(data, "cifsacl", 7) == 0) {
Steve French0a4b92c2006-01-12 15:44:21 -08001260 vol->cifs_acl = 1;
1261 } else if (strnicmp(data, "nocifsacl", 9) == 0) {
1262 vol->cifs_acl = 0;
Steve French50c2f752007-07-13 00:33:32 +00001263 } else if (strnicmp(data, "acl", 3) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 vol->no_psx_acl = 0;
Steve French50c2f752007-07-13 00:33:32 +00001265 } else if (strnicmp(data, "noacl", 5) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 vol->no_psx_acl = 1;
Steve French50c2f752007-07-13 00:33:32 +00001267 } else if (strnicmp(data, "sign", 4) == 0) {
Steve French750d1152006-06-27 06:28:30 +00001268 vol->secFlg |= CIFSSEC_MUST_SIGN;
Steve French95b1cb92008-05-15 16:44:38 +00001269 } else if (strnicmp(data, "seal", 4) == 0) {
1270 /* we do not do the following in secFlags because seal
1271 is a per tree connection (mount) not a per socket
1272 or per-smb connection option in the protocol */
1273 /* vol->secFlg |= CIFSSEC_MUST_SEAL; */
1274 vol->seal = 1;
Steve French50c2f752007-07-13 00:33:32 +00001275 } else if (strnicmp(data, "direct", 6) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 vol->direct_io = 1;
Steve French50c2f752007-07-13 00:33:32 +00001277 } else if (strnicmp(data, "forcedirectio", 13) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 vol->direct_io = 1;
Steve French50c2f752007-07-13 00:33:32 +00001279 } else if (strnicmp(data, "in6_addr", 8) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (!value || !*value) {
1281 vol->in6_addr = NULL;
1282 } else if (strnlen(value, 49) == 48) {
1283 vol->in6_addr = value;
1284 } else {
Steve French50c2f752007-07-13 00:33:32 +00001285 printk(KERN_WARNING "CIFS: ip v6 address not "
1286 "48 characters long\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return 1;
1288 }
1289 } else if (strnicmp(data, "noac", 4) == 0) {
Steve French50c2f752007-07-13 00:33:32 +00001290 printk(KERN_WARNING "CIFS: Mount option noac not "
1291 "supported. Instead set "
1292 "/proc/fs/cifs/LookupCacheEnabled to 0\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 } else
Steve French50c2f752007-07-13 00:33:32 +00001294 printk(KERN_WARNING "CIFS: Unknown mount option %s\n",
1295 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297 if (vol->UNC == NULL) {
Steve French4523cc32007-04-30 20:13:06 +00001298 if (devname == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00001299 printk(KERN_WARNING "CIFS: Missing UNC name for mount "
1300 "target\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return 1;
1302 }
1303 if ((temp_len = strnlen(devname, 300)) < 300) {
Steve French50c2f752007-07-13 00:33:32 +00001304 vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
Steve French4523cc32007-04-30 20:13:06 +00001305 if (vol->UNC == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return 1;
Steve French50c2f752007-07-13 00:33:32 +00001307 strcpy(vol->UNC, devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (strncmp(vol->UNC, "//", 2) == 0) {
1309 vol->UNC[0] = '\\';
1310 vol->UNC[1] = '\\';
1311 } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
Steve French50c2f752007-07-13 00:33:32 +00001312 printk(KERN_WARNING "CIFS: UNC Path does not "
1313 "begin with // or \\\\ \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return 1;
1315 }
Igor Mammedov7c5e6282008-05-08 20:48:42 +00001316 value = strpbrk(vol->UNC+2, "/\\");
1317 if (value)
1318 *value = '\\';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 } else {
1320 printk(KERN_WARNING "CIFS: UNC name too long\n");
1321 return 1;
1322 }
1323 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001324 if (vol->UNCip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 vol->UNCip = &vol->UNC[2];
1326
1327 return 0;
1328}
1329
1330static struct cifsSesInfo *
Steve French50c2f752007-07-13 00:33:32 +00001331cifs_find_tcp_session(struct in_addr *target_ip_addr,
Cyrill Gorcunov1b20d672008-05-09 18:17:21 +00001332 struct in6_addr *target_ip6_addr,
1333 char *userName, struct TCP_Server_Info **psrvTcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 struct list_head *tmp;
1336 struct cifsSesInfo *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Cyrill Gorcunov1b20d672008-05-09 18:17:21 +00001338 *psrvTcp = NULL;
1339
1340 read_lock(&GlobalSMBSeslock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 list_for_each(tmp, &GlobalSMBSessionList) {
1342 ses = list_entry(tmp, struct cifsSesInfo, cifsSessionList);
Cyrill Gorcunov1b20d672008-05-09 18:17:21 +00001343 if (!ses->server)
1344 continue;
Steve French50c2f752007-07-13 00:33:32 +00001345
Cyrill Gorcunov1b20d672008-05-09 18:17:21 +00001346 if (target_ip_addr &&
1347 ses->server->addr.sockAddr.sin_addr.s_addr != target_ip_addr->s_addr)
1348 continue;
1349 else if (target_ip6_addr &&
1350 memcmp(&ses->server->addr.sockAddr6.sin6_addr,
1351 target_ip6_addr, sizeof(*target_ip6_addr)))
1352 continue;
Jeff Layton02eadef2008-05-09 21:26:11 +00001353 /* BB lock server and tcp session; increment use count here?? */
Steve French50c2f752007-07-13 00:33:32 +00001354
Cyrill Gorcunov1b20d672008-05-09 18:17:21 +00001355 /* found a match on the TCP session */
1356 *psrvTcp = ses->server;
1357
1358 /* BB check if reconnection needed */
1359 if (strncmp(ses->userName, userName, MAX_USERNAME_SIZE) == 0) {
1360 read_unlock(&GlobalSMBSeslock);
1361 /* Found exact match on both TCP and
1362 SMB sessions */
1363 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 /* else tcp and smb sessions need reconnection */
1366 }
1367 read_unlock(&GlobalSMBSeslock);
Cyrill Gorcunov1b20d672008-05-09 18:17:21 +00001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return NULL;
1370}
1371
1372static struct cifsTconInfo *
1373find_unc(__be32 new_target_ip_addr, char *uncName, char *userName)
1374{
1375 struct list_head *tmp;
1376 struct cifsTconInfo *tcon;
Steve Frenchdea570e02008-05-06 22:05:51 +00001377 __be32 old_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 read_lock(&GlobalSMBSeslock);
Steve Frenchdea570e02008-05-06 22:05:51 +00001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 list_for_each(tmp, &GlobalTreeConnectionList) {
Steve Frenche466e482006-08-15 13:07:18 +00001382 cFYI(1, ("Next tcon"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 tcon = list_entry(tmp, struct cifsTconInfo, cifsConnectionList);
Steve Frenchdea570e02008-05-06 22:05:51 +00001384 if (!tcon->ses || !tcon->ses->server)
1385 continue;
1386
1387 old_ip = tcon->ses->server->addr.sockAddr.sin_addr.s_addr;
1388 cFYI(1, ("old ip addr: %x == new ip %x ?",
1389 old_ip, new_target_ip_addr));
1390
1391 if (old_ip != new_target_ip_addr)
1392 continue;
1393
1394 /* BB lock tcon, server, tcp session and increment use count? */
1395 /* found a match on the TCP session */
1396 /* BB check if reconnection needed */
1397 cFYI(1, ("IP match, old UNC: %s new: %s",
1398 tcon->treeName, uncName));
1399
1400 if (strncmp(tcon->treeName, uncName, MAX_TREE_SIZE))
1401 continue;
1402
1403 cFYI(1, ("and old usr: %s new: %s",
1404 tcon->treeName, uncName));
1405
1406 if (strncmp(tcon->ses->userName, userName, MAX_USERNAME_SIZE))
1407 continue;
1408
1409 /* matched smb session (user name) */
1410 read_unlock(&GlobalSMBSeslock);
1411 return tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
Steve Frenchdea570e02008-05-06 22:05:51 +00001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 read_unlock(&GlobalSMBSeslock);
1415 return NULL;
1416}
1417
1418int
Steve French50c2f752007-07-13 00:33:32 +00001419get_dfs_path(int xid, struct cifsSesInfo *pSesInfo, const char *old_path,
1420 const struct nls_table *nls_codepage, unsigned int *pnum_referrals,
Steve French366781c2008-01-25 10:12:41 +00001421 struct dfs_info3_param **preferrals, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 char *temp_unc;
1424 int rc = 0;
1425
1426 *pnum_referrals = 0;
Steve French366781c2008-01-25 10:12:41 +00001427 *preferrals = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 if (pSesInfo->ipc_tid == 0) {
1430 temp_unc = kmalloc(2 /* for slashes */ +
Steve French50c2f752007-07-13 00:33:32 +00001431 strnlen(pSesInfo->serverName,
1432 SERVER_NAME_LEN_WITH_NULL * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 + 1 + 4 /* slash IPC$ */ + 2,
1434 GFP_KERNEL);
1435 if (temp_unc == NULL)
1436 return -ENOMEM;
1437 temp_unc[0] = '\\';
1438 temp_unc[1] = '\\';
1439 strcpy(temp_unc + 2, pSesInfo->serverName);
1440 strcpy(temp_unc + 2 + strlen(pSesInfo->serverName), "\\IPC$");
1441 rc = CIFSTCon(xid, pSesInfo, temp_unc, NULL, nls_codepage);
1442 cFYI(1,
Steve French50c2f752007-07-13 00:33:32 +00001443 ("CIFS Tcon rc = %d ipc_tid = %d", rc, pSesInfo->ipc_tid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 kfree(temp_unc);
1445 }
1446 if (rc == 0)
Steve Frenchc2cf07d2008-05-15 06:20:02 +00001447 rc = CIFSGetDFSRefer(xid, pSesInfo, old_path, preferrals,
Steve French737b7582005-04-28 22:41:06 -07001448 pnum_referrals, nls_codepage, remap);
Steve French366781c2008-01-25 10:12:41 +00001449 /* BB map targetUNCs to dfs_info3 structures, here or
1450 in CIFSGetDFSRefer BB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 return rc;
1453}
1454
Jeff Layton09e50d52008-07-23 10:11:19 -04001455#ifdef CONFIG_DEBUG_LOCK_ALLOC
1456static struct lock_class_key cifs_key[2];
1457static struct lock_class_key cifs_slock_key[2];
1458
1459static inline void
1460cifs_reclassify_socket4(struct socket *sock)
1461{
1462 struct sock *sk = sock->sk;
1463 BUG_ON(sock_owned_by_user(sk));
1464 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
1465 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
1466}
1467
1468static inline void
1469cifs_reclassify_socket6(struct socket *sock)
1470{
1471 struct sock *sk = sock->sk;
1472 BUG_ON(sock_owned_by_user(sk));
1473 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
1474 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
1475}
1476#else
1477static inline void
1478cifs_reclassify_socket4(struct socket *sock)
1479{
1480}
1481
1482static inline void
1483cifs_reclassify_socket6(struct socket *sock)
1484{
1485}
1486#endif
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00001489static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Steve French50c2f752007-07-13 00:33:32 +00001491 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Steve French50c2f752007-07-13 00:33:32 +00001493 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 /* mask a nibble at a time and encode */
1495 target[j] = 'A' + (0x0F & (source[i] >> 4));
1496 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00001497 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499
1500}
1501
1502
1503static int
Steve French50c2f752007-07-13 00:33:32 +00001504ipv4_connect(struct sockaddr_in *psin_server, struct socket **csocket,
1505 char *netbios_name, char *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 int rc = 0;
1508 int connected = 0;
1509 __be16 orig_port = 0;
1510
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001511 if (*csocket == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00001512 rc = sock_create_kern(PF_INET, SOCK_STREAM,
1513 IPPROTO_TCP, csocket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (rc < 0) {
Steve French50c2f752007-07-13 00:33:32 +00001515 cERROR(1, ("Error %d creating socket", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 *csocket = NULL;
1517 return rc;
1518 } else {
1519 /* BB other socket options to set KEEPALIVE, NODELAY? */
Steve French467a8f82007-06-27 22:41:32 +00001520 cFYI(1, ("Socket created"));
Steve French50c2f752007-07-13 00:33:32 +00001521 (*csocket)->sk->sk_allocation = GFP_NOFS;
Jeff Layton09e50d52008-07-23 10:11:19 -04001522 cifs_reclassify_socket4(*csocket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524 }
1525
1526 psin_server->sin_family = AF_INET;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001527 if (psin_server->sin_port) { /* user overrode default port */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 rc = (*csocket)->ops->connect(*csocket,
1529 (struct sockaddr *) psin_server,
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00001530 sizeof(struct sockaddr_in), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (rc >= 0)
1532 connected = 1;
Steve French50c2f752007-07-13 00:33:32 +00001533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001535 if (!connected) {
Steve French50c2f752007-07-13 00:33:32 +00001536 /* save original port so we can retry user specified port
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 later if fall back ports fail this time */
1538 orig_port = psin_server->sin_port;
1539
1540 /* do not retry on the same port we just failed on */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001541 if (psin_server->sin_port != htons(CIFS_PORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 psin_server->sin_port = htons(CIFS_PORT);
1543
1544 rc = (*csocket)->ops->connect(*csocket,
1545 (struct sockaddr *) psin_server,
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00001546 sizeof(struct sockaddr_in), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (rc >= 0)
1548 connected = 1;
1549 }
1550 }
1551 if (!connected) {
1552 psin_server->sin_port = htons(RFC1001_PORT);
1553 rc = (*csocket)->ops->connect(*csocket, (struct sockaddr *)
Steve French50c2f752007-07-13 00:33:32 +00001554 psin_server,
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00001555 sizeof(struct sockaddr_in), 0);
Steve French50c2f752007-07-13 00:33:32 +00001556 if (rc >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 connected = 1;
1558 }
1559
1560 /* give up here - unless we want to retry on different
1561 protocol families some day */
1562 if (!connected) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001563 if (orig_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 psin_server->sin_port = orig_port;
Steve French50c2f752007-07-13 00:33:32 +00001565 cFYI(1, ("Error %d connecting to server via ipv4", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 sock_release(*csocket);
1567 *csocket = NULL;
1568 return rc;
1569 }
Steve French50c2f752007-07-13 00:33:32 +00001570 /* Eventually check for other socket options to change from
1571 the default. sock_setsockopt not used because it expects
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 user space buffer */
Steve French50c2f752007-07-13 00:33:32 +00001573 cFYI(1, ("sndbuf %d rcvbuf %d rcvtimeo 0x%lx",
1574 (*csocket)->sk->sk_sndbuf,
Steve Frenchb387eae2005-10-10 14:21:15 -07001575 (*csocket)->sk->sk_rcvbuf, (*csocket)->sk->sk_rcvtimeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 (*csocket)->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchb387eae2005-10-10 14:21:15 -07001577 /* make the bufsizes depend on wsize/rsize and max requests */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001578 if ((*csocket)->sk->sk_sndbuf < (200 * 1024))
Steve Frenchb387eae2005-10-10 14:21:15 -07001579 (*csocket)->sk->sk_sndbuf = 200 * 1024;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001580 if ((*csocket)->sk->sk_rcvbuf < (140 * 1024))
Steve Frenchb387eae2005-10-10 14:21:15 -07001581 (*csocket)->sk->sk_rcvbuf = 140 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 /* send RFC1001 sessinit */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001584 if (psin_server->sin_port == htons(RFC1001_PORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /* some servers require RFC1001 sessinit before sending
Steve French50c2f752007-07-13 00:33:32 +00001586 negprot - BB check reconnection in case where second
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 sessinit is sent but no second negprot */
Steve French50c2f752007-07-13 00:33:32 +00001588 struct rfc1002_session_packet *ses_init_buf;
1589 struct smb_hdr *smb_buf;
1590 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
1591 GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001592 if (ses_init_buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 ses_init_buf->trailer.session_req.called_len = 32;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001594 if (target_name && (target_name[0] != 0)) {
Steve Frencha10faeb22005-08-22 21:38:31 -07001595 rfc1002mangle(ses_init_buf->trailer.session_req.called_name,
1596 target_name, 16);
1597 } else {
1598 rfc1002mangle(ses_init_buf->trailer.session_req.called_name,
Steve French50c2f752007-07-13 00:33:32 +00001599 DEFAULT_CIFS_CALLED_NAME, 16);
Steve Frencha10faeb22005-08-22 21:38:31 -07001600 }
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 ses_init_buf->trailer.session_req.calling_len = 32;
1603 /* calling name ends in null (byte 16) from old smb
1604 convention. */
Steve French50c2f752007-07-13 00:33:32 +00001605 if (netbios_name && (netbios_name[0] != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 rfc1002mangle(ses_init_buf->trailer.session_req.calling_name,
Steve French50c2f752007-07-13 00:33:32 +00001607 netbios_name, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 } else {
1609 rfc1002mangle(ses_init_buf->trailer.session_req.calling_name,
Steve French50c2f752007-07-13 00:33:32 +00001610 "LINUX_CIFS_CLNT", 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612 ses_init_buf->trailer.session_req.scope1 = 0;
1613 ses_init_buf->trailer.session_req.scope2 = 0;
1614 smb_buf = (struct smb_hdr *)ses_init_buf;
1615 /* sizeof RFC1002_SESSION_REQUEST with no scope */
1616 smb_buf->smb_buf_length = 0x81000044;
1617 rc = smb_send(*csocket, smb_buf, 0x44,
1618 (struct sockaddr *)psin_server);
1619 kfree(ses_init_buf);
Steve French50c2f752007-07-13 00:33:32 +00001620 msleep(1); /* RFC1001 layer in at least one server
Steve French083d3a22006-03-03 09:53:36 +00001621 requires very short break before negprot
1622 presumably because not expecting negprot
1623 to follow so fast. This is a simple
Steve French50c2f752007-07-13 00:33:32 +00001624 solution that works without
Steve French083d3a22006-03-03 09:53:36 +00001625 complicating the code and causes no
1626 significant slowing down on mount
1627 for everyone else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
Steve French50c2f752007-07-13 00:33:32 +00001629 /* else the negprot may still work without this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 even though malloc failed */
Steve French50c2f752007-07-13 00:33:32 +00001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Steve French50c2f752007-07-13 00:33:32 +00001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return rc;
1635}
1636
1637static int
1638ipv6_connect(struct sockaddr_in6 *psin_server, struct socket **csocket)
1639{
1640 int rc = 0;
1641 int connected = 0;
1642 __be16 orig_port = 0;
1643
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001644 if (*csocket == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00001645 rc = sock_create_kern(PF_INET6, SOCK_STREAM,
1646 IPPROTO_TCP, csocket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (rc < 0) {
Steve French50c2f752007-07-13 00:33:32 +00001648 cERROR(1, ("Error %d creating ipv6 socket", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 *csocket = NULL;
1650 return rc;
1651 } else {
1652 /* BB other socket options to set KEEPALIVE, NODELAY? */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001653 cFYI(1, ("ipv6 Socket created"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 (*csocket)->sk->sk_allocation = GFP_NOFS;
Jeff Layton09e50d52008-07-23 10:11:19 -04001655 cifs_reclassify_socket6(*csocket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
1657 }
1658
1659 psin_server->sin6_family = AF_INET6;
1660
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001661 if (psin_server->sin6_port) { /* user overrode default port */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 rc = (*csocket)->ops->connect(*csocket,
1663 (struct sockaddr *) psin_server,
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00001664 sizeof(struct sockaddr_in6), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (rc >= 0)
1666 connected = 1;
Steve French50c2f752007-07-13 00:33:32 +00001667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001669 if (!connected) {
Steve French50c2f752007-07-13 00:33:32 +00001670 /* save original port so we can retry user specified port
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 later if fall back ports fail this time */
1672
1673 orig_port = psin_server->sin6_port;
1674 /* do not retry on the same port we just failed on */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001675 if (psin_server->sin6_port != htons(CIFS_PORT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 psin_server->sin6_port = htons(CIFS_PORT);
1677
1678 rc = (*csocket)->ops->connect(*csocket,
1679 (struct sockaddr *) psin_server,
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00001680 sizeof(struct sockaddr_in6), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (rc >= 0)
1682 connected = 1;
1683 }
1684 }
1685 if (!connected) {
1686 psin_server->sin6_port = htons(RFC1001_PORT);
1687 rc = (*csocket)->ops->connect(*csocket, (struct sockaddr *)
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00001688 psin_server, sizeof(struct sockaddr_in6), 0);
Steve French50c2f752007-07-13 00:33:32 +00001689 if (rc >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 connected = 1;
1691 }
1692
1693 /* give up here - unless we want to retry on different
1694 protocol families some day */
1695 if (!connected) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001696 if (orig_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 psin_server->sin6_port = orig_port;
Steve French50c2f752007-07-13 00:33:32 +00001698 cFYI(1, ("Error %d connecting to server via ipv6", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 sock_release(*csocket);
1700 *csocket = NULL;
1701 return rc;
1702 }
Steve French50c2f752007-07-13 00:33:32 +00001703 /* Eventually check for other socket options to change from
1704 the default. sock_setsockopt not used because it expects
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 user space buffer */
1706 (*csocket)->sk->sk_rcvtimeo = 7 * HZ;
Steve French50c2f752007-07-13 00:33:32 +00001707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return rc;
1709}
1710
Steve French50c2f752007-07-13 00:33:32 +00001711void reset_cifs_unix_caps(int xid, struct cifsTconInfo *tcon,
1712 struct super_block *sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00001713{
1714 /* if we are reconnecting then should we check to see if
1715 * any requested capabilities changed locally e.g. via
1716 * remount but we can not do much about it here
1717 * if they have (even if we could detect it by the following)
1718 * Perhaps we could add a backpointer to array of sb from tcon
1719 * or if we change to make all sb to same share the same
1720 * sb as NFS - then we only have one backpointer to sb.
1721 * What if we wanted to mount the server share twice once with
1722 * and once without posixacls or posix paths? */
1723 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00001724
Steve Frenchc18c8422007-07-18 23:21:09 +00001725 if (vol_info && vol_info->no_linux_ext) {
1726 tcon->fsUnixInfo.Capability = 0;
1727 tcon->unix_ext = 0; /* Unix Extensions disabled */
1728 cFYI(1, ("Linux protocol extensions disabled"));
1729 return;
1730 } else if (vol_info)
1731 tcon->unix_ext = 1; /* Unix Extensions supported */
1732
1733 if (tcon->unix_ext == 0) {
1734 cFYI(1, ("Unix extensions disabled so not set on reconnect"));
1735 return;
1736 }
Steve French50c2f752007-07-13 00:33:32 +00001737
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001738 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00001739 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00001740
Steve French8af18972007-02-14 04:42:51 +00001741 /* check for reconnect case in which we do not
1742 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001743 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00001744 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00001745 originally at mount time */
1746 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
1747 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00001748 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
1749 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
1750 cERROR(1, ("POSIXPATH support change"));
Steve French8af18972007-02-14 04:42:51 +00001751 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00001752 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
1753 cERROR(1, ("possible reconnect error"));
1754 cERROR(1,
1755 ("server disabled POSIX path support"));
1756 }
Steve French8af18972007-02-14 04:42:51 +00001757 }
Steve French50c2f752007-07-13 00:33:32 +00001758
Steve French8af18972007-02-14 04:42:51 +00001759 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00001760 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00001761 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00001762 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001763 cFYI(1, ("negotiated posix acl support"));
1764 if (sb)
Steve French8af18972007-02-14 04:42:51 +00001765 sb->s_flags |= MS_POSIXACL;
1766 }
1767
Steve French75865f8c2007-06-24 18:30:48 +00001768 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00001769 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00001770 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001771 cFYI(1, ("negotiate posix pathnames"));
Steve French75865f8c2007-06-24 18:30:48 +00001772 if (sb)
Steve French50c2f752007-07-13 00:33:32 +00001773 CIFS_SB(sb)->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00001774 CIFS_MOUNT_POSIX_PATHS;
1775 }
Steve French50c2f752007-07-13 00:33:32 +00001776
Steve French984acfe2007-04-26 16:42:50 +00001777 /* We might be setting the path sep back to a different
1778 form if we are reconnecting and the server switched its
Steve French50c2f752007-07-13 00:33:32 +00001779 posix path capability for this share */
Steve French75865f8c2007-06-24 18:30:48 +00001780 if (sb && (CIFS_SB(sb)->prepathlen > 0))
Steve French984acfe2007-04-26 16:42:50 +00001781 CIFS_SB(sb)->prepath[0] = CIFS_DIR_SEP(CIFS_SB(sb));
Steve French75865f8c2007-06-24 18:30:48 +00001782
1783 if (sb && (CIFS_SB(sb)->rsize > 127 * 1024)) {
1784 if ((cap & CIFS_UNIX_LARGE_READ_CAP) == 0) {
1785 CIFS_SB(sb)->rsize = 127 * 1024;
Steve French90c81e02008-02-12 20:32:36 +00001786 cFYI(DBG2,
1787 ("larger reads not supported by srv"));
Steve French75865f8c2007-06-24 18:30:48 +00001788 }
1789 }
Steve French50c2f752007-07-13 00:33:32 +00001790
1791
1792 cFYI(1, ("Negotiate caps 0x%x", (int)cap));
Steve French8af18972007-02-14 04:42:51 +00001793#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00001794 if (cap & CIFS_UNIX_FCNTL_CAP)
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001795 cFYI(1, ("FCNTL cap"));
Steve French75865f8c2007-06-24 18:30:48 +00001796 if (cap & CIFS_UNIX_EXTATTR_CAP)
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001797 cFYI(1, ("EXTATTR cap"));
Steve French75865f8c2007-06-24 18:30:48 +00001798 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001799 cFYI(1, ("POSIX path cap"));
Steve French75865f8c2007-06-24 18:30:48 +00001800 if (cap & CIFS_UNIX_XATTR_CAP)
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001801 cFYI(1, ("XATTR cap"));
Steve French75865f8c2007-06-24 18:30:48 +00001802 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001803 cFYI(1, ("POSIX ACL cap"));
Steve French75865f8c2007-06-24 18:30:48 +00001804 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001805 cFYI(1, ("very large read cap"));
Steve French75865f8c2007-06-24 18:30:48 +00001806 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001807 cFYI(1, ("very large write cap"));
Steve French8af18972007-02-14 04:42:51 +00001808#endif /* CIFS_DEBUG2 */
1809 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00001810 if (vol_info == NULL) {
Steve French5a44b312007-09-20 15:16:24 +00001811 cFYI(1, ("resetting capabilities failed"));
Steve French442aa312007-09-24 20:25:46 +00001812 } else
Steve French5a44b312007-09-20 15:16:24 +00001813 cERROR(1, ("Negotiating Unix capabilities "
1814 "with the server failed. Consider "
1815 "mounting with the Unix Extensions\n"
1816 "disabled, if problems are found, "
1817 "by specifying the nounix mount "
Steve French2224f4e2007-09-20 15:37:29 +00001818 "option."));
Steve French5a44b312007-09-20 15:16:24 +00001819
Steve French8af18972007-02-14 04:42:51 +00001820 }
1821 }
1822}
1823
Steve French03a143c2008-02-14 06:38:30 +00001824static void
1825convert_delimiter(char *path, char delim)
1826{
1827 int i;
Steve Frenchc2d68ea2008-02-15 19:20:18 +00001828 char old_delim;
Steve French03a143c2008-02-14 06:38:30 +00001829
1830 if (path == NULL)
1831 return;
1832
Steve French582d21e2008-05-13 04:54:12 +00001833 if (delim == '/')
Steve Frenchc2d68ea2008-02-15 19:20:18 +00001834 old_delim = '\\';
1835 else
1836 old_delim = '/';
1837
Steve French03a143c2008-02-14 06:38:30 +00001838 for (i = 0; path[i] != '\0'; i++) {
Steve Frenchc2d68ea2008-02-15 19:20:18 +00001839 if (path[i] == old_delim)
Steve French03a143c2008-02-14 06:38:30 +00001840 path[i] = delim;
1841 }
1842}
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844int
1845cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
1846 char *mount_data, const char *devname)
1847{
1848 int rc = 0;
1849 int xid;
1850 int address_type = AF_INET;
1851 struct socket *csocket = NULL;
1852 struct sockaddr_in sin_server;
1853 struct sockaddr_in6 sin_server6;
1854 struct smb_vol volume_info;
1855 struct cifsSesInfo *pSesInfo = NULL;
1856 struct cifsSesInfo *existingCifsSes = NULL;
1857 struct cifsTconInfo *tcon = NULL;
1858 struct TCP_Server_Info *srvTcp = NULL;
1859
1860 xid = GetXid();
1861
1862/* cFYI(1, ("Entering cifs_mount. Xid: %d with: %s", xid, mount_data)); */
Steve French50c2f752007-07-13 00:33:32 +00001863
1864 memset(&volume_info, 0, sizeof(struct smb_vol));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (cifs_parse_mount_options(mount_data, devname, &volume_info)) {
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001866 rc = -EINVAL;
1867 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869
Jeff Layton8426c392007-05-05 03:27:49 +00001870 if (volume_info.nullauth) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001871 cFYI(1, ("null user"));
Jeff Layton9b8f5f52007-11-09 23:25:04 +00001872 volume_info.username = "";
Jeff Layton8426c392007-05-05 03:27:49 +00001873 } else if (volume_info.username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 /* BB fixme parse for domain name here */
Steve French467a8f82007-06-27 22:41:32 +00001875 cFYI(1, ("Username: %s", volume_info.username));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 } else {
Steve Frenchbf820672005-12-01 22:32:42 -08001877 cifserror("No username specified");
Steve French50c2f752007-07-13 00:33:32 +00001878 /* In userspace mount helper we can get user name from alternate
1879 locations such as env variables and files on disk */
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001880 rc = -EINVAL;
1881 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883
1884 if (volume_info.UNCip && volume_info.UNC) {
Steve French50c2f752007-07-13 00:33:32 +00001885 rc = cifs_inet_pton(AF_INET, volume_info.UNCip,
1886 &sin_server.sin_addr.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001888 if (rc <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 /* not ipv4 address, try ipv6 */
Steve French50c2f752007-07-13 00:33:32 +00001890 rc = cifs_inet_pton(AF_INET6, volume_info.UNCip,
1891 &sin_server6.sin6_addr.in6_u);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001892 if (rc > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 address_type = AF_INET6;
1894 } else {
1895 address_type = AF_INET;
1896 }
Steve French50c2f752007-07-13 00:33:32 +00001897
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001898 if (rc <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /* we failed translating address */
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001900 rc = -EINVAL;
1901 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903
1904 cFYI(1, ("UNC: %s ip: %s", volume_info.UNC, volume_info.UNCip));
1905 /* success */
1906 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00001907 } else if (volume_info.UNCip) {
1908 /* BB using ip addr as server name to connect to the
1909 DFS root below */
1910 cERROR(1, ("Connecting to DFS root not implemented yet"));
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001911 rc = -EINVAL;
1912 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 } else /* which servers DFS root would we conect to */ {
1914 cERROR(1,
Steve French50c2f752007-07-13 00:33:32 +00001915 ("CIFS mount error: No UNC path (e.g. -o "
1916 "unc=//192.168.1.100/public) specified"));
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001917 rc = -EINVAL;
1918 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920
1921 /* this is needed for ASCII cp to Unicode converts */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001922 if (volume_info.iocharset == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 cifs_sb->local_nls = load_nls_default();
1924 /* load_nls_default can not return null */
1925 } else {
1926 cifs_sb->local_nls = load_nls(volume_info.iocharset);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001927 if (cifs_sb->local_nls == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00001928 cERROR(1, ("CIFS mount error: iocharset %s not found",
1929 volume_info.iocharset));
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001930 rc = -ELIBACC;
1931 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933 }
1934
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001935 if (address_type == AF_INET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 existingCifsSes = cifs_find_tcp_session(&sin_server.sin_addr,
1937 NULL /* no ipv6 addr */,
1938 volume_info.username, &srvTcp);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001939 else if (address_type == AF_INET6) {
1940 cFYI(1, ("looking for ipv6 address"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 existingCifsSes = cifs_find_tcp_session(NULL /* no ipv4 addr */,
1942 &sin_server6.sin6_addr,
1943 volume_info.username, &srvTcp);
Steve French5858ae42007-04-25 11:59:10 +00001944 } else {
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001945 rc = -EINVAL;
1946 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (srvTcp) {
Steve French50c2f752007-07-13 00:33:32 +00001950 cFYI(1, ("Existing tcp session with server found"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 } else { /* create socket */
Steve French4523cc32007-04-30 20:13:06 +00001952 if (volume_info.port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 sin_server.sin_port = htons(volume_info.port);
1954 else
1955 sin_server.sin_port = 0;
Steve French5858ae42007-04-25 11:59:10 +00001956 if (address_type == AF_INET6) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001957 cFYI(1, ("attempting ipv6 connect"));
Steve French5858ae42007-04-25 11:59:10 +00001958 /* BB should we allow ipv6 on port 139? */
1959 /* other OS never observed in Wild doing 139 with v6 */
Steve French50c2f752007-07-13 00:33:32 +00001960 rc = ipv6_connect(&sin_server6, &csocket);
1961 } else
1962 rc = ipv4_connect(&sin_server, &csocket,
Steve Frencha10faeb22005-08-22 21:38:31 -07001963 volume_info.source_rfc1001_name,
1964 volume_info.target_rfc1001_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if (rc < 0) {
Steve French50c2f752007-07-13 00:33:32 +00001966 cERROR(1, ("Error connecting to IPv4 socket. "
1967 "Aborting operation"));
Steve French4523cc32007-04-30 20:13:06 +00001968 if (csocket != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 sock_release(csocket);
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001970 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00001973 srvTcp = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
1974 if (!srvTcp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 rc = -ENOMEM;
1976 sock_release(csocket);
Jeff Layton70fe7dc2007-11-16 22:21:07 +00001977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 } else {
Steve French50c2f752007-07-13 00:33:32 +00001979 memcpy(&srvTcp->addr.sockAddr, &sin_server,
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00001980 sizeof(struct sockaddr_in));
Steve French50c2f752007-07-13 00:33:32 +00001981 atomic_set(&srvTcp->inFlight, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /* BB Add code for ipv6 case too */
1983 srvTcp->ssocket = csocket;
1984 srvTcp->protocolType = IPV4;
Jeff Laytonc359cf32007-11-16 22:22:06 +00001985 srvTcp->hostname = extract_hostname(volume_info.UNC);
1986 if (IS_ERR(srvTcp->hostname)) {
1987 rc = PTR_ERR(srvTcp->hostname);
1988 sock_release(csocket);
1989 goto out;
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 init_waitqueue_head(&srvTcp->response_q);
1992 init_waitqueue_head(&srvTcp->request_q);
1993 INIT_LIST_HEAD(&srvTcp->pending_mid_q);
1994 /* at this point we are the only ones with the pointer
1995 to the struct since the kernel thread not created yet
1996 so no need to spinlock this init of tcpStatus */
1997 srvTcp->tcpStatus = CifsNew;
1998 init_MUTEX(&srvTcp->tcpSem);
Igor Mammedovaaf737a2007-04-03 19:16:43 +00001999 srvTcp->tsk = kthread_run((void *)(void *)cifs_demultiplex_thread, srvTcp, "cifsd");
Steve French8840dee2007-11-16 23:05:52 +00002000 if (IS_ERR(srvTcp->tsk)) {
Igor Mammedovaaf737a2007-04-03 19:16:43 +00002001 rc = PTR_ERR(srvTcp->tsk);
Steve French50c2f752007-07-13 00:33:32 +00002002 cERROR(1, ("error %d create cifsd thread", rc));
Igor Mammedovaaf737a2007-04-03 19:16:43 +00002003 srvTcp->tsk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 sock_release(csocket);
Jeff Laytonc359cf32007-11-16 22:22:06 +00002005 kfree(srvTcp->hostname);
Jeff Layton70fe7dc2007-11-16 22:21:07 +00002006 goto out;
Steve Frenchf1914012005-08-18 09:37:34 -07002007 }
Steve Frenchf1914012005-08-18 09:37:34 -07002008 rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00002009 memcpy(srvTcp->workstation_RFC1001_name,
2010 volume_info.source_rfc1001_name, 16);
2011 memcpy(srvTcp->server_RFC1001_name,
2012 volume_info.target_rfc1001_name, 16);
Steve Frenchad009ac2005-04-28 22:41:05 -07002013 srvTcp->sequence_number = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015 }
2016
2017 if (existingCifsSes) {
2018 pSesInfo = existingCifsSes;
Jeff Layton1d9a8852007-12-31 01:37:11 +00002019 cFYI(1, ("Existing smb sess found (status=%d)",
2020 pSesInfo->status));
Steve French88e7d702008-01-03 17:37:09 +00002021 down(&pSesInfo->sesSem);
Jeff Layton1d9a8852007-12-31 01:37:11 +00002022 if (pSesInfo->status == CifsNeedReconnect) {
2023 cFYI(1, ("Session needs reconnect"));
Jeff Layton1d9a8852007-12-31 01:37:11 +00002024 rc = cifs_setup_session(xid, pSesInfo,
2025 cifs_sb->local_nls);
Jeff Layton1d9a8852007-12-31 01:37:11 +00002026 }
Steve French88e7d702008-01-03 17:37:09 +00002027 up(&pSesInfo->sesSem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 } else if (!rc) {
Steve Frenchbf820672005-12-01 22:32:42 -08002029 cFYI(1, ("Existing smb sess not found"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 pSesInfo = sesInfoAlloc();
2031 if (pSesInfo == NULL)
2032 rc = -ENOMEM;
2033 else {
2034 pSesInfo->server = srvTcp;
2035 sprintf(pSesInfo->serverName, "%u.%u.%u.%u",
2036 NIPQUAD(sin_server.sin_addr.s_addr));
2037 }
2038
Steve French50c2f752007-07-13 00:33:32 +00002039 if (!rc) {
2040 /* volume_info.password freed at unmount */
Jeff Layton70fe7dc2007-11-16 22:21:07 +00002041 if (volume_info.password) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 pSesInfo->password = volume_info.password;
Jeff Layton70fe7dc2007-11-16 22:21:07 +00002043 /* set to NULL to prevent freeing on exit */
2044 volume_info.password = NULL;
2045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (volume_info.username)
2047 strncpy(pSesInfo->userName,
Steve French50c2f752007-07-13 00:33:32 +00002048 volume_info.username,
2049 MAX_USERNAME_SIZE);
Steve French39798772006-05-31 22:40:51 +00002050 if (volume_info.domainname) {
2051 int len = strlen(volume_info.domainname);
Steve French50c2f752007-07-13 00:33:32 +00002052 pSesInfo->domainName =
Steve French39798772006-05-31 22:40:51 +00002053 kmalloc(len + 1, GFP_KERNEL);
Steve French4523cc32007-04-30 20:13:06 +00002054 if (pSesInfo->domainName)
Steve French39798772006-05-31 22:40:51 +00002055 strcpy(pSesInfo->domainName,
2056 volume_info.domainname);
2057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 pSesInfo->linux_uid = volume_info.linux_uid;
Steve French750d1152006-06-27 06:28:30 +00002059 pSesInfo->overrideSecFlg = volume_info.secFlg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 down(&pSesInfo->sesSem);
Steve French189acaa2006-06-23 02:33:48 +00002061 /* BB FIXME need to pass vol->secFlgs BB */
Steve French50c2f752007-07-13 00:33:32 +00002062 rc = cifs_setup_session(xid, pSesInfo,
2063 cifs_sb->local_nls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 up(&pSesInfo->sesSem);
Steve French4523cc32007-04-30 20:13:06 +00002065 if (!rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 atomic_inc(&srvTcp->socketUseCount);
Jeff Layton70fe7dc2007-11-16 22:21:07 +00002067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Steve French50c2f752007-07-13 00:33:32 +00002069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /* search for existing tcon to this server share */
2071 if (!rc) {
Steve French4523cc32007-04-30 20:13:06 +00002072 if (volume_info.rsize > CIFSMaxBufSize) {
Steve French50c2f752007-07-13 00:33:32 +00002073 cERROR(1, ("rsize %d too large, using MaxBufSize",
Steve French0ae0efa2005-10-10 10:57:19 -07002074 volume_info.rsize));
2075 cifs_sb->rsize = CIFSMaxBufSize;
Steve French75865f8c2007-06-24 18:30:48 +00002076 } else if ((volume_info.rsize) &&
2077 (volume_info.rsize <= CIFSMaxBufSize))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 cifs_sb->rsize = volume_info.rsize;
Steve French0ae0efa2005-10-10 10:57:19 -07002079 else /* default */
2080 cifs_sb->rsize = CIFSMaxBufSize;
2081
Steve French4523cc32007-04-30 20:13:06 +00002082 if (volume_info.wsize > PAGEVEC_SIZE * PAGE_CACHE_SIZE) {
Steve French50c2f752007-07-13 00:33:32 +00002083 cERROR(1, ("wsize %d too large, using 4096 instead",
Steve French0ae0efa2005-10-10 10:57:19 -07002084 volume_info.wsize));
2085 cifs_sb->wsize = 4096;
Steve French4523cc32007-04-30 20:13:06 +00002086 } else if (volume_info.wsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 cifs_sb->wsize = volume_info.wsize;
2088 else
Steve French50c2f752007-07-13 00:33:32 +00002089 cifs_sb->wsize =
Steve French1877c9e2006-01-27 18:36:11 -08002090 min_t(const int, PAGEVEC_SIZE * PAGE_CACHE_SIZE,
2091 127*1024);
Steve French17cbbaf2006-01-24 20:26:48 -08002092 /* old default of CIFSMaxBufSize was too small now
Steve French50c2f752007-07-13 00:33:32 +00002093 that SMB Write2 can send multiple pages in kvec.
Steve French17cbbaf2006-01-24 20:26:48 -08002094 RFC1001 does not describe what happens when frame
2095 bigger than 128K is sent so use that as max in
2096 conjunction with 52K kvec constraint on arch with 4K
2097 page size */
2098
Steve French4523cc32007-04-30 20:13:06 +00002099 if (cifs_sb->rsize < 2048) {
Steve French50c2f752007-07-13 00:33:32 +00002100 cifs_sb->rsize = 2048;
Steve French6cec2ae2006-02-22 17:31:52 -06002101 /* Windows ME may prefer this */
Steve French467a8f82007-06-27 22:41:32 +00002102 cFYI(1, ("readsize set to minimum: 2048"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
Steve French2fe87f02006-09-21 07:02:52 +00002104 /* calculate prepath */
2105 cifs_sb->prepath = volume_info.prepath;
Steve French4523cc32007-04-30 20:13:06 +00002106 if (cifs_sb->prepath) {
Steve French2fe87f02006-09-21 07:02:52 +00002107 cifs_sb->prepathlen = strlen(cifs_sb->prepath);
Steve French03a143c2008-02-14 06:38:30 +00002108 /* we can not convert the / to \ in the path
2109 separators in the prefixpath yet because we do not
2110 know (until reset_cifs_unix_caps is called later)
2111 whether POSIX PATH CAP is available. We normalize
2112 the / to \ after reset_cifs_unix_caps is called */
Steve French2fe87f02006-09-21 07:02:52 +00002113 volume_info.prepath = NULL;
Steve French50c2f752007-07-13 00:33:32 +00002114 } else
Steve French2fe87f02006-09-21 07:02:52 +00002115 cifs_sb->prepathlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 cifs_sb->mnt_uid = volume_info.linux_uid;
2117 cifs_sb->mnt_gid = volume_info.linux_gid;
2118 cifs_sb->mnt_file_mode = volume_info.file_mode;
2119 cifs_sb->mnt_dir_mode = volume_info.dir_mode;
Steve French467a8f82007-06-27 22:41:32 +00002120 cFYI(1, ("file mode: 0x%x dir mode: 0x%x",
2121 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Steve French4523cc32007-04-30 20:13:06 +00002123 if (volume_info.noperm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
Steve French4523cc32007-04-30 20:13:06 +00002125 if (volume_info.setuids)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French4523cc32007-04-30 20:13:06 +00002127 if (volume_info.server_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
Steve French4523cc32007-04-30 20:13:06 +00002129 if (volume_info.remap)
Steve French6a0b4822005-04-28 22:41:05 -07002130 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
Steve French4523cc32007-04-30 20:13:06 +00002131 if (volume_info.no_xattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
Steve French4523cc32007-04-30 20:13:06 +00002133 if (volume_info.sfu_emul)
Steve Frenchd7245c22005-07-14 18:25:12 -05002134 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
Steve French4523cc32007-04-30 20:13:06 +00002135 if (volume_info.nobrl)
Steve Frenchc46fa8a2005-08-18 20:49:57 -07002136 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French4523cc32007-04-30 20:13:06 +00002137 if (volume_info.cifs_acl)
Steve French0a4b92c2006-01-12 15:44:21 -08002138 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Steve French4523cc32007-04-30 20:13:06 +00002139 if (volume_info.override_uid)
2140 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
2141 if (volume_info.override_gid)
2142 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
Jeff Laytond0a9c072008-05-12 22:23:49 +00002143 if (volume_info.dynperm)
2144 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Steve French4523cc32007-04-30 20:13:06 +00002145 if (volume_info.direct_io) {
Steve French467a8f82007-06-27 22:41:32 +00002146 cFYI(1, ("mounting share using direct i/o"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
2148 }
2149
Steve French27adb442008-05-23 19:43:29 +00002150 if ((volume_info.cifs_acl) && (volume_info.dynperm))
2151 cERROR(1, ("mount option dynperm ignored if cifsacl "
2152 "mount option supported"));
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 tcon =
2155 find_unc(sin_server.sin_addr.s_addr, volume_info.UNC,
2156 volume_info.username);
2157 if (tcon) {
Steve Frenchbf820672005-12-01 22:32:42 -08002158 cFYI(1, ("Found match on UNC path"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 /* we can have only one retry value for a connection
2160 to a share so for resources mounted more than once
Steve French50c2f752007-07-13 00:33:32 +00002161 to the same server share the last value passed in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 for the retry flag is used */
2163 tcon->retry = volume_info.retry;
Steve Frenchd3485d32005-08-19 11:04:29 -07002164 tcon->nocase = volume_info.nocase;
Steve French95b1cb92008-05-15 16:44:38 +00002165 if (tcon->seal != volume_info.seal)
2166 cERROR(1, ("transport encryption setting "
2167 "conflicts with existing tid"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 } else {
2169 tcon = tconInfoAlloc();
2170 if (tcon == NULL)
2171 rc = -ENOMEM;
2172 else {
Steve French50c2f752007-07-13 00:33:32 +00002173 /* check for null share name ie connecting to
Steve French8af18972007-02-14 04:42:51 +00002174 * dfs root */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Steve French50c2f752007-07-13 00:33:32 +00002176 /* BB check if this works for exactly length
Steve French8af18972007-02-14 04:42:51 +00002177 * three strings */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if ((strchr(volume_info.UNC + 3, '\\') == NULL)
2179 && (strchr(volume_info.UNC + 3, '/') ==
2180 NULL)) {
Steve French646dd532008-05-15 01:50:56 +00002181/* rc = connect_to_dfs_path(xid, pSesInfo,
Steve French8af18972007-02-14 04:42:51 +00002182 "", cifs_sb->local_nls,
Steve French50c2f752007-07-13 00:33:32 +00002183 cifs_sb->mnt_cifs_flags &
Steve French646dd532008-05-15 01:50:56 +00002184 CIFS_MOUNT_MAP_SPECIAL_CHR);*/
2185 cFYI(1, ("DFS root not supported"));
Jeff Layton70fe7dc2007-11-16 22:21:07 +00002186 rc = -ENODEV;
2187 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 } else {
Steve French8af18972007-02-14 04:42:51 +00002189 /* BB Do we need to wrap sesSem around
2190 * this TCon call and Unix SetFS as
2191 * we do on SessSetup and reconnect? */
Steve French50c2f752007-07-13 00:33:32 +00002192 rc = CIFSTCon(xid, pSesInfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 volume_info.UNC,
2194 tcon, cifs_sb->local_nls);
2195 cFYI(1, ("CIFS Tcon rc = %d", rc));
Steve French2c1b8612008-10-16 18:35:21 +00002196 if (volume_info.nodfs) {
2197 tcon->Flags &=
2198 ~SMB_SHARE_IS_IN_DFS;
2199 cFYI(1, ("DFS disabled (%d)",
2200 tcon->Flags));
2201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203 if (!rc) {
2204 atomic_inc(&pSesInfo->inUse);
2205 tcon->retry = volume_info.retry;
Steve Frenchd3485d32005-08-19 11:04:29 -07002206 tcon->nocase = volume_info.nocase;
Steve French95b1cb92008-05-15 16:44:38 +00002207 tcon->seal = volume_info.seal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
2209 }
2210 }
2211 }
Steve French4523cc32007-04-30 20:13:06 +00002212 if (pSesInfo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (pSesInfo->capabilities & CAP_LARGE_FILES) {
2214 sb->s_maxbytes = (u64) 1 << 63;
2215 } else
2216 sb->s_maxbytes = (u64) 1 << 31; /* 2 GB */
2217 }
2218
Steve French8af18972007-02-14 04:42:51 +00002219 /* BB FIXME fix time_gran to be larger for LANMAN sessions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 sb->s_time_gran = 100;
2221
2222/* on error free sesinfo and tcon struct if needed */
2223 if (rc) {
2224 /* if session setup failed, use count is zero but
2225 we still need to free cifsd thread */
Steve French4523cc32007-04-30 20:13:06 +00002226 if (atomic_read(&srvTcp->socketUseCount) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 spin_lock(&GlobalMid_Lock);
2228 srvTcp->tcpStatus = CifsExiting;
2229 spin_unlock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +00002230 force_sig(SIGKILL, srvTcp->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
2232 /* If find_unc succeeded then rc == 0 so we can not end */
2233 if (tcon) /* up accidently freeing someone elses tcon struct */
2234 tconInfoFree(tcon);
2235 if (existingCifsSes == NULL) {
2236 if (pSesInfo) {
Steve French50c2f752007-07-13 00:33:32 +00002237 if ((pSesInfo->server) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 (pSesInfo->status == CifsGood)) {
2239 int temp_rc;
2240 temp_rc = CIFSSMBLogoff(xid, pSesInfo);
2241 /* if the socketUseCount is now zero */
Steve French4523cc32007-04-30 20:13:06 +00002242 if ((temp_rc == -ESHUTDOWN) &&
Steve French50c2f752007-07-13 00:33:32 +00002243 (pSesInfo->server) &&
Jeff Layton469ee612008-10-16 18:46:39 +00002244 (pSesInfo->server->tsk))
Jeff5d9c7202007-06-25 22:16:35 +00002245 force_sig(SIGKILL,
2246 pSesInfo->server->tsk);
Steve Frencha0136892007-10-04 20:05:09 +00002247 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 cFYI(1, ("No session or bad tcon"));
Steve Frencha0136892007-10-04 20:05:09 +00002249 if ((pSesInfo->server) &&
2250 (pSesInfo->server->tsk)) {
Jeff Layton469ee612008-10-16 18:46:39 +00002251 spin_lock(&GlobalMid_Lock);
2252 srvTcp->tcpStatus = CifsExiting;
2253 spin_unlock(&GlobalMid_Lock);
Steve Frencha0136892007-10-04 20:05:09 +00002254 force_sig(SIGKILL,
2255 pSesInfo->server->tsk);
Steve Frencha0136892007-10-04 20:05:09 +00002256 }
2257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 sesInfoFree(pSesInfo);
2259 /* pSesInfo = NULL; */
2260 }
2261 }
2262 } else {
2263 atomic_inc(&tcon->useCount);
2264 cifs_sb->tcon = tcon;
2265 tcon->ses = pSesInfo;
2266
Steve French82940a42006-03-02 03:24:57 +00002267 /* do not care if following two calls succeed - informational */
Steve French7f8ed422007-09-28 22:28:55 +00002268 if (!tcon->ipc) {
2269 CIFSSMBQFSDeviceInfo(xid, tcon);
2270 CIFSSMBQFSAttributeInfo(xid, tcon);
2271 }
Steve French50c2f752007-07-13 00:33:32 +00002272
Steve French8af18972007-02-14 04:42:51 +00002273 /* tell server which Unix caps we support */
2274 if (tcon->ses->capabilities & CAP_UNIX)
Steve Frenchc18c8422007-07-18 23:21:09 +00002275 /* reset of caps checks mount to see if unix extensions
2276 disabled for just this mount */
Steve French8af18972007-02-14 04:42:51 +00002277 reset_cifs_unix_caps(xid, tcon, sb, &volume_info);
Steve Frenchc18c8422007-07-18 23:21:09 +00002278 else
2279 tcon->unix_ext = 0; /* server does not support them */
2280
Steve French03a143c2008-02-14 06:38:30 +00002281 /* convert forward to back slashes in prepath here if needed */
Igor Mammedov11b6d642008-02-15 19:06:04 +00002282 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) == 0)
2283 convert_delimiter(cifs_sb->prepath,
2284 CIFS_DIR_SEP(cifs_sb));
Steve French03a143c2008-02-14 06:38:30 +00002285
Steve Frenchc18c8422007-07-18 23:21:09 +00002286 if ((tcon->unix_ext == 0) && (cifs_sb->rsize > (1024 * 127))) {
Steve French75865f8c2007-06-24 18:30:48 +00002287 cifs_sb->rsize = 1024 * 127;
Steve French90c81e02008-02-12 20:32:36 +00002288 cFYI(DBG2,
2289 ("no very large read support, rsize now 127K"));
Steve French75865f8c2007-06-24 18:30:48 +00002290 }
Steve French3e844692005-10-03 13:37:24 -07002291 if (!(tcon->ses->capabilities & CAP_LARGE_WRITE_X))
2292 cifs_sb->wsize = min(cifs_sb->wsize,
2293 (tcon->ses->server->maxBuf -
2294 MAX_CIFS_HDR_SIZE));
Steve French0ae0efa2005-10-10 10:57:19 -07002295 if (!(tcon->ses->capabilities & CAP_LARGE_READ_X))
Steve French50c2f752007-07-13 00:33:32 +00002296 cifs_sb->rsize = min(cifs_sb->rsize,
2297 (tcon->ses->server->maxBuf -
2298 MAX_CIFS_HDR_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300
2301 /* volume_info.password is freed above when existing session found
2302 (in which case it is not needed anymore) but when new sesion is created
2303 the password ptr is put in the new session structure (in which case the
2304 password will be freed at unmount time) */
Jeff Layton70fe7dc2007-11-16 22:21:07 +00002305out:
2306 /* zero out password before freeing */
2307 if (volume_info.password != NULL) {
2308 memset(volume_info.password, 0, strlen(volume_info.password));
2309 kfree(volume_info.password);
2310 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08002311 kfree(volume_info.UNC);
Steve French2fe87f02006-09-21 07:02:52 +00002312 kfree(volume_info.prepath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 FreeXid(xid);
2314 return rc;
2315}
2316
2317static int
2318CIFSSessSetup(unsigned int xid, struct cifsSesInfo *ses,
Steve French7c7b25b2006-06-01 19:20:10 +00002319 char session_key[CIFS_SESS_KEY_SIZE],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 const struct nls_table *nls_codepage)
2321{
2322 struct smb_hdr *smb_buffer;
2323 struct smb_hdr *smb_buffer_response;
2324 SESSION_SETUP_ANDX *pSMB;
2325 SESSION_SETUP_ANDX *pSMBr;
2326 char *bcc_ptr;
2327 char *user;
2328 char *domain;
2329 int rc = 0;
2330 int remaining_words = 0;
2331 int bytes_returned = 0;
2332 int len;
2333 __u32 capabilities;
2334 __u16 count;
2335
Steve Frencheeac8042006-01-13 21:34:58 -08002336 cFYI(1, ("In sesssetup"));
Steve French4523cc32007-04-30 20:13:06 +00002337 if (ses == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return -EINVAL;
2339 user = ses->userName;
2340 domain = ses->domainName;
2341 smb_buffer = cifs_buf_get();
Steve French582d21e2008-05-13 04:54:12 +00002342
2343 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return -ENOMEM;
Steve French582d21e2008-05-13 04:54:12 +00002345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 smb_buffer_response = smb_buffer;
2347 pSMBr = pSMB = (SESSION_SETUP_ANDX *) smb_buffer;
2348
2349 /* send SMBsessionSetup here */
2350 header_assemble(smb_buffer, SMB_COM_SESSION_SETUP_ANDX,
2351 NULL /* no tCon exists yet */ , 13 /* wct */ );
2352
Steve French1982c342005-08-17 12:38:22 -07002353 smb_buffer->Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 pSMB->req_no_secext.AndXCommand = 0xFF;
2355 pSMB->req_no_secext.MaxBufferSize = cpu_to_le16(ses->server->maxBuf);
2356 pSMB->req_no_secext.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
2357
Steve French50c2f752007-07-13 00:33:32 +00002358 if (ses->server->secMode &
2359 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
2361
2362 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
2363 CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
2364 if (ses->capabilities & CAP_UNICODE) {
2365 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
2366 capabilities |= CAP_UNICODE;
2367 }
2368 if (ses->capabilities & CAP_STATUS32) {
2369 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
2370 capabilities |= CAP_STATUS32;
2371 }
2372 if (ses->capabilities & CAP_DFS) {
2373 smb_buffer->Flags2 |= SMBFLG2_DFS;
2374 capabilities |= CAP_DFS;
2375 }
2376 pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
2377
Steve French50c2f752007-07-13 00:33:32 +00002378 pSMB->req_no_secext.CaseInsensitivePasswordLength =
Steve French7c7b25b2006-06-01 19:20:10 +00002379 cpu_to_le16(CIFS_SESS_KEY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 pSMB->req_no_secext.CaseSensitivePasswordLength =
Steve French7c7b25b2006-06-01 19:20:10 +00002382 cpu_to_le16(CIFS_SESS_KEY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 bcc_ptr = pByteArea(smb_buffer);
Steve French7c7b25b2006-06-01 19:20:10 +00002384 memcpy(bcc_ptr, (char *) session_key, CIFS_SESS_KEY_SIZE);
2385 bcc_ptr += CIFS_SESS_KEY_SIZE;
2386 memcpy(bcc_ptr, (char *) session_key, CIFS_SESS_KEY_SIZE);
2387 bcc_ptr += CIFS_SESS_KEY_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 if (ses->capabilities & CAP_UNICODE) {
2390 if ((long) bcc_ptr % 2) { /* must be word aligned for Unicode */
2391 *bcc_ptr = 0;
2392 bcc_ptr++;
2393 }
Steve French4523cc32007-04-30 20:13:06 +00002394 if (user == NULL)
Steve French39798772006-05-31 22:40:51 +00002395 bytes_returned = 0; /* skip null user */
Steve French50c2f752007-07-13 00:33:32 +00002396 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 bytes_returned =
Steve French50c2f752007-07-13 00:33:32 +00002398 cifs_strtoUCS((__le16 *) bcc_ptr, user, 100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 nls_codepage);
2400 /* convert number of 16 bit words to bytes */
2401 bcc_ptr += 2 * bytes_returned;
2402 bcc_ptr += 2; /* trailing null */
2403 if (domain == NULL)
2404 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08002405 cifs_strtoUCS((__le16 *) bcc_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 "CIFS_LINUX_DOM", 32, nls_codepage);
2407 else
2408 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08002409 cifs_strtoUCS((__le16 *) bcc_ptr, domain, 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 nls_codepage);
2411 bcc_ptr += 2 * bytes_returned;
2412 bcc_ptr += 2;
2413 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08002414 cifs_strtoUCS((__le16 *) bcc_ptr, "Linux version ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 32, nls_codepage);
2416 bcc_ptr += 2 * bytes_returned;
2417 bytes_returned =
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07002418 cifs_strtoUCS((__le16 *) bcc_ptr, utsname()->release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 32, nls_codepage);
2420 bcc_ptr += 2 * bytes_returned;
2421 bcc_ptr += 2;
2422 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08002423 cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 64, nls_codepage);
2425 bcc_ptr += 2 * bytes_returned;
2426 bcc_ptr += 2;
2427 } else {
Steve French50c2f752007-07-13 00:33:32 +00002428 if (user != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 strncpy(bcc_ptr, user, 200);
2430 bcc_ptr += strnlen(user, 200);
2431 }
2432 *bcc_ptr = 0;
2433 bcc_ptr++;
2434 if (domain == NULL) {
2435 strcpy(bcc_ptr, "CIFS_LINUX_DOM");
2436 bcc_ptr += strlen("CIFS_LINUX_DOM") + 1;
2437 } else {
2438 strncpy(bcc_ptr, domain, 64);
2439 bcc_ptr += strnlen(domain, 64);
2440 *bcc_ptr = 0;
2441 bcc_ptr++;
2442 }
2443 strcpy(bcc_ptr, "Linux version ");
2444 bcc_ptr += strlen("Linux version ");
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07002445 strcpy(bcc_ptr, utsname()->release);
2446 bcc_ptr += strlen(utsname()->release) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
2448 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
2449 }
2450 count = (long) bcc_ptr - (long) pByteArea(smb_buffer);
2451 smb_buffer->smb_buf_length += count;
2452 pSMB->req_no_secext.ByteCount = cpu_to_le16(count);
2453
2454 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response,
Steve French133672e2007-11-13 22:41:37 +00002455 &bytes_returned, CIFS_LONG_OP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if (rc) {
2457/* rc = map_smb_to_linux_error(smb_buffer_response); now done in SendReceive */
2458 } else if ((smb_buffer_response->WordCount == 3)
2459 || (smb_buffer_response->WordCount == 4)) {
2460 __u16 action = le16_to_cpu(pSMBr->resp.Action);
2461 __u16 blob_len = le16_to_cpu(pSMBr->resp.SecurityBlobLength);
2462 if (action & GUEST_LOGIN)
Steve French50c2f752007-07-13 00:33:32 +00002463 cFYI(1, (" Guest login")); /* BB mark SesInfo struct? */
2464 ses->Suid = smb_buffer_response->Uid; /* UID left in wire format
2465 (little endian) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 cFYI(1, ("UID = %d ", ses->Suid));
Steve French50c2f752007-07-13 00:33:32 +00002467 /* response can have either 3 or 4 word count - Samba sends 3 */
2468 bcc_ptr = pByteArea(smb_buffer_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if ((pSMBr->resp.hdr.WordCount == 3)
2470 || ((pSMBr->resp.hdr.WordCount == 4)
2471 && (blob_len < pSMBr->resp.ByteCount))) {
2472 if (pSMBr->resp.hdr.WordCount == 4)
2473 bcc_ptr += blob_len;
2474
2475 if (smb_buffer->Flags2 & SMBFLG2_UNICODE) {
2476 if ((long) (bcc_ptr) % 2) {
2477 remaining_words =
Steve French50c2f752007-07-13 00:33:32 +00002478 (BCC(smb_buffer_response) - 1) / 2;
2479 /* Unicode strings must be word
2480 aligned */
2481 bcc_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 } else {
2483 remaining_words =
2484 BCC(smb_buffer_response) / 2;
2485 }
2486 len =
2487 UniStrnlen((wchar_t *) bcc_ptr,
2488 remaining_words - 1);
2489/* We look for obvious messed up bcc or strings in response so we do not go off
2490 the end since (at least) WIN2K and Windows XP have a major bug in not null
2491 terminating last Unicode string in response */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002492 if (ses->serverOS)
Steve Frencha424f8b2006-05-30 18:06:04 +00002493 kfree(ses->serverOS);
Steve French50c2f752007-07-13 00:33:32 +00002494 ses->serverOS = kzalloc(2 * (len + 1),
2495 GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002496 if (ses->serverOS == NULL)
Steve French433dc242005-04-28 22:41:08 -07002497 goto sesssetup_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 cifs_strfromUCS_le(ses->serverOS,
Steve French50c2f752007-07-13 00:33:32 +00002499 (__le16 *)bcc_ptr,
2500 len, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 bcc_ptr += 2 * (len + 1);
2502 remaining_words -= len + 1;
2503 ses->serverOS[2 * len] = 0;
2504 ses->serverOS[1 + (2 * len)] = 0;
2505 if (remaining_words > 0) {
2506 len = UniStrnlen((wchar_t *)bcc_ptr,
2507 remaining_words-1);
Steve Frenchcd49b492006-06-26 04:22:36 +00002508 kfree(ses->serverNOS);
Steve French50c2f752007-07-13 00:33:32 +00002509 ses->serverNOS = kzalloc(2 * (len + 1),
2510 GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002511 if (ses->serverNOS == NULL)
Steve French433dc242005-04-28 22:41:08 -07002512 goto sesssetup_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 cifs_strfromUCS_le(ses->serverNOS,
Steve French50c2f752007-07-13 00:33:32 +00002514 (__le16 *)bcc_ptr,
2515 len, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 bcc_ptr += 2 * (len + 1);
2517 ses->serverNOS[2 * len] = 0;
2518 ses->serverNOS[1 + (2 * len)] = 0;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002519 if (strncmp(ses->serverNOS,
Steve French50c2f752007-07-13 00:33:32 +00002520 "NT LAN Manager 4", 16) == 0) {
Steve French467a8f82007-06-27 22:41:32 +00002521 cFYI(1, ("NT4 server"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 ses->flags |= CIFS_SES_NT4;
2523 }
2524 remaining_words -= len + 1;
2525 if (remaining_words > 0) {
Steve French433dc242005-04-28 22:41:08 -07002526 len = UniStrnlen((wchar_t *) bcc_ptr, remaining_words);
Steve French50c2f752007-07-13 00:33:32 +00002527 /* last string is not always null terminated
2528 (for e.g. for Windows XP & 2000) */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002529 if (ses->serverDomain)
Steve Frencha424f8b2006-05-30 18:06:04 +00002530 kfree(ses->serverDomain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 ses->serverDomain =
Steve French50c2f752007-07-13 00:33:32 +00002532 kzalloc(2*(len+1),
2533 GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002534 if (ses->serverDomain == NULL)
Steve French433dc242005-04-28 22:41:08 -07002535 goto sesssetup_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 cifs_strfromUCS_le(ses->serverDomain,
Steve French50c2f752007-07-13 00:33:32 +00002537 (__le16 *)bcc_ptr,
2538 len, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 bcc_ptr += 2 * (len + 1);
2540 ses->serverDomain[2*len] = 0;
2541 ses->serverDomain[1+(2*len)] = 0;
Steve French50c2f752007-07-13 00:33:32 +00002542 } else { /* else no more room so create
2543 dummy domain string */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002544 if (ses->serverDomain)
Steve Frencha424f8b2006-05-30 18:06:04 +00002545 kfree(ses->serverDomain);
Steve French50c2f752007-07-13 00:33:32 +00002546 ses->serverDomain =
Pekka Enberge915fc42005-09-06 15:18:35 -07002547 kzalloc(2, GFP_KERNEL);
Steve Frencha424f8b2006-05-30 18:06:04 +00002548 }
Steve French50c2f752007-07-13 00:33:32 +00002549 } else { /* no room so create dummy domain
2550 and NOS string */
2551
Steve French433dc242005-04-28 22:41:08 -07002552 /* if these kcallocs fail not much we
2553 can do, but better to not fail the
2554 sesssetup itself */
Steve Frenchcd49b492006-06-26 04:22:36 +00002555 kfree(ses->serverDomain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 ses->serverDomain =
Pekka Enberge915fc42005-09-06 15:18:35 -07002557 kzalloc(2, GFP_KERNEL);
Steve Frenchcd49b492006-06-26 04:22:36 +00002558 kfree(ses->serverNOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 ses->serverNOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07002560 kzalloc(2, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 }
2562 } else { /* ASCII */
2563 len = strnlen(bcc_ptr, 1024);
2564 if (((long) bcc_ptr + len) - (long)
2565 pByteArea(smb_buffer_response)
2566 <= BCC(smb_buffer_response)) {
Steve Frenchcd49b492006-06-26 04:22:36 +00002567 kfree(ses->serverOS);
Steve French50c2f752007-07-13 00:33:32 +00002568 ses->serverOS = kzalloc(len + 1,
2569 GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002570 if (ses->serverOS == NULL)
Steve French433dc242005-04-28 22:41:08 -07002571 goto sesssetup_nomem;
Steve French50c2f752007-07-13 00:33:32 +00002572 strncpy(ses->serverOS, bcc_ptr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574 bcc_ptr += len;
Steve French50c2f752007-07-13 00:33:32 +00002575 /* null terminate the string */
2576 bcc_ptr[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 bcc_ptr++;
2578
2579 len = strnlen(bcc_ptr, 1024);
Steve Frenchcd49b492006-06-26 04:22:36 +00002580 kfree(ses->serverNOS);
Steve French50c2f752007-07-13 00:33:32 +00002581 ses->serverNOS = kzalloc(len + 1,
2582 GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002583 if (ses->serverNOS == NULL)
Steve French433dc242005-04-28 22:41:08 -07002584 goto sesssetup_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 strncpy(ses->serverNOS, bcc_ptr, len);
2586 bcc_ptr += len;
2587 bcc_ptr[0] = 0;
2588 bcc_ptr++;
2589
2590 len = strnlen(bcc_ptr, 1024);
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 = kzalloc(len + 1,
2594 GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002595 if (ses->serverDomain == NULL)
Steve French433dc242005-04-28 22:41:08 -07002596 goto sesssetup_nomem;
Steve French50c2f752007-07-13 00:33:32 +00002597 strncpy(ses->serverDomain, bcc_ptr,
2598 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 bcc_ptr += len;
2600 bcc_ptr[0] = 0;
2601 bcc_ptr++;
2602 } else
2603 cFYI(1,
Steve French50c2f752007-07-13 00:33:32 +00002604 ("Variable field of length %d "
2605 "extends beyond end of smb ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 len));
2607 }
2608 } else {
2609 cERROR(1,
Steve French50c2f752007-07-13 00:33:32 +00002610 (" Security Blob Length extends beyond "
2611 "end of SMB"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 }
2613 } else {
2614 cERROR(1,
2615 (" Invalid Word count %d: ",
2616 smb_buffer_response->WordCount));
2617 rc = -EIO;
2618 }
Steve French433dc242005-04-28 22:41:08 -07002619sesssetup_nomem: /* do not return an error on nomem for the info strings,
2620 since that could make reconnection harder, and
2621 reconnection might be needed to free memory */
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00002622 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624 return rc;
2625}
2626
2627static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628CIFSNTLMSSPNegotiateSessSetup(unsigned int xid,
Steve French4b18f2a2008-04-29 00:06:05 +00002629 struct cifsSesInfo *ses, bool *pNTLMv2_flag,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 const struct nls_table *nls_codepage)
2631{
2632 struct smb_hdr *smb_buffer;
2633 struct smb_hdr *smb_buffer_response;
2634 SESSION_SETUP_ANDX *pSMB;
2635 SESSION_SETUP_ANDX *pSMBr;
2636 char *bcc_ptr;
2637 char *domain;
2638 int rc = 0;
2639 int remaining_words = 0;
2640 int bytes_returned = 0;
2641 int len;
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00002642 int SecurityBlobLength = sizeof(NEGOTIATE_MESSAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 PNEGOTIATE_MESSAGE SecurityBlob;
2644 PCHALLENGE_MESSAGE SecurityBlob2;
2645 __u32 negotiate_flags, capabilities;
2646 __u16 count;
2647
Steve French12b3b8f2006-02-09 21:12:47 +00002648 cFYI(1, ("In NTLMSSP sesssetup (negotiate)"));
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002649 if (ses == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 return -EINVAL;
2651 domain = ses->domainName;
Steve French4b18f2a2008-04-29 00:06:05 +00002652 *pNTLMv2_flag = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 smb_buffer = cifs_buf_get();
2654 if (smb_buffer == NULL) {
2655 return -ENOMEM;
2656 }
2657 smb_buffer_response = smb_buffer;
2658 pSMB = (SESSION_SETUP_ANDX *) smb_buffer;
2659 pSMBr = (SESSION_SETUP_ANDX *) smb_buffer_response;
2660
2661 /* send SMBsessionSetup here */
2662 header_assemble(smb_buffer, SMB_COM_SESSION_SETUP_ANDX,
2663 NULL /* no tCon exists yet */ , 12 /* wct */ );
Steve French1982c342005-08-17 12:38:22 -07002664
2665 smb_buffer->Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
2667 pSMB->req.hdr.Flags |= (SMBFLG_CASELESS | SMBFLG_CANONICAL_PATH_FORMAT);
2668
2669 pSMB->req.AndXCommand = 0xFF;
2670 pSMB->req.MaxBufferSize = cpu_to_le16(ses->server->maxBuf);
2671 pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
2672
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002673 if (ses->server->secMode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
2675
2676 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
2677 CAP_EXTENDED_SECURITY;
2678 if (ses->capabilities & CAP_UNICODE) {
2679 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
2680 capabilities |= CAP_UNICODE;
2681 }
2682 if (ses->capabilities & CAP_STATUS32) {
2683 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
2684 capabilities |= CAP_STATUS32;
2685 }
2686 if (ses->capabilities & CAP_DFS) {
2687 smb_buffer->Flags2 |= SMBFLG2_DFS;
2688 capabilities |= CAP_DFS;
2689 }
2690 pSMB->req.Capabilities = cpu_to_le32(capabilities);
2691
2692 bcc_ptr = (char *) &pSMB->req.SecurityBlob;
2693 SecurityBlob = (PNEGOTIATE_MESSAGE) bcc_ptr;
2694 strncpy(SecurityBlob->Signature, NTLMSSP_SIGNATURE, 8);
2695 SecurityBlob->MessageType = NtLmNegotiate;
2696 negotiate_flags =
2697 NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_OEM |
Steve French12b3b8f2006-02-09 21:12:47 +00002698 NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_NTLM |
2699 NTLMSSP_NEGOTIATE_56 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 /* NTLMSSP_NEGOTIATE_ALWAYS_SIGN | */ NTLMSSP_NEGOTIATE_128;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002701 if (sign_CIFS_PDUs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 negotiate_flags |= NTLMSSP_NEGOTIATE_SIGN;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002703/* if (ntlmv2_support)
Steve French39798772006-05-31 22:40:51 +00002704 negotiate_flags |= NTLMSSP_NEGOTIATE_NTLMV2;*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 /* setup pointers to domain name and workstation name */
2706 bcc_ptr += SecurityBlobLength;
2707
2708 SecurityBlob->WorkstationName.Buffer = 0;
2709 SecurityBlob->WorkstationName.Length = 0;
2710 SecurityBlob->WorkstationName.MaximumLength = 0;
2711
Steve French12b3b8f2006-02-09 21:12:47 +00002712 /* Domain not sent on first Sesssetup in NTLMSSP, instead it is sent
2713 along with username on auth request (ie the response to challenge) */
2714 SecurityBlob->DomainName.Buffer = 0;
2715 SecurityBlob->DomainName.Length = 0;
2716 SecurityBlob->DomainName.MaximumLength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 if (ses->capabilities & CAP_UNICODE) {
2718 if ((long) bcc_ptr % 2) {
2719 *bcc_ptr = 0;
2720 bcc_ptr++;
2721 }
2722
2723 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08002724 cifs_strtoUCS((__le16 *) bcc_ptr, "Linux version ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 32, nls_codepage);
2726 bcc_ptr += 2 * bytes_returned;
2727 bytes_returned =
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07002728 cifs_strtoUCS((__le16 *) bcc_ptr, utsname()->release, 32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 nls_codepage);
2730 bcc_ptr += 2 * bytes_returned;
2731 bcc_ptr += 2; /* null terminate Linux version */
2732 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08002733 cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 64, nls_codepage);
2735 bcc_ptr += 2 * bytes_returned;
2736 *(bcc_ptr + 1) = 0;
2737 *(bcc_ptr + 2) = 0;
2738 bcc_ptr += 2; /* null terminate network opsys string */
2739 *(bcc_ptr + 1) = 0;
2740 *(bcc_ptr + 2) = 0;
2741 bcc_ptr += 2; /* null domain */
2742 } else { /* ASCII */
2743 strcpy(bcc_ptr, "Linux version ");
2744 bcc_ptr += strlen("Linux version ");
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07002745 strcpy(bcc_ptr, utsname()->release);
2746 bcc_ptr += strlen(utsname()->release) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
2748 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
2749 bcc_ptr++; /* empty domain field */
2750 *bcc_ptr = 0;
2751 }
2752 SecurityBlob->NegotiateFlags = cpu_to_le32(negotiate_flags);
2753 pSMB->req.SecurityBlobLength = cpu_to_le16(SecurityBlobLength);
2754 count = (long) bcc_ptr - (long) pByteArea(smb_buffer);
2755 smb_buffer->smb_buf_length += count;
2756 pSMB->req.ByteCount = cpu_to_le16(count);
2757
2758 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response,
Steve French133672e2007-11-13 22:41:37 +00002759 &bytes_returned, CIFS_LONG_OP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 if (smb_buffer_response->Status.CifsError ==
2762 cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
2763 rc = 0;
2764
2765 if (rc) {
2766/* rc = map_smb_to_linux_error(smb_buffer_response); *//* done in SendReceive now */
2767 } else if ((smb_buffer_response->WordCount == 3)
2768 || (smb_buffer_response->WordCount == 4)) {
2769 __u16 action = le16_to_cpu(pSMBr->resp.Action);
2770 __u16 blob_len = le16_to_cpu(pSMBr->resp.SecurityBlobLength);
2771
2772 if (action & GUEST_LOGIN)
Steve French50c2f752007-07-13 00:33:32 +00002773 cFYI(1, (" Guest login"));
2774 /* Do we want to set anything in SesInfo struct when guest login? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Steve French50c2f752007-07-13 00:33:32 +00002776 bcc_ptr = pByteArea(smb_buffer_response);
2777 /* response can have either 3 or 4 word count - Samba sends 3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 SecurityBlob2 = (PCHALLENGE_MESSAGE) bcc_ptr;
2780 if (SecurityBlob2->MessageType != NtLmChallenge) {
2781 cFYI(1,
2782 ("Unexpected NTLMSSP message type received %d",
2783 SecurityBlob2->MessageType));
2784 } else if (ses) {
Steve French50c2f752007-07-13 00:33:32 +00002785 ses->Suid = smb_buffer_response->Uid; /* UID left in le format */
Steve French12b3b8f2006-02-09 21:12:47 +00002786 cFYI(1, ("UID = %d", ses->Suid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if ((pSMBr->resp.hdr.WordCount == 3)
2788 || ((pSMBr->resp.hdr.WordCount == 4)
2789 && (blob_len <
2790 pSMBr->resp.ByteCount))) {
2791
2792 if (pSMBr->resp.hdr.WordCount == 4) {
2793 bcc_ptr += blob_len;
Steve French12b3b8f2006-02-09 21:12:47 +00002794 cFYI(1, ("Security Blob Length %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 blob_len));
2796 }
2797
Steve French12b3b8f2006-02-09 21:12:47 +00002798 cFYI(1, ("NTLMSSP Challenge rcvd"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 memcpy(ses->server->cryptKey,
2801 SecurityBlob2->Challenge,
2802 CIFS_CRYPTO_KEY_SIZE);
Steve French50c2f752007-07-13 00:33:32 +00002803 if (SecurityBlob2->NegotiateFlags &
Steve French12b3b8f2006-02-09 21:12:47 +00002804 cpu_to_le32(NTLMSSP_NEGOTIATE_NTLMV2))
Steve French4b18f2a2008-04-29 00:06:05 +00002805 *pNTLMv2_flag = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
Steve French50c2f752007-07-13 00:33:32 +00002807 if ((SecurityBlob2->NegotiateFlags &
2808 cpu_to_le32(NTLMSSP_NEGOTIATE_ALWAYS_SIGN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 || (sign_CIFS_PDUs > 1))
Steve French50c2f752007-07-13 00:33:32 +00002810 ses->server->secMode |=
2811 SECMODE_SIGN_REQUIRED;
2812 if ((SecurityBlob2->NegotiateFlags &
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 cpu_to_le32(NTLMSSP_NEGOTIATE_SIGN)) && (sign_CIFS_PDUs))
Steve French50c2f752007-07-13 00:33:32 +00002814 ses->server->secMode |=
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 SECMODE_SIGN_ENABLED;
2816
2817 if (smb_buffer->Flags2 & SMBFLG2_UNICODE) {
2818 if ((long) (bcc_ptr) % 2) {
2819 remaining_words =
2820 (BCC(smb_buffer_response)
2821 - 1) / 2;
Steve French50c2f752007-07-13 00:33:32 +00002822 /* Must word align unicode strings */
2823 bcc_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 } else {
2825 remaining_words =
2826 BCC
2827 (smb_buffer_response) / 2;
2828 }
2829 len =
2830 UniStrnlen((wchar_t *) bcc_ptr,
2831 remaining_words - 1);
2832/* We look for obvious messed up bcc or strings in response so we do not go off
2833 the end since (at least) WIN2K and Windows XP have a major bug in not null
2834 terminating last Unicode string in response */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002835 if (ses->serverOS)
Steve Frencha424f8b2006-05-30 18:06:04 +00002836 kfree(ses->serverOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 ses->serverOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07002838 kzalloc(2 * (len + 1), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 cifs_strfromUCS_le(ses->serverOS,
Steve Frenche89dc922005-11-11 15:18:19 -08002840 (__le16 *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 bcc_ptr, len,
2842 nls_codepage);
2843 bcc_ptr += 2 * (len + 1);
2844 remaining_words -= len + 1;
2845 ses->serverOS[2 * len] = 0;
2846 ses->serverOS[1 + (2 * len)] = 0;
2847 if (remaining_words > 0) {
2848 len = UniStrnlen((wchar_t *)
2849 bcc_ptr,
2850 remaining_words
2851 - 1);
Steve Frenchcd49b492006-06-26 04:22:36 +00002852 kfree(ses->serverNOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 ses->serverNOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07002854 kzalloc(2 * (len + 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 GFP_KERNEL);
2856 cifs_strfromUCS_le(ses->
2857 serverNOS,
Steve Frenche89dc922005-11-11 15:18:19 -08002858 (__le16 *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 bcc_ptr,
2860 len,
2861 nls_codepage);
2862 bcc_ptr += 2 * (len + 1);
2863 ses->serverNOS[2 * len] = 0;
2864 ses->serverNOS[1 +
2865 (2 * len)] = 0;
2866 remaining_words -= len + 1;
2867 if (remaining_words > 0) {
Steve French50c2f752007-07-13 00:33:32 +00002868 len = UniStrnlen((wchar_t *) bcc_ptr, remaining_words);
2869 /* last string not always null terminated
2870 (for e.g. for Windows XP & 2000) */
Steve Frenchcd49b492006-06-26 04:22:36 +00002871 kfree(ses->serverDomain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 ses->serverDomain =
Pekka Enberge915fc42005-09-06 15:18:35 -07002873 kzalloc(2 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 (len +
2875 1),
2876 GFP_KERNEL);
2877 cifs_strfromUCS_le
Steve Frenche89dc922005-11-11 15:18:19 -08002878 (ses->serverDomain,
2879 (__le16 *)bcc_ptr,
2880 len, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 bcc_ptr +=
2882 2 * (len + 1);
Steve Frenche89dc922005-11-11 15:18:19 -08002883 ses->serverDomain[2*len]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 = 0;
Steve Frenche89dc922005-11-11 15:18:19 -08002885 ses->serverDomain
2886 [1 + (2 * len)]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 = 0;
2888 } /* else no more room so create dummy domain string */
Steve Frencha424f8b2006-05-30 18:06:04 +00002889 else {
Steve Frenchcd49b492006-06-26 04:22:36 +00002890 kfree(ses->serverDomain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 ses->serverDomain =
Pekka Enberge915fc42005-09-06 15:18:35 -07002892 kzalloc(2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 GFP_KERNEL);
Steve Frencha424f8b2006-05-30 18:06:04 +00002894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 } else { /* no room so create dummy domain and NOS string */
Steve Frenchcd49b492006-06-26 04:22:36 +00002896 kfree(ses->serverDomain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 ses->serverDomain =
Pekka Enberge915fc42005-09-06 15:18:35 -07002898 kzalloc(2, GFP_KERNEL);
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, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 }
2903 } else { /* ASCII */
2904 len = strnlen(bcc_ptr, 1024);
2905 if (((long) bcc_ptr + len) - (long)
2906 pByteArea(smb_buffer_response)
2907 <= BCC(smb_buffer_response)) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002908 if (ses->serverOS)
Steve Frencha424f8b2006-05-30 18:06:04 +00002909 kfree(ses->serverOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 ses->serverOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07002911 kzalloc(len + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 GFP_KERNEL);
2913 strncpy(ses->serverOS,
2914 bcc_ptr, len);
2915
2916 bcc_ptr += len;
2917 bcc_ptr[0] = 0; /* null terminate string */
2918 bcc_ptr++;
2919
2920 len = strnlen(bcc_ptr, 1024);
Steve Frenchcd49b492006-06-26 04:22:36 +00002921 kfree(ses->serverNOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 ses->serverNOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07002923 kzalloc(len + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 GFP_KERNEL);
2925 strncpy(ses->serverNOS, bcc_ptr, len);
2926 bcc_ptr += len;
2927 bcc_ptr[0] = 0;
2928 bcc_ptr++;
2929
2930 len = strnlen(bcc_ptr, 1024);
Steve Frenchcd49b492006-06-26 04:22:36 +00002931 kfree(ses->serverDomain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 ses->serverDomain =
Pekka Enberge915fc42005-09-06 15:18:35 -07002933 kzalloc(len + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 GFP_KERNEL);
Steve French50c2f752007-07-13 00:33:32 +00002935 strncpy(ses->serverDomain,
2936 bcc_ptr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 bcc_ptr += len;
2938 bcc_ptr[0] = 0;
2939 bcc_ptr++;
2940 } else
2941 cFYI(1,
Steve French63135e02007-07-17 17:34:02 +00002942 ("field of length %d "
2943 "extends beyond end of smb",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 len));
2945 }
2946 } else {
Steve French50c2f752007-07-13 00:33:32 +00002947 cERROR(1, ("Security Blob Length extends beyond"
2948 " end of SMB"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
2950 } else {
2951 cERROR(1, ("No session structure passed in."));
2952 }
2953 } else {
2954 cERROR(1,
Steve French5815449d2006-02-14 01:36:20 +00002955 (" Invalid Word count %d:",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 smb_buffer_response->WordCount));
2957 rc = -EIO;
2958 }
2959
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00002960 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
2962 return rc;
2963}
2964static int
2965CIFSNTLMSSPAuthSessSetup(unsigned int xid, struct cifsSesInfo *ses,
Steve French4b18f2a2008-04-29 00:06:05 +00002966 char *ntlm_session_key, bool ntlmv2_flag,
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00002967 const struct nls_table *nls_codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
2969 struct smb_hdr *smb_buffer;
2970 struct smb_hdr *smb_buffer_response;
2971 SESSION_SETUP_ANDX *pSMB;
2972 SESSION_SETUP_ANDX *pSMBr;
2973 char *bcc_ptr;
2974 char *user;
2975 char *domain;
2976 int rc = 0;
2977 int remaining_words = 0;
2978 int bytes_returned = 0;
2979 int len;
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00002980 int SecurityBlobLength = sizeof(AUTHENTICATE_MESSAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 PAUTHENTICATE_MESSAGE SecurityBlob;
2982 __u32 negotiate_flags, capabilities;
2983 __u16 count;
2984
2985 cFYI(1, ("In NTLMSSPSessSetup (Authenticate)"));
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002986 if (ses == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return -EINVAL;
2988 user = ses->userName;
2989 domain = ses->domainName;
2990 smb_buffer = cifs_buf_get();
2991 if (smb_buffer == NULL) {
2992 return -ENOMEM;
2993 }
2994 smb_buffer_response = smb_buffer;
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00002995 pSMB = (SESSION_SETUP_ANDX *)smb_buffer;
2996 pSMBr = (SESSION_SETUP_ANDX *)smb_buffer_response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
2998 /* send SMBsessionSetup here */
2999 header_assemble(smb_buffer, SMB_COM_SESSION_SETUP_ANDX,
3000 NULL /* no tCon exists yet */ , 12 /* wct */ );
Steve French1982c342005-08-17 12:38:22 -07003001
3002 smb_buffer->Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 pSMB->req.hdr.Flags |= (SMBFLG_CASELESS | SMBFLG_CANONICAL_PATH_FORMAT);
3004 pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
3005 pSMB->req.AndXCommand = 0xFF;
3006 pSMB->req.MaxBufferSize = cpu_to_le16(ses->server->maxBuf);
3007 pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
3008
3009 pSMB->req.hdr.Uid = ses->Suid;
3010
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003011 if (ses->server->secMode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3013
3014 capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003015 CAP_EXTENDED_SECURITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 if (ses->capabilities & CAP_UNICODE) {
3017 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3018 capabilities |= CAP_UNICODE;
3019 }
3020 if (ses->capabilities & CAP_STATUS32) {
3021 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3022 capabilities |= CAP_STATUS32;
3023 }
3024 if (ses->capabilities & CAP_DFS) {
3025 smb_buffer->Flags2 |= SMBFLG2_DFS;
3026 capabilities |= CAP_DFS;
3027 }
3028 pSMB->req.Capabilities = cpu_to_le32(capabilities);
3029
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003030 bcc_ptr = (char *)&pSMB->req.SecurityBlob;
3031 SecurityBlob = (PAUTHENTICATE_MESSAGE)bcc_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 strncpy(SecurityBlob->Signature, NTLMSSP_SIGNATURE, 8);
3033 SecurityBlob->MessageType = NtLmAuthenticate;
3034 bcc_ptr += SecurityBlobLength;
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003035 negotiate_flags = NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_REQUEST_TARGET |
3036 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_TARGET_INFO |
3037 0x80000000 | NTLMSSP_NEGOTIATE_128;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003038 if (sign_CIFS_PDUs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 negotiate_flags |= /* NTLMSSP_NEGOTIATE_ALWAYS_SIGN |*/ NTLMSSP_NEGOTIATE_SIGN;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003040 if (ntlmv2_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 negotiate_flags |= NTLMSSP_NEGOTIATE_NTLMV2;
3042
3043/* setup pointers to domain name and workstation name */
3044
3045 SecurityBlob->WorkstationName.Buffer = 0;
3046 SecurityBlob->WorkstationName.Length = 0;
3047 SecurityBlob->WorkstationName.MaximumLength = 0;
3048 SecurityBlob->SessionKey.Length = 0;
3049 SecurityBlob->SessionKey.MaximumLength = 0;
3050 SecurityBlob->SessionKey.Buffer = 0;
3051
3052 SecurityBlob->LmChallengeResponse.Length = 0;
3053 SecurityBlob->LmChallengeResponse.MaximumLength = 0;
3054 SecurityBlob->LmChallengeResponse.Buffer = 0;
3055
3056 SecurityBlob->NtChallengeResponse.Length =
Steve French7c7b25b2006-06-01 19:20:10 +00003057 cpu_to_le16(CIFS_SESS_KEY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 SecurityBlob->NtChallengeResponse.MaximumLength =
Steve French7c7b25b2006-06-01 19:20:10 +00003059 cpu_to_le16(CIFS_SESS_KEY_SIZE);
3060 memcpy(bcc_ptr, ntlm_session_key, CIFS_SESS_KEY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 SecurityBlob->NtChallengeResponse.Buffer =
3062 cpu_to_le32(SecurityBlobLength);
Steve French7c7b25b2006-06-01 19:20:10 +00003063 SecurityBlobLength += CIFS_SESS_KEY_SIZE;
3064 bcc_ptr += CIFS_SESS_KEY_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
3066 if (ses->capabilities & CAP_UNICODE) {
3067 if (domain == NULL) {
3068 SecurityBlob->DomainName.Buffer = 0;
3069 SecurityBlob->DomainName.Length = 0;
3070 SecurityBlob->DomainName.MaximumLength = 0;
3071 } else {
Steve French77159b42007-08-31 01:10:17 +00003072 __u16 ln = cifs_strtoUCS((__le16 *) bcc_ptr, domain, 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 nls_codepage);
Steve French77159b42007-08-31 01:10:17 +00003074 ln *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 SecurityBlob->DomainName.MaximumLength =
Steve French77159b42007-08-31 01:10:17 +00003076 cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 SecurityBlob->DomainName.Buffer =
3078 cpu_to_le32(SecurityBlobLength);
Steve French77159b42007-08-31 01:10:17 +00003079 bcc_ptr += ln;
3080 SecurityBlobLength += ln;
3081 SecurityBlob->DomainName.Length = cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
3083 if (user == NULL) {
3084 SecurityBlob->UserName.Buffer = 0;
3085 SecurityBlob->UserName.Length = 0;
3086 SecurityBlob->UserName.MaximumLength = 0;
3087 } else {
Steve French77159b42007-08-31 01:10:17 +00003088 __u16 ln = cifs_strtoUCS((__le16 *) bcc_ptr, user, 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 nls_codepage);
Steve French77159b42007-08-31 01:10:17 +00003090 ln *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 SecurityBlob->UserName.MaximumLength =
Steve French77159b42007-08-31 01:10:17 +00003092 cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 SecurityBlob->UserName.Buffer =
3094 cpu_to_le32(SecurityBlobLength);
Steve French77159b42007-08-31 01:10:17 +00003095 bcc_ptr += ln;
3096 SecurityBlobLength += ln;
3097 SecurityBlob->UserName.Length = cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 }
3099
Steve French63135e02007-07-17 17:34:02 +00003100 /* SecurityBlob->WorkstationName.Length =
3101 cifs_strtoUCS((__le16 *) bcc_ptr, "AMACHINE",64, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 SecurityBlob->WorkstationName.Length *= 2;
Steve French63135e02007-07-17 17:34:02 +00003103 SecurityBlob->WorkstationName.MaximumLength =
3104 cpu_to_le16(SecurityBlob->WorkstationName.Length);
3105 SecurityBlob->WorkstationName.Buffer =
3106 cpu_to_le32(SecurityBlobLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 bcc_ptr += SecurityBlob->WorkstationName.Length;
3108 SecurityBlobLength += SecurityBlob->WorkstationName.Length;
Steve French63135e02007-07-17 17:34:02 +00003109 SecurityBlob->WorkstationName.Length =
3110 cpu_to_le16(SecurityBlob->WorkstationName.Length); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
3112 if ((long) bcc_ptr % 2) {
3113 *bcc_ptr = 0;
3114 bcc_ptr++;
3115 }
3116 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08003117 cifs_strtoUCS((__le16 *) bcc_ptr, "Linux version ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 32, nls_codepage);
3119 bcc_ptr += 2 * bytes_returned;
3120 bytes_returned =
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07003121 cifs_strtoUCS((__le16 *) bcc_ptr, utsname()->release, 32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 nls_codepage);
3123 bcc_ptr += 2 * bytes_returned;
3124 bcc_ptr += 2; /* null term version string */
3125 bytes_returned =
Steve Frenche89dc922005-11-11 15:18:19 -08003126 cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 64, nls_codepage);
3128 bcc_ptr += 2 * bytes_returned;
3129 *(bcc_ptr + 1) = 0;
3130 *(bcc_ptr + 2) = 0;
3131 bcc_ptr += 2; /* null terminate network opsys string */
3132 *(bcc_ptr + 1) = 0;
3133 *(bcc_ptr + 2) = 0;
3134 bcc_ptr += 2; /* null domain */
3135 } else { /* ASCII */
3136 if (domain == NULL) {
3137 SecurityBlob->DomainName.Buffer = 0;
3138 SecurityBlob->DomainName.Length = 0;
3139 SecurityBlob->DomainName.MaximumLength = 0;
3140 } else {
Steve French77159b42007-08-31 01:10:17 +00003141 __u16 ln;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 negotiate_flags |= NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED;
3143 strncpy(bcc_ptr, domain, 63);
Steve French77159b42007-08-31 01:10:17 +00003144 ln = strnlen(domain, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 SecurityBlob->DomainName.MaximumLength =
Steve French77159b42007-08-31 01:10:17 +00003146 cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 SecurityBlob->DomainName.Buffer =
3148 cpu_to_le32(SecurityBlobLength);
Steve French77159b42007-08-31 01:10:17 +00003149 bcc_ptr += ln;
3150 SecurityBlobLength += ln;
3151 SecurityBlob->DomainName.Length = cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 }
3153 if (user == NULL) {
3154 SecurityBlob->UserName.Buffer = 0;
3155 SecurityBlob->UserName.Length = 0;
3156 SecurityBlob->UserName.MaximumLength = 0;
3157 } else {
Steve French77159b42007-08-31 01:10:17 +00003158 __u16 ln;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 strncpy(bcc_ptr, user, 63);
Steve French77159b42007-08-31 01:10:17 +00003160 ln = strnlen(user, 64);
3161 SecurityBlob->UserName.MaximumLength = cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 SecurityBlob->UserName.Buffer =
Steve French77159b42007-08-31 01:10:17 +00003163 cpu_to_le32(SecurityBlobLength);
3164 bcc_ptr += ln;
3165 SecurityBlobLength += ln;
3166 SecurityBlob->UserName.Length = cpu_to_le16(ln);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 }
3168 /* BB fill in our workstation name if known BB */
3169
3170 strcpy(bcc_ptr, "Linux version ");
3171 bcc_ptr += strlen("Linux version ");
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07003172 strcpy(bcc_ptr, utsname()->release);
3173 bcc_ptr += strlen(utsname()->release) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
3175 bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
3176 bcc_ptr++; /* null domain */
3177 *bcc_ptr = 0;
3178 }
3179 SecurityBlob->NegotiateFlags = cpu_to_le32(negotiate_flags);
3180 pSMB->req.SecurityBlobLength = cpu_to_le16(SecurityBlobLength);
3181 count = (long) bcc_ptr - (long) pByteArea(smb_buffer);
3182 smb_buffer->smb_buf_length += count;
3183 pSMB->req.ByteCount = cpu_to_le16(count);
3184
3185 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response,
Steve French133672e2007-11-13 22:41:37 +00003186 &bytes_returned, CIFS_LONG_OP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 if (rc) {
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003188/* rc = map_smb_to_linux_error(smb_buffer_response) done in SendReceive now */
3189 } else if ((smb_buffer_response->WordCount == 3) ||
3190 (smb_buffer_response->WordCount == 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 __u16 action = le16_to_cpu(pSMBr->resp.Action);
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003192 __u16 blob_len = le16_to_cpu(pSMBr->resp.SecurityBlobLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 if (action & GUEST_LOGIN)
Steve French50c2f752007-07-13 00:33:32 +00003194 cFYI(1, (" Guest login")); /* BB Should we set anything
3195 in SesInfo struct ? */
3196/* if (SecurityBlob2->MessageType != NtLm??) {
3197 cFYI("Unexpected message type on auth response is %d"));
3198 } */
3199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 if (ses) {
3201 cFYI(1,
Steve French50c2f752007-07-13 00:33:32 +00003202 ("Check challenge UID %d vs auth response UID %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 ses->Suid, smb_buffer_response->Uid));
Steve French50c2f752007-07-13 00:33:32 +00003204 /* UID left in wire format */
3205 ses->Suid = smb_buffer_response->Uid;
3206 bcc_ptr = pByteArea(smb_buffer_response);
3207 /* response can have either 3 or 4 word count - Samba sends 3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 if ((pSMBr->resp.hdr.WordCount == 3)
3209 || ((pSMBr->resp.hdr.WordCount == 4)
3210 && (blob_len <
3211 pSMBr->resp.ByteCount))) {
3212 if (pSMBr->resp.hdr.WordCount == 4) {
3213 bcc_ptr +=
3214 blob_len;
3215 cFYI(1,
3216 ("Security Blob Length %d ",
3217 blob_len));
3218 }
3219
3220 cFYI(1,
3221 ("NTLMSSP response to Authenticate "));
3222
3223 if (smb_buffer->Flags2 & SMBFLG2_UNICODE) {
3224 if ((long) (bcc_ptr) % 2) {
3225 remaining_words =
3226 (BCC(smb_buffer_response)
3227 - 1) / 2;
3228 bcc_ptr++; /* Unicode strings must be word aligned */
3229 } else {
3230 remaining_words = BCC(smb_buffer_response) / 2;
3231 }
Steve French77159b42007-08-31 01:10:17 +00003232 len = UniStrnlen((wchar_t *) bcc_ptr,
3233 remaining_words - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234/* We look for obvious messed up bcc or strings in response so we do not go off
3235 the end since (at least) WIN2K and Windows XP have a major bug in not null
3236 terminating last Unicode string in response */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003237 if (ses->serverOS)
Steve French08775832006-05-30 18:08:26 +00003238 kfree(ses->serverOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 ses->serverOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07003240 kzalloc(2 * (len + 1), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 cifs_strfromUCS_le(ses->serverOS,
Steve Frenche89dc922005-11-11 15:18:19 -08003242 (__le16 *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 bcc_ptr, len,
3244 nls_codepage);
3245 bcc_ptr += 2 * (len + 1);
3246 remaining_words -= len + 1;
3247 ses->serverOS[2 * len] = 0;
3248 ses->serverOS[1 + (2 * len)] = 0;
3249 if (remaining_words > 0) {
3250 len = UniStrnlen((wchar_t *)
3251 bcc_ptr,
3252 remaining_words
3253 - 1);
Steve Frenchcd49b492006-06-26 04:22:36 +00003254 kfree(ses->serverNOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 ses->serverNOS =
Pekka Enberge915fc42005-09-06 15:18:35 -07003256 kzalloc(2 * (len + 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 GFP_KERNEL);
3258 cifs_strfromUCS_le(ses->
3259 serverNOS,
Steve Frenche89dc922005-11-11 15:18:19 -08003260 (__le16 *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 bcc_ptr,
3262 len,
3263 nls_codepage);
3264 bcc_ptr += 2 * (len + 1);
3265 ses->serverNOS[2 * len] = 0;
3266 ses->serverNOS[1+(2*len)] = 0;
3267 remaining_words -= len + 1;
3268 if (remaining_words > 0) {
Steve French50c2f752007-07-13 00:33:32 +00003269 len = UniStrnlen((wchar_t *) bcc_ptr, remaining_words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 /* last string not always null terminated (e.g. for Windows XP & 2000) */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003271 if (ses->serverDomain)
Steve Frencha424f8b2006-05-30 18:06:04 +00003272 kfree(ses->serverDomain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 ses->serverDomain =
Pekka Enberge915fc42005-09-06 15:18:35 -07003274 kzalloc(2 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 (len +
3276 1),
3277 GFP_KERNEL);
3278 cifs_strfromUCS_le
3279 (ses->
3280 serverDomain,
Steve Frenche89dc922005-11-11 15:18:19 -08003281 (__le16 *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 bcc_ptr, len,
3283 nls_codepage);
3284 bcc_ptr +=
3285 2 * (len + 1);
3286 ses->
3287 serverDomain[2
3288 * len]
3289 = 0;
3290 ses->
3291 serverDomain[1
3292 +
3293 (2
3294 *
3295 len)]
3296 = 0;
3297 } /* else no more room so create dummy domain string */
Steve Frencha424f8b2006-05-30 18:06:04 +00003298 else {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003299 if (ses->serverDomain)
Steve Frencha424f8b2006-05-30 18:06:04 +00003300 kfree(ses->serverDomain);
Pekka Enberge915fc42005-09-06 15:18:35 -07003301 ses->serverDomain = kzalloc(2,GFP_KERNEL);
Steve Frencha424f8b2006-05-30 18:06:04 +00003302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 } else { /* no room so create dummy domain and NOS string */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003304 if (ses->serverDomain)
Steve Frencha424f8b2006-05-30 18:06:04 +00003305 kfree(ses->serverDomain);
Pekka Enberge915fc42005-09-06 15:18:35 -07003306 ses->serverDomain = kzalloc(2, GFP_KERNEL);
Steve Frenchcd49b492006-06-26 04:22:36 +00003307 kfree(ses->serverNOS);
Pekka Enberge915fc42005-09-06 15:18:35 -07003308 ses->serverNOS = kzalloc(2, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 }
3310 } else { /* ASCII */
3311 len = strnlen(bcc_ptr, 1024);
Steve French50c2f752007-07-13 00:33:32 +00003312 if (((long) bcc_ptr + len) -
3313 (long) pByteArea(smb_buffer_response)
Steve French63135e02007-07-17 17:34:02 +00003314 <= BCC(smb_buffer_response)) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003315 if (ses->serverOS)
Steve Frencha424f8b2006-05-30 18:06:04 +00003316 kfree(ses->serverOS);
Steve French77159b42007-08-31 01:10:17 +00003317 ses->serverOS = kzalloc(len + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 strncpy(ses->serverOS,bcc_ptr, len);
3319
3320 bcc_ptr += len;
3321 bcc_ptr[0] = 0; /* null terminate the string */
3322 bcc_ptr++;
3323
3324 len = strnlen(bcc_ptr, 1024);
Steve Frenchcd49b492006-06-26 04:22:36 +00003325 kfree(ses->serverNOS);
Steve French50c2f752007-07-13 00:33:32 +00003326 ses->serverNOS = kzalloc(len+1,
3327 GFP_KERNEL);
Steve French63135e02007-07-17 17:34:02 +00003328 strncpy(ses->serverNOS,
3329 bcc_ptr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 bcc_ptr += len;
3331 bcc_ptr[0] = 0;
3332 bcc_ptr++;
3333
3334 len = strnlen(bcc_ptr, 1024);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003335 if (ses->serverDomain)
Steve Frencha424f8b2006-05-30 18:06:04 +00003336 kfree(ses->serverDomain);
Steve French63135e02007-07-17 17:34:02 +00003337 ses->serverDomain =
3338 kzalloc(len+1,
3339 GFP_KERNEL);
3340 strncpy(ses->serverDomain,
3341 bcc_ptr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 bcc_ptr += len;
3343 bcc_ptr[0] = 0;
3344 bcc_ptr++;
3345 } else
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003346 cFYI(1, ("field of length %d "
Steve French63135e02007-07-17 17:34:02 +00003347 "extends beyond end of smb ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 len));
3349 }
3350 } else {
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003351 cERROR(1, ("Security Blob extends beyond end "
Steve French63135e02007-07-17 17:34:02 +00003352 "of SMB"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
3354 } else {
3355 cERROR(1, ("No session structure passed in."));
3356 }
3357 } else {
Cyrill Gorcunov6345a3a2007-10-16 17:57:55 +00003358 cERROR(1, ("Invalid Word count %d: ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 smb_buffer_response->WordCount));
3360 rc = -EIO;
3361 }
3362
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00003363 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
3365 return rc;
3366}
3367
3368int
3369CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
3370 const char *tree, struct cifsTconInfo *tcon,
3371 const struct nls_table *nls_codepage)
3372{
3373 struct smb_hdr *smb_buffer;
3374 struct smb_hdr *smb_buffer_response;
3375 TCONX_REQ *pSMB;
3376 TCONX_RSP *pSMBr;
3377 unsigned char *bcc_ptr;
3378 int rc = 0;
3379 int length;
3380 __u16 count;
3381
3382 if (ses == NULL)
3383 return -EIO;
3384
3385 smb_buffer = cifs_buf_get();
3386 if (smb_buffer == NULL) {
3387 return -ENOMEM;
3388 }
3389 smb_buffer_response = smb_buffer;
3390
3391 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3392 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07003393
3394 smb_buffer->Mid = GetNextMid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 smb_buffer->Uid = ses->Suid;
3396 pSMB = (TCONX_REQ *) smb_buffer;
3397 pSMBr = (TCONX_RSP *) smb_buffer_response;
3398
3399 pSMB->AndXCommand = 0xFF;
3400 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 bcc_ptr = &pSMB->Password[0];
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003402 if ((ses->server->secMode) & SECMODE_USER) {
Steve Frencheeac8042006-01-13 21:34:58 -08003403 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00003404 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08003405 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00003406 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08003407 } else {
Steve French7c7b25b2006-06-01 19:20:10 +00003408 pSMB->PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08003409 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
3410 specified as required (when that support is added to
3411 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00003412 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08003413 by Samba (not sure whether other servers allow
3414 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00003415#ifdef CONFIG_CIFS_WEAK_PW_HASH
Steve French50c2f752007-07-13 00:33:32 +00003416 if ((extended_security & CIFSSEC_MAY_LANMAN) &&
Steve French7c7b25b2006-06-01 19:20:10 +00003417 (ses->server->secType == LANMAN))
3418 calc_lanman_hash(ses, bcc_ptr);
3419 else
3420#endif /* CIFS_WEAK_PW_HASH */
Steve Frencheeac8042006-01-13 21:34:58 -08003421 SMBNTencrypt(ses->password,
3422 ses->server->cryptKey,
3423 bcc_ptr);
3424
Steve French7c7b25b2006-06-01 19:20:10 +00003425 bcc_ptr += CIFS_SESS_KEY_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003426 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00003427 /* must align unicode strings */
3428 *bcc_ptr = 0; /* null byte password */
3429 bcc_ptr++;
3430 }
Steve Frencheeac8042006-01-13 21:34:58 -08003431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
Steve French50c2f752007-07-13 00:33:32 +00003433 if (ses->server->secMode &
Steve Frencha878fb22006-05-30 18:04:19 +00003434 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3436
3437 if (ses->capabilities & CAP_STATUS32) {
3438 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3439 }
3440 if (ses->capabilities & CAP_DFS) {
3441 smb_buffer->Flags2 |= SMBFLG2_DFS;
3442 }
3443 if (ses->capabilities & CAP_UNICODE) {
3444 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3445 length =
Steve French50c2f752007-07-13 00:33:32 +00003446 cifs_strtoUCS((__le16 *) bcc_ptr, tree,
3447 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00003448 (/* server len*/ + 256 /* share len */), nls_codepage);
3449 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 bcc_ptr += 2; /* skip trailing null */
3451 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 strcpy(bcc_ptr, tree);
3453 bcc_ptr += strlen(tree) + 1;
3454 }
3455 strcpy(bcc_ptr, "?????");
3456 bcc_ptr += strlen("?????");
3457 bcc_ptr += 1;
3458 count = bcc_ptr - &pSMB->Password[0];
3459 pSMB->hdr.smb_buf_length += count;
3460 pSMB->ByteCount = cpu_to_le16(count);
3461
Steve French133672e2007-11-13 22:41:37 +00003462 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
3463 CIFS_STD_OP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
3465 /* if (rc) rc = map_smb_to_linux_error(smb_buffer_response); */
3466 /* above now done in SendReceive */
3467 if ((rc == 0) && (tcon != NULL)) {
3468 tcon->tidStatus = CifsGood;
3469 tcon->tid = smb_buffer_response->Tid;
3470 bcc_ptr = pByteArea(smb_buffer_response);
3471 length = strnlen(bcc_ptr, BCC(smb_buffer_response) - 2);
Steve French50c2f752007-07-13 00:33:32 +00003472 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00003473 if (length == 3) {
3474 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3475 (bcc_ptr[2] == 'C')) {
3476 cFYI(1, ("IPC connection"));
3477 tcon->ipc = 1;
3478 }
3479 } else if (length == 2) {
3480 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3481 /* the most common case */
3482 cFYI(1, ("disk share connection"));
3483 }
3484 }
Steve French50c2f752007-07-13 00:33:32 +00003485 bcc_ptr += length + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 strncpy(tcon->treeName, tree, MAX_TREE_SIZE);
3487 if (smb_buffer->Flags2 & SMBFLG2_UNICODE) {
3488 length = UniStrnlen((wchar_t *) bcc_ptr, 512);
3489 if ((bcc_ptr + (2 * length)) -
3490 pByteArea(smb_buffer_response) <=
3491 BCC(smb_buffer_response)) {
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003492 kfree(tcon->nativeFileSystem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 tcon->nativeFileSystem =
Pekka Enberge915fc42005-09-06 15:18:35 -07003494 kzalloc(length + 2, GFP_KERNEL);
Steve French88f370a2007-09-15 03:01:17 +00003495 if (tcon->nativeFileSystem)
3496 cifs_strfromUCS_le(
3497 tcon->nativeFileSystem,
3498 (__le16 *) bcc_ptr,
3499 length, nls_codepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 bcc_ptr += 2 * length;
3501 bcc_ptr[0] = 0; /* null terminate the string */
3502 bcc_ptr[1] = 0;
3503 bcc_ptr += 2;
3504 }
Steve French50c2f752007-07-13 00:33:32 +00003505 /* else do not bother copying these information fields*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 } else {
3507 length = strnlen(bcc_ptr, 1024);
3508 if ((bcc_ptr + length) -
3509 pByteArea(smb_buffer_response) <=
3510 BCC(smb_buffer_response)) {
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003511 kfree(tcon->nativeFileSystem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 tcon->nativeFileSystem =
Pekka Enberge915fc42005-09-06 15:18:35 -07003513 kzalloc(length + 1, GFP_KERNEL);
Steve French88f370a2007-09-15 03:01:17 +00003514 if (tcon->nativeFileSystem)
3515 strncpy(tcon->nativeFileSystem, bcc_ptr,
3516 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 }
Steve French50c2f752007-07-13 00:33:32 +00003518 /* else do not bother copying these information fields*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003520 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00003521 (smb_buffer_response->WordCount == 7))
3522 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00003523 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
3524 else
3525 tcon->Flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 cFYI(1, ("Tcon flags: 0x%x ", tcon->Flags));
3527 } else if ((rc == 0) && tcon == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003528 /* all we need to save for IPC$ connection */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 ses->ipc_tid = smb_buffer_response->Tid;
3530 }
3531
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00003532 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 return rc;
3534}
3535
3536int
3537cifs_umount(struct super_block *sb, struct cifs_sb_info *cifs_sb)
3538{
3539 int rc = 0;
3540 int xid;
3541 struct cifsSesInfo *ses = NULL;
3542 struct task_struct *cifsd_task;
Steve French50c2f752007-07-13 00:33:32 +00003543 char *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545 xid = GetXid();
3546
3547 if (cifs_sb->tcon) {
3548 ses = cifs_sb->tcon->ses; /* save ptr to ses before delete tcon!*/
3549 rc = CIFSSMBTDis(xid, cifs_sb->tcon);
3550 if (rc == -EBUSY) {
3551 FreeXid(xid);
3552 return 0;
3553 }
Steve French5d941ca2008-04-15 18:40:48 +00003554 DeleteTconOplockQEntries(cifs_sb->tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 tconInfoFree(cifs_sb->tcon);
3556 if ((ses) && (ses->server)) {
3557 /* save off task so we do not refer to ses later */
3558 cifsd_task = ses->server->tsk;
3559 cFYI(1, ("About to do SMBLogoff "));
3560 rc = CIFSSMBLogoff(xid, ses);
3561 if (rc == -EBUSY) {
3562 FreeXid(xid);
3563 return 0;
3564 } else if (rc == -ESHUTDOWN) {
Steve French467a8f82007-06-27 22:41:32 +00003565 cFYI(1, ("Waking up socket by sending signal"));
Jeff Layton469ee612008-10-16 18:46:39 +00003566 if (cifsd_task)
Steve French50c2f752007-07-13 00:33:32 +00003567 force_sig(SIGKILL, cifsd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 rc = 0;
3569 } /* else - we have an smb session
3570 left on this socket do not kill cifsd */
3571 } else
3572 cFYI(1, ("No session or bad tcon"));
3573 }
Steve French50c2f752007-07-13 00:33:32 +00003574
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 cifs_sb->tcon = NULL;
Steve French2fe87f02006-09-21 07:02:52 +00003576 tmp = cifs_sb->prepath;
3577 cifs_sb->prepathlen = 0;
3578 cifs_sb->prepath = NULL;
3579 kfree(tmp);
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07003580 if (ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 sesInfoFree(ses);
3582
3583 FreeXid(xid);
Steve French88e7d702008-01-03 17:37:09 +00003584 return rc;
Steve French50c2f752007-07-13 00:33:32 +00003585}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
3587int cifs_setup_session(unsigned int xid, struct cifsSesInfo *pSesInfo,
Steve French50c2f752007-07-13 00:33:32 +00003588 struct nls_table *nls_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589{
3590 int rc = 0;
Steve French7c7b25b2006-06-01 19:20:10 +00003591 char ntlm_session_key[CIFS_SESS_KEY_SIZE];
Steve French4b18f2a2008-04-29 00:06:05 +00003592 bool ntlmv2_flag = false;
Steve Frenchad009ac2005-04-28 22:41:05 -07003593 int first_time = 0;
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003594 struct TCP_Server_Info *server = pSesInfo->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
3596 /* what if server changes its buffer size after dropping the session? */
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003597 if (server->maxBuf == 0) /* no need to send on reconnect */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 rc = CIFSSMBNegotiate(xid, pSesInfo);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003599 if (rc == -EAGAIN) {
3600 /* retry only once on 1st time connection */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 rc = CIFSSMBNegotiate(xid, pSesInfo);
Steve French50c2f752007-07-13 00:33:32 +00003602 if (rc == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 rc = -EHOSTDOWN;
3604 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003605 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 spin_lock(&GlobalMid_Lock);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003607 if (server->tcpStatus != CifsExiting)
3608 server->tcpStatus = CifsGood;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 else
3610 rc = -EHOSTDOWN;
3611 spin_unlock(&GlobalMid_Lock);
3612
3613 }
Steve Frenchad009ac2005-04-28 22:41:05 -07003614 first_time = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 }
Steve French26b994f2008-08-06 05:11:33 +00003616
3617 if (rc)
3618 goto ss_err_exit;
3619
3620 pSesInfo->flags = 0;
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003621 pSesInfo->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00003622 if (linuxExtEnabled == 0)
3623 pSesInfo->capabilities &= (~CAP_UNIX);
Steve Frenchad009ac2005-04-28 22:41:05 -07003624 /* pSesInfo->sequence_number = 0;*/
Steve French26b994f2008-08-06 05:11:33 +00003625 cFYI(1, ("Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d",
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003626 server->secMode, server->capabilities, server->timeAdj));
3627
Steve French26b994f2008-08-06 05:11:33 +00003628 if (experimEnabled < 2)
3629 rc = CIFS_SessSetup(xid, pSesInfo, first_time, nls_info);
3630 else if (extended_security
3631 && (pSesInfo->capabilities & CAP_EXTENDED_SECURITY)
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003632 && (server->secType == NTLMSSP)) {
Steve French26b994f2008-08-06 05:11:33 +00003633 rc = -EOPNOTSUPP;
3634 } else if (extended_security
3635 && (pSesInfo->capabilities & CAP_EXTENDED_SECURITY)
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003636 && (server->secType == RawNTLMSSP)) {
Steve French26b994f2008-08-06 05:11:33 +00003637 cFYI(1, ("NTLMSSP sesssetup"));
3638 rc = CIFSNTLMSSPNegotiateSessSetup(xid, pSesInfo, &ntlmv2_flag,
3639 nls_info);
3640 if (!rc) {
3641 if (ntlmv2_flag) {
3642 char *v2_response;
3643 cFYI(1, ("more secure NTLM ver2 hash"));
3644 if (CalcNTLMv2_partial_mac_key(pSesInfo,
3645 nls_info)) {
3646 rc = -ENOMEM;
3647 goto ss_err_exit;
3648 } else
3649 v2_response = kmalloc(16 + 64 /* blob*/,
3650 GFP_KERNEL);
3651 if (v2_response) {
3652 CalcNTLMv2_response(pSesInfo,
3653 v2_response);
3654 /* if (first_time)
3655 cifs_calculate_ntlmv2_mac_key */
3656 kfree(v2_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 /* BB Put dummy sig in SessSetup PDU? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 } else {
Steve French26b994f2008-08-06 05:11:33 +00003659 rc = -ENOMEM;
3660 goto ss_err_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 }
Steve French26b994f2008-08-06 05:11:33 +00003662
3663 } else {
3664 SMBNTencrypt(pSesInfo->password,
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003665 server->cryptKey,
Steve French26b994f2008-08-06 05:11:33 +00003666 ntlm_session_key);
3667
3668 if (first_time)
3669 cifs_calculate_mac_key(
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003670 &server->mac_signing_key,
Steve French26b994f2008-08-06 05:11:33 +00003671 ntlm_session_key,
3672 pSesInfo->password);
3673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 /* for better security the weaker lanman hash not sent
3675 in AuthSessSetup so we no longer calculate it */
3676
Steve French26b994f2008-08-06 05:11:33 +00003677 rc = CIFSNTLMSSPAuthSessSetup(xid, pSesInfo,
3678 ntlm_session_key,
3679 ntlmv2_flag,
3680 nls_info);
3681 }
3682 } else { /* old style NTLM 0.12 session setup */
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003683 SMBNTencrypt(pSesInfo->password, server->cryptKey,
Steve French26b994f2008-08-06 05:11:33 +00003684 ntlm_session_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Steve French26b994f2008-08-06 05:11:33 +00003686 if (first_time)
Jeff Laytoncb7691b2008-08-18 15:41:05 -04003687 cifs_calculate_mac_key(&server->mac_signing_key,
3688 ntlm_session_key,
3689 pSesInfo->password);
Steve Frenchad009ac2005-04-28 22:41:05 -07003690
Steve French26b994f2008-08-06 05:11:33 +00003691 rc = CIFSSessSetup(xid, pSesInfo, ntlm_session_key, nls_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
Steve French26b994f2008-08-06 05:11:33 +00003693 if (rc) {
3694 cERROR(1, ("Send error in SessSetup = %d", rc));
3695 } else {
3696 cFYI(1, ("CIFS Session Established successfully"));
Jeff Layton469ee612008-10-16 18:46:39 +00003697 spin_lock(&GlobalMid_Lock);
Steve French26b994f2008-08-06 05:11:33 +00003698 pSesInfo->status = CifsGood;
Jeff Layton469ee612008-10-16 18:46:39 +00003699 spin_unlock(&GlobalMid_Lock);
Steve French26b994f2008-08-06 05:11:33 +00003700 }
3701
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702ss_err_exit:
3703 return rc;
3704}
3705