4 * Copyright (C) International Business Machines Corp., 2002,2011
5 * Author(s): Steve French (sfrench@us.ibm.com)
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.
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.
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
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/net.h>
23 #include <linux/string.h>
24 #include <linux/sched/signal.h>
25 #include <linux/list.h>
26 #include <linux/wait.h>
27 #include <linux/slab.h>
28 #include <linux/pagemap.h>
29 #include <linux/ctype.h>
30 #include <linux/utsname.h>
31 #include <linux/mempool.h>
32 #include <linux/delay.h>
33 #include <linux/completion.h>
34 #include <linux/kthread.h>
35 #include <linux/pagevec.h>
36 #include <linux/freezer.h>
37 #include <linux/namei.h>
38 #include <linux/uuid.h>
39 #include <linux/uaccess.h>
40 #include <asm/processor.h>
41 #include <linux/inet.h>
42 #include <linux/module.h>
43 #include <keys/user-type.h>
45 #include <linux/parser.h>
46 #include <linux/bvec.h>
50 #include "cifsproto.h"
51 #include "cifs_unicode.h"
52 #include "cifs_debug.h"
53 #include "cifs_fs_sb.h"
54 #include "dns_resolve.h"
57 #include "rfc1002pdu.h"
59 #include "smb2proto.h"
62 #define RFC1001_PORT 139
64 extern mempool_t *cifs_req_poolp;
66 /* FIXME: should these be tunable? */
67 #define TLINK_ERROR_EXPIRE (1 * HZ)
68 #define TLINK_IDLE_EXPIRE (600 * HZ)
71 /* Mount options that take no arguments */
72 Opt_user_xattr, Opt_nouser_xattr,
73 Opt_forceuid, Opt_noforceuid,
74 Opt_forcegid, Opt_noforcegid,
75 Opt_noblocksend, Opt_noautotune,
76 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
77 Opt_mapposix, Opt_nomapposix,
78 Opt_mapchars, Opt_nomapchars, Opt_sfu,
79 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
80 Opt_noposixpaths, Opt_nounix,
83 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
84 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
85 Opt_nohard, Opt_nosoft,
87 Opt_nostrictsync, Opt_strictsync,
88 Opt_serverino, Opt_noserverino,
89 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
90 Opt_acl, Opt_noacl, Opt_locallease,
91 Opt_sign, Opt_seal, Opt_noac,
92 Opt_fsc, Opt_mfsymlinks,
93 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
94 Opt_persistent, Opt_nopersistent,
95 Opt_resilient, Opt_noresilient,
98 /* Mount options which take numeric value */
99 Opt_backupuid, Opt_backupgid, Opt_uid,
100 Opt_cruid, Opt_gid, Opt_file_mode,
101 Opt_dirmode, Opt_port,
102 Opt_rsize, Opt_wsize, Opt_actimeo,
103 Opt_echo_interval, Opt_max_credits,
106 /* Mount options which take string value */
107 Opt_user, Opt_pass, Opt_ip,
108 Opt_domain, Opt_srcaddr, Opt_iocharset,
109 Opt_netbiosname, Opt_servern,
110 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
112 /* Mount options to be ignored */
115 /* Options which could be blank */
123 static const match_table_t cifs_mount_option_tokens = {
125 { Opt_user_xattr, "user_xattr" },
126 { Opt_nouser_xattr, "nouser_xattr" },
127 { Opt_forceuid, "forceuid" },
128 { Opt_noforceuid, "noforceuid" },
129 { Opt_forcegid, "forcegid" },
130 { Opt_noforcegid, "noforcegid" },
131 { Opt_noblocksend, "noblocksend" },
132 { Opt_noautotune, "noautotune" },
133 { Opt_hard, "hard" },
134 { Opt_soft, "soft" },
135 { Opt_perm, "perm" },
136 { Opt_noperm, "noperm" },
137 { Opt_mapchars, "mapchars" }, /* SFU style */
138 { Opt_nomapchars, "nomapchars" },
139 { Opt_mapposix, "mapposix" }, /* SFM style */
140 { Opt_nomapposix, "nomapposix" },
142 { Opt_nosfu, "nosfu" },
143 { Opt_nodfs, "nodfs" },
144 { Opt_posixpaths, "posixpaths" },
145 { Opt_noposixpaths, "noposixpaths" },
146 { Opt_nounix, "nounix" },
147 { Opt_nounix, "nolinux" },
148 { Opt_nocase, "nocase" },
149 { Opt_nocase, "ignorecase" },
151 { Opt_nobrl, "nobrl" },
152 { Opt_nobrl, "nolock" },
153 { Opt_forcemandatorylock, "forcemandatorylock" },
154 { Opt_forcemandatorylock, "forcemand" },
155 { Opt_setuids, "setuids" },
156 { Opt_nosetuids, "nosetuids" },
157 { Opt_setuidfromacl, "idsfromsid" },
158 { Opt_dynperm, "dynperm" },
159 { Opt_nodynperm, "nodynperm" },
160 { Opt_nohard, "nohard" },
161 { Opt_nosoft, "nosoft" },
162 { Opt_nointr, "nointr" },
163 { Opt_intr, "intr" },
164 { Opt_nostrictsync, "nostrictsync" },
165 { Opt_strictsync, "strictsync" },
166 { Opt_serverino, "serverino" },
167 { Opt_noserverino, "noserverino" },
168 { Opt_rwpidforward, "rwpidforward" },
169 { Opt_cifsacl, "cifsacl" },
170 { Opt_nocifsacl, "nocifsacl" },
172 { Opt_noacl, "noacl" },
173 { Opt_locallease, "locallease" },
174 { Opt_sign, "sign" },
175 { Opt_seal, "seal" },
176 { Opt_noac, "noac" },
178 { Opt_mfsymlinks, "mfsymlinks" },
179 { Opt_multiuser, "multiuser" },
180 { Opt_sloppy, "sloppy" },
181 { Opt_nosharesock, "nosharesock" },
182 { Opt_persistent, "persistenthandles"},
183 { Opt_nopersistent, "nopersistenthandles"},
184 { Opt_resilient, "resilienthandles"},
185 { Opt_noresilient, "noresilienthandles"},
186 { Opt_domainauto, "domainauto"},
188 { Opt_backupuid, "backupuid=%s" },
189 { Opt_backupgid, "backupgid=%s" },
190 { Opt_uid, "uid=%s" },
191 { Opt_cruid, "cruid=%s" },
192 { Opt_gid, "gid=%s" },
193 { Opt_file_mode, "file_mode=%s" },
194 { Opt_dirmode, "dirmode=%s" },
195 { Opt_dirmode, "dir_mode=%s" },
196 { Opt_port, "port=%s" },
197 { Opt_rsize, "rsize=%s" },
198 { Opt_wsize, "wsize=%s" },
199 { Opt_actimeo, "actimeo=%s" },
200 { Opt_echo_interval, "echo_interval=%s" },
201 { Opt_max_credits, "max_credits=%s" },
202 { Opt_snapshot, "snapshot=%s" },
204 { Opt_blank_user, "user=" },
205 { Opt_blank_user, "username=" },
206 { Opt_user, "user=%s" },
207 { Opt_user, "username=%s" },
208 { Opt_blank_pass, "pass=" },
209 { Opt_blank_pass, "password=" },
210 { Opt_pass, "pass=%s" },
211 { Opt_pass, "password=%s" },
212 { Opt_blank_ip, "ip=" },
213 { Opt_blank_ip, "addr=" },
215 { Opt_ip, "addr=%s" },
216 { Opt_ignore, "unc=%s" },
217 { Opt_ignore, "target=%s" },
218 { Opt_ignore, "path=%s" },
219 { Opt_domain, "dom=%s" },
220 { Opt_domain, "domain=%s" },
221 { Opt_domain, "workgroup=%s" },
222 { Opt_srcaddr, "srcaddr=%s" },
223 { Opt_ignore, "prefixpath=%s" },
224 { Opt_iocharset, "iocharset=%s" },
225 { Opt_netbiosname, "netbiosname=%s" },
226 { Opt_servern, "servern=%s" },
227 { Opt_ver, "ver=%s" },
228 { Opt_vers, "vers=%s" },
229 { Opt_sec, "sec=%s" },
230 { Opt_cache, "cache=%s" },
232 { Opt_ignore, "cred" },
233 { Opt_ignore, "credentials" },
234 { Opt_ignore, "cred=%s" },
235 { Opt_ignore, "credentials=%s" },
236 { Opt_ignore, "guest" },
237 { Opt_ignore, "rw" },
238 { Opt_ignore, "ro" },
239 { Opt_ignore, "suid" },
240 { Opt_ignore, "nosuid" },
241 { Opt_ignore, "exec" },
242 { Opt_ignore, "noexec" },
243 { Opt_ignore, "nodev" },
244 { Opt_ignore, "noauto" },
245 { Opt_ignore, "dev" },
246 { Opt_ignore, "mand" },
247 { Opt_ignore, "nomand" },
248 { Opt_ignore, "_netdev" },
254 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
255 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
256 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
257 Opt_sec_ntlmv2i, Opt_sec_lanman,
263 static const match_table_t cifs_secflavor_tokens = {
264 { Opt_sec_krb5, "krb5" },
265 { Opt_sec_krb5i, "krb5i" },
266 { Opt_sec_krb5p, "krb5p" },
267 { Opt_sec_ntlmsspi, "ntlmsspi" },
268 { Opt_sec_ntlmssp, "ntlmssp" },
269 { Opt_ntlm, "ntlm" },
270 { Opt_sec_ntlmi, "ntlmi" },
271 { Opt_sec_ntlmv2, "nontlm" },
272 { Opt_sec_ntlmv2, "ntlmv2" },
273 { Opt_sec_ntlmv2i, "ntlmv2i" },
274 { Opt_sec_lanman, "lanman" },
275 { Opt_sec_none, "none" },
277 { Opt_sec_err, NULL }
288 static const match_table_t cifs_cacheflavor_tokens = {
289 { Opt_cache_loose, "loose" },
290 { Opt_cache_strict, "strict" },
291 { Opt_cache_none, "none" },
292 { Opt_cache_err, NULL }
295 static const match_table_t cifs_smb_version_tokens = {
296 { Smb_1, SMB1_VERSION_STRING },
297 { Smb_20, SMB20_VERSION_STRING},
298 { Smb_21, SMB21_VERSION_STRING },
299 { Smb_30, SMB30_VERSION_STRING },
300 { Smb_302, SMB302_VERSION_STRING },
301 #ifdef CONFIG_CIFS_SMB311
302 { Smb_311, SMB311_VERSION_STRING },
303 { Smb_311, ALT_SMB311_VERSION_STRING },
305 { Smb_3any, SMB3ANY_VERSION_STRING },
306 { Smb_default, SMBDEFAULT_VERSION_STRING },
307 { Smb_version_err, NULL }
310 static int ip_connect(struct TCP_Server_Info *server);
311 static int generic_ip_connect(struct TCP_Server_Info *server);
312 static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
313 static void cifs_prune_tlinks(struct work_struct *work);
314 static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
315 const char *devname);
318 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
319 * get their ip addresses changed at some point.
321 * This should be called with server->srv_mutex held.
323 #ifdef CONFIG_CIFS_DFS_UPCALL
324 static int reconn_set_ipaddr(struct TCP_Server_Info *server)
328 char *unc, *ipaddr = NULL;
330 if (!server->hostname)
333 len = strlen(server->hostname) + 3;
335 unc = kmalloc(len, GFP_KERNEL);
337 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
340 snprintf(unc, len, "\\\\%s", server->hostname);
342 rc = dns_resolve_server_name_to_ip(unc, &ipaddr);
346 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
347 __func__, server->hostname, rc);
351 spin_lock(&cifs_tcp_ses_lock);
352 rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
354 spin_unlock(&cifs_tcp_ses_lock);
360 static inline int reconn_set_ipaddr(struct TCP_Server_Info *server)
367 * cifs tcp session reconnection
369 * mark tcp session as reconnecting so temporarily locked
370 * mark all smb sessions as reconnecting for tcp session
371 * reconnect tcp session
372 * wake up waiters on reconnection? - (not needed currently)
375 cifs_reconnect(struct TCP_Server_Info *server)
378 struct list_head *tmp, *tmp2;
379 struct cifs_ses *ses;
380 struct cifs_tcon *tcon;
381 struct mid_q_entry *mid_entry;
382 struct list_head retry_list;
384 spin_lock(&GlobalMid_Lock);
385 if (server->tcpStatus == CifsExiting) {
386 /* the demux thread will exit normally
387 next time through the loop */
388 spin_unlock(&GlobalMid_Lock);
391 server->tcpStatus = CifsNeedReconnect;
392 spin_unlock(&GlobalMid_Lock);
394 server->max_read = 0;
396 cifs_dbg(FYI, "Reconnecting tcp session\n");
398 /* before reconnecting the tcp session, mark the smb session (uid)
399 and the tid bad so they are not used until reconnected */
400 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
402 spin_lock(&cifs_tcp_ses_lock);
403 list_for_each(tmp, &server->smb_ses_list) {
404 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
405 ses->need_reconnect = true;
407 list_for_each(tmp2, &ses->tcon_list) {
408 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
409 tcon->need_reconnect = true;
412 spin_unlock(&cifs_tcp_ses_lock);
414 /* do not want to be sending data on a socket we are freeing */
415 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
416 mutex_lock(&server->srv_mutex);
417 if (server->ssocket) {
418 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
419 server->ssocket->state, server->ssocket->flags);
420 kernel_sock_shutdown(server->ssocket, SHUT_WR);
421 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
422 server->ssocket->state, server->ssocket->flags);
423 sock_release(server->ssocket);
424 server->ssocket = NULL;
426 server->sequence_number = 0;
427 server->session_estab = false;
428 kfree(server->session_key.response);
429 server->session_key.response = NULL;
430 server->session_key.len = 0;
431 server->lstrp = jiffies;
433 /* mark submitted MIDs for retry and issue callback */
434 INIT_LIST_HEAD(&retry_list);
435 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
436 spin_lock(&GlobalMid_Lock);
437 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
438 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
439 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
440 mid_entry->mid_state = MID_RETRY_NEEDED;
441 list_move(&mid_entry->qhead, &retry_list);
443 spin_unlock(&GlobalMid_Lock);
444 mutex_unlock(&server->srv_mutex);
446 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
447 list_for_each_safe(tmp, tmp2, &retry_list) {
448 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
449 list_del_init(&mid_entry->qhead);
450 mid_entry->callback(mid_entry);
456 /* we should try only the port we connected to before */
457 mutex_lock(&server->srv_mutex);
458 rc = generic_ip_connect(server);
460 cifs_dbg(FYI, "reconnect error %d\n", rc);
461 rc = reconn_set_ipaddr(server);
463 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
466 mutex_unlock(&server->srv_mutex);
469 atomic_inc(&tcpSesReconnectCount);
470 spin_lock(&GlobalMid_Lock);
471 if (server->tcpStatus != CifsExiting)
472 server->tcpStatus = CifsNeedNegotiate;
473 spin_unlock(&GlobalMid_Lock);
474 mutex_unlock(&server->srv_mutex);
476 } while (server->tcpStatus == CifsNeedReconnect);
478 if (server->tcpStatus == CifsNeedNegotiate)
479 mod_delayed_work(cifsiod_wq, &server->echo, 0);
485 cifs_echo_request(struct work_struct *work)
488 struct TCP_Server_Info *server = container_of(work,
489 struct TCP_Server_Info, echo.work);
490 unsigned long echo_interval;
493 * If we need to renegotiate, set echo interval to zero to
494 * immediately call echo service where we can renegotiate.
496 if (server->tcpStatus == CifsNeedNegotiate)
499 echo_interval = server->echo_interval;
502 * We cannot send an echo if it is disabled.
503 * Also, no need to ping if we got a response recently.
506 if (server->tcpStatus == CifsNeedReconnect ||
507 server->tcpStatus == CifsExiting ||
508 server->tcpStatus == CifsNew ||
509 (server->ops->can_echo && !server->ops->can_echo(server)) ||
510 time_before(jiffies, server->lstrp + echo_interval - HZ))
513 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
515 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
519 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
523 allocate_buffers(struct TCP_Server_Info *server)
525 if (!server->bigbuf) {
526 server->bigbuf = (char *)cifs_buf_get();
527 if (!server->bigbuf) {
528 cifs_dbg(VFS, "No memory for large SMB response\n");
530 /* retry will check if exiting */
533 } else if (server->large_buf) {
534 /* we are reusing a dirty large buf, clear its start */
535 memset(server->bigbuf, 0, HEADER_SIZE(server));
538 if (!server->smallbuf) {
539 server->smallbuf = (char *)cifs_small_buf_get();
540 if (!server->smallbuf) {
541 cifs_dbg(VFS, "No memory for SMB response\n");
543 /* retry will check if exiting */
546 /* beginning of smb buffer is cleared in our buf_get */
548 /* if existing small buf clear beginning */
549 memset(server->smallbuf, 0, HEADER_SIZE(server));
556 server_unresponsive(struct TCP_Server_Info *server)
559 * We need to wait 3 echo intervals to make sure we handle such
561 * 1s client sends a normal SMB request
562 * 3s client gets a response
563 * 30s echo workqueue job pops, and decides we got a response recently
564 * and don't need to send another
566 * 65s kernel_recvmsg times out, and we see that we haven't gotten
567 * a response in >60s.
569 if ((server->tcpStatus == CifsGood ||
570 server->tcpStatus == CifsNeedNegotiate) &&
571 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
572 cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
573 server->hostname, (3 * server->echo_interval) / HZ);
574 cifs_reconnect(server);
575 wake_up(&server->response_q);
583 zero_credits(struct TCP_Server_Info *server)
587 spin_lock(&server->req_lock);
588 val = server->credits + server->echo_credits + server->oplock_credits;
589 if (server->in_flight == 0 && val == 0) {
590 spin_unlock(&server->req_lock);
593 spin_unlock(&server->req_lock);
598 cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
603 smb_msg->msg_control = NULL;
604 smb_msg->msg_controllen = 0;
606 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
609 /* reconnect if no credits and no requests in flight */
610 if (zero_credits(server)) {
611 cifs_reconnect(server);
612 return -ECONNABORTED;
615 if (server_unresponsive(server))
616 return -ECONNABORTED;
618 length = sock_recvmsg(server->ssocket, smb_msg, 0);
620 if (server->tcpStatus == CifsExiting)
623 if (server->tcpStatus == CifsNeedReconnect) {
624 cifs_reconnect(server);
625 return -ECONNABORTED;
628 if (length == -ERESTARTSYS ||
632 * Minimum sleep to prevent looping, allowing socket
633 * to clear and app threads to set tcpStatus
634 * CifsNeedReconnect if server hung.
636 usleep_range(1000, 2000);
642 cifs_dbg(FYI, "Received no data or error: %d\n", length);
643 cifs_reconnect(server);
644 return -ECONNABORTED;
651 cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
652 unsigned int to_read)
654 struct msghdr smb_msg;
655 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
656 iov_iter_kvec(&smb_msg.msg_iter, READ | ITER_KVEC, &iov, 1, to_read);
658 return cifs_readv_from_socket(server, &smb_msg);
662 cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
663 unsigned int to_read)
665 struct msghdr smb_msg;
666 struct bio_vec bv = {.bv_page = page, .bv_len = to_read};
667 iov_iter_bvec(&smb_msg.msg_iter, READ | ITER_BVEC, &bv, 1, to_read);
668 return cifs_readv_from_socket(server, &smb_msg);
672 is_smb_response(struct TCP_Server_Info *server, unsigned char type)
675 * The first byte big endian of the length field,
676 * is actually not part of the length but the type
677 * with the most common, zero, as regular data.
680 case RFC1002_SESSION_MESSAGE:
681 /* Regular SMB response */
683 case RFC1002_SESSION_KEEP_ALIVE:
684 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
686 case RFC1002_POSITIVE_SESSION_RESPONSE:
687 cifs_dbg(FYI, "RFC 1002 positive session response\n");
689 case RFC1002_NEGATIVE_SESSION_RESPONSE:
691 * We get this from Windows 98 instead of an error on
692 * SMB negprot response.
694 cifs_dbg(FYI, "RFC 1002 negative session response\n");
695 /* give server a second to clean up */
698 * Always try 445 first on reconnect since we get NACK
699 * on some if we ever connected to port 139 (the NACK
700 * is since we do not begin with RFC1001 session
703 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
704 cifs_reconnect(server);
705 wake_up(&server->response_q);
708 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
709 cifs_reconnect(server);
716 dequeue_mid(struct mid_q_entry *mid, bool malformed)
718 #ifdef CONFIG_CIFS_STATS2
719 mid->when_received = jiffies;
721 spin_lock(&GlobalMid_Lock);
723 mid->mid_state = MID_RESPONSE_RECEIVED;
725 mid->mid_state = MID_RESPONSE_MALFORMED;
726 list_del_init(&mid->qhead);
727 spin_unlock(&GlobalMid_Lock);
731 handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
732 char *buf, int malformed)
734 if (server->ops->check_trans2 &&
735 server->ops->check_trans2(mid, server, buf, malformed))
738 mid->large_buf = server->large_buf;
739 /* Was previous buf put in mpx struct for multi-rsp? */
740 if (!mid->multiRsp) {
741 /* smb buffer will be freed by user thread */
742 if (server->large_buf)
743 server->bigbuf = NULL;
745 server->smallbuf = NULL;
747 dequeue_mid(mid, malformed);
750 static void clean_demultiplex_info(struct TCP_Server_Info *server)
754 /* take it off the list, if it's not already */
755 spin_lock(&cifs_tcp_ses_lock);
756 list_del_init(&server->tcp_ses_list);
757 spin_unlock(&cifs_tcp_ses_lock);
759 cancel_delayed_work_sync(&server->echo);
761 spin_lock(&GlobalMid_Lock);
762 server->tcpStatus = CifsExiting;
763 spin_unlock(&GlobalMid_Lock);
764 wake_up_all(&server->response_q);
766 /* check if we have blocked requests that need to free */
767 spin_lock(&server->req_lock);
768 if (server->credits <= 0)
770 spin_unlock(&server->req_lock);
772 * Although there should not be any requests blocked on this queue it
773 * can not hurt to be paranoid and try to wake up requests that may
774 * haven been blocked when more than 50 at time were on the wire to the
775 * same server - they now will see the session is in exit state and get
776 * out of SendReceive.
778 wake_up_all(&server->request_q);
779 /* give those requests time to exit */
782 if (server->ssocket) {
783 sock_release(server->ssocket);
784 server->ssocket = NULL;
787 if (!list_empty(&server->pending_mid_q)) {
788 struct list_head dispose_list;
789 struct mid_q_entry *mid_entry;
790 struct list_head *tmp, *tmp2;
792 INIT_LIST_HEAD(&dispose_list);
793 spin_lock(&GlobalMid_Lock);
794 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
795 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
796 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
797 mid_entry->mid_state = MID_SHUTDOWN;
798 list_move(&mid_entry->qhead, &dispose_list);
800 spin_unlock(&GlobalMid_Lock);
802 /* now walk dispose list and issue callbacks */
803 list_for_each_safe(tmp, tmp2, &dispose_list) {
804 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
805 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
806 list_del_init(&mid_entry->qhead);
807 mid_entry->callback(mid_entry);
809 /* 1/8th of sec is more than enough time for them to exit */
813 if (!list_empty(&server->pending_mid_q)) {
815 * mpx threads have not exited yet give them at least the smb
816 * send timeout time for long ops.
818 * Due to delays on oplock break requests, we need to wait at
819 * least 45 seconds before giving up on a request getting a
820 * response and going ahead and killing cifsd.
822 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
825 * If threads still have not exited they are probably never
826 * coming home not much else we can do but free the memory.
830 kfree(server->hostname);
833 length = atomic_dec_return(&tcpSesAllocCount);
835 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
839 standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
842 char *buf = server->smallbuf;
843 unsigned int pdu_length = get_rfc1002_length(buf);
845 /* make sure this will fit in a large buffer */
846 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) - 4) {
847 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
848 cifs_reconnect(server);
849 wake_up(&server->response_q);
850 return -ECONNABORTED;
853 /* switch to large buffer if too big for a small one */
854 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
855 server->large_buf = true;
856 memcpy(server->bigbuf, buf, server->total_read);
857 buf = server->bigbuf;
860 /* now read the rest */
861 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
862 pdu_length - HEADER_SIZE(server) + 1 + 4);
865 server->total_read += length;
867 dump_smb(buf, server->total_read);
869 return cifs_handle_standard(server, mid);
873 cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
875 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
879 * We know that we received enough to get to the MID as we
880 * checked the pdu_length earlier. Now check to see
881 * if the rest of the header is OK. We borrow the length
882 * var for the rest of the loop to avoid a new stack var.
884 * 48 bytes is enough to display the header and a little bit
885 * into the payload for debugging purposes.
887 length = server->ops->check_message(buf, server->total_read, server);
889 cifs_dump_mem("Bad SMB: ", buf,
890 min_t(unsigned int, server->total_read, 48));
892 if (server->ops->is_session_expired &&
893 server->ops->is_session_expired(buf)) {
894 cifs_reconnect(server);
895 wake_up(&server->response_q);
899 if (server->ops->is_status_pending &&
900 server->ops->is_status_pending(buf, server, length))
906 handle_mid(mid, server, buf, length);
911 cifs_demultiplex_thread(void *p)
914 struct TCP_Server_Info *server = p;
915 unsigned int pdu_length;
917 struct task_struct *task_to_wake = NULL;
918 struct mid_q_entry *mid_entry;
920 current->flags |= PF_MEMALLOC;
921 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
923 length = atomic_inc_return(&tcpSesAllocCount);
925 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
928 allow_kernel_signal(SIGKILL);
929 while (server->tcpStatus != CifsExiting) {
933 if (!allocate_buffers(server))
936 server->large_buf = false;
937 buf = server->smallbuf;
938 pdu_length = 4; /* enough to get RFC1001 header */
940 length = cifs_read_from_socket(server, buf, pdu_length);
943 server->total_read = length;
946 * The right amount was read from socket - 4 bytes,
947 * so we can now interpret the length field.
949 pdu_length = get_rfc1002_length(buf);
951 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
952 if (!is_smb_response(server, buf[0]))
955 /* make sure we have enough to get to the MID */
956 if (pdu_length < HEADER_SIZE(server) - 1 - 4) {
957 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
959 cifs_reconnect(server);
960 wake_up(&server->response_q);
964 /* read down to the MID */
965 length = cifs_read_from_socket(server, buf + 4,
966 HEADER_SIZE(server) - 1 - 4);
969 server->total_read += length;
972 if (server->ops->is_transform_hdr &&
973 server->ops->receive_transform &&
974 server->ops->is_transform_hdr(buf)) {
975 length = server->ops->receive_transform(server,
978 mid_entry = server->ops->find_mid(server, buf);
980 if (!mid_entry || !mid_entry->receive)
981 length = standard_receive3(server, mid_entry);
983 length = mid_entry->receive(server, mid_entry);
988 cifs_mid_q_entry_release(mid_entry);
992 if (server->large_buf)
993 buf = server->bigbuf;
995 server->lstrp = jiffies;
996 if (mid_entry != NULL) {
997 if ((mid_entry->mid_flags & MID_WAIT_CANCELLED) &&
998 mid_entry->mid_state == MID_RESPONSE_RECEIVED &&
999 server->ops->handle_cancelled_mid)
1000 server->ops->handle_cancelled_mid(
1001 mid_entry->resp_buf,
1004 if (!mid_entry->multiRsp || mid_entry->multiEnd)
1005 mid_entry->callback(mid_entry);
1007 cifs_mid_q_entry_release(mid_entry);
1008 } else if (server->ops->is_oplock_break &&
1009 server->ops->is_oplock_break(buf, server)) {
1010 cifs_dbg(FYI, "Received oplock break\n");
1012 cifs_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
1013 atomic_read(&midCount));
1014 cifs_dump_mem("Received Data is: ", buf,
1015 HEADER_SIZE(server));
1016 #ifdef CONFIG_CIFS_DEBUG2
1017 if (server->ops->dump_detail)
1018 server->ops->dump_detail(buf);
1019 cifs_dump_mids(server);
1020 #endif /* CIFS_DEBUG2 */
1023 } /* end while !EXITING */
1025 /* buffer usually freed in free_mid - need to free it here on exit */
1026 cifs_buf_release(server->bigbuf);
1027 if (server->smallbuf) /* no sense logging a debug message if NULL */
1028 cifs_small_buf_release(server->smallbuf);
1030 task_to_wake = xchg(&server->tsk, NULL);
1031 clean_demultiplex_info(server);
1033 /* if server->tsk was NULL then wait for a signal before exiting */
1034 if (!task_to_wake) {
1035 set_current_state(TASK_INTERRUPTIBLE);
1036 while (!signal_pending(current)) {
1038 set_current_state(TASK_INTERRUPTIBLE);
1040 set_current_state(TASK_RUNNING);
1043 module_put_and_exit(0);
1046 /* extract the host portion of the UNC string */
1048 extract_hostname(const char *unc)
1054 /* skip double chars at beginning of string */
1055 /* BB: check validity of these bytes? */
1058 /* delimiter between hostname and sharename is always '\\' now */
1059 delim = strchr(src, '\\');
1061 return ERR_PTR(-EINVAL);
1064 dst = kmalloc((len + 1), GFP_KERNEL);
1066 return ERR_PTR(-ENOMEM);
1068 memcpy(dst, src, len);
1074 static int get_option_ul(substring_t args[], unsigned long *option)
1079 string = match_strdup(args);
1082 rc = kstrtoul(string, 0, option);
1088 static int get_option_uid(substring_t args[], kuid_t *result)
1090 unsigned long value;
1094 rc = get_option_ul(args, &value);
1098 uid = make_kuid(current_user_ns(), value);
1099 if (!uid_valid(uid))
1106 static int get_option_gid(substring_t args[], kgid_t *result)
1108 unsigned long value;
1112 rc = get_option_ul(args, &value);
1116 gid = make_kgid(current_user_ns(), value);
1117 if (!gid_valid(gid))
1124 static int cifs_parse_security_flavors(char *value,
1125 struct smb_vol *vol)
1128 substring_t args[MAX_OPT_ARGS];
1131 * With mount options, the last one should win. Reset any existing
1132 * settings back to default.
1134 vol->sectype = Unspecified;
1137 switch (match_token(value, cifs_secflavor_tokens, args)) {
1139 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1145 vol->sectype = Kerberos;
1147 case Opt_sec_ntlmsspi:
1150 case Opt_sec_ntlmssp:
1151 vol->sectype = RawNTLMSSP;
1157 vol->sectype = NTLM;
1159 case Opt_sec_ntlmv2i:
1162 case Opt_sec_ntlmv2:
1163 vol->sectype = NTLMv2;
1165 #ifdef CONFIG_CIFS_WEAK_PW_HASH
1166 case Opt_sec_lanman:
1167 vol->sectype = LANMAN;
1174 cifs_dbg(VFS, "bad security option: %s\n", value);
1182 cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1184 substring_t args[MAX_OPT_ARGS];
1186 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1187 case Opt_cache_loose:
1188 vol->direct_io = false;
1189 vol->strict_io = false;
1191 case Opt_cache_strict:
1192 vol->direct_io = false;
1193 vol->strict_io = true;
1195 case Opt_cache_none:
1196 vol->direct_io = true;
1197 vol->strict_io = false;
1200 cifs_dbg(VFS, "bad cache= option: %s\n", value);
1207 cifs_parse_smb_version(char *value, struct smb_vol *vol)
1209 substring_t args[MAX_OPT_ARGS];
1211 switch (match_token(value, cifs_smb_version_tokens, args)) {
1212 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1214 vol->ops = &smb1_operations;
1215 vol->vals = &smb1_values;
1218 vol->ops = &smb20_operations;
1219 vol->vals = &smb20_values;
1223 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1226 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1228 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
1230 vol->ops = &smb21_operations;
1231 vol->vals = &smb21_values;
1234 vol->ops = &smb30_operations;
1235 vol->vals = &smb30_values;
1238 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1239 vol->vals = &smb302_values;
1241 #ifdef CONFIG_CIFS_SMB311
1243 vol->ops = &smb311_operations;
1244 vol->vals = &smb311_values;
1248 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1249 vol->vals = &smb3any_values;
1252 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1253 vol->vals = &smbdefault_values;
1256 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
1263 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1264 * fields with the result. Returns 0 on success and an error otherwise.
1267 cifs_parse_devname(const char *devname, struct smb_vol *vol)
1270 const char *delims = "/\\";
1273 if (unlikely(!devname || !*devname)) {
1274 cifs_dbg(VFS, "Device name not specified.\n");
1278 /* make sure we have a valid UNC double delimiter prefix */
1279 len = strspn(devname, delims);
1283 /* find delimiter between host and sharename */
1284 pos = strpbrk(devname + 2, delims);
1288 /* skip past delimiter */
1291 /* now go until next delimiter or end of string */
1292 len = strcspn(pos, delims);
1294 /* move "pos" up to delimiter or NULL */
1296 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1300 convert_delimiter(vol->UNC, '\\');
1302 /* skip any delimiter */
1303 if (*pos == '/' || *pos == '\\')
1306 /* If pos is NULL then no prepath */
1310 vol->prepath = kstrdup(pos, GFP_KERNEL);
1318 cifs_parse_mount_options(const char *mountdata, const char *devname,
1319 struct smb_vol *vol)
1322 char *mountdata_copy = NULL, *options;
1323 unsigned int temp_len, i, j;
1325 short int override_uid = -1;
1326 short int override_gid = -1;
1327 bool uid_specified = false;
1328 bool gid_specified = false;
1329 bool sloppy = false;
1330 char *invalid = NULL;
1331 char *nodename = utsname()->nodename;
1332 char *string = NULL;
1333 char *tmp_end, *value;
1335 bool got_ip = false;
1336 bool got_version = false;
1337 unsigned short port = 0;
1338 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
1342 delim = separator[0];
1344 /* ensure we always start with zeroed-out smb_vol */
1345 memset(vol, 0, sizeof(*vol));
1348 * does not have to be perfect mapping since field is
1349 * informational, only used for servers that do not support
1350 * port 445 and it can be overridden at mount time
1352 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1353 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
1354 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1356 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
1357 /* null target name indicates to use *SMBSERVR default called name
1358 if we end up sending RFC1001 session initialize */
1359 vol->target_rfc1001_name[0] = 0;
1360 vol->cred_uid = current_uid();
1361 vol->linux_uid = current_uid();
1362 vol->linux_gid = current_gid();
1365 * default to SFM style remapping of seven reserved characters
1366 * unless user overrides it or we negotiate CIFS POSIX where
1367 * it is unnecessary. Can not simultaneously use more than one mapping
1368 * since then readdir could list files that open could not open
1372 /* default to only allowing write access to owner of the mount */
1373 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
1375 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
1376 /* default is always to request posix paths. */
1377 vol->posix_paths = 1;
1378 /* default to using server inode numbers where available */
1379 vol->server_ino = 1;
1381 /* default is to use strict cifs caching semantics */
1382 vol->strict_io = true;
1384 vol->actimeo = CIFS_DEF_ACTIMEO;
1386 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1387 vol->ops = &smb30_operations;
1388 vol->vals = &smbdefault_values;
1390 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1393 goto cifs_parse_mount_err;
1395 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1396 if (!mountdata_copy)
1397 goto cifs_parse_mount_err;
1399 options = mountdata_copy;
1400 end = options + strlen(options);
1402 if (strncmp(options, "sep=", 4) == 0) {
1403 if (options[4] != 0) {
1404 separator[0] = options[4];
1407 cifs_dbg(FYI, "Null separator not allowed\n");
1410 vol->backupuid_specified = false; /* no backup intent for a user */
1411 vol->backupgid_specified = false; /* no backup intent for a group */
1413 switch (cifs_parse_devname(devname, vol)) {
1417 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1418 goto cifs_parse_mount_err;
1420 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1421 goto cifs_parse_mount_err;
1423 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1424 goto cifs_parse_mount_err;
1427 while ((data = strsep(&options, separator)) != NULL) {
1428 substring_t args[MAX_OPT_ARGS];
1429 unsigned long option;
1435 token = match_token(data, cifs_mount_option_tokens, args);
1439 /* Ingnore the following */
1443 /* Boolean values */
1444 case Opt_user_xattr:
1447 case Opt_nouser_xattr:
1453 case Opt_noforceuid:
1459 case Opt_noforcegid:
1462 case Opt_noblocksend:
1463 vol->noblocksnd = 1;
1465 case Opt_noautotune:
1466 vol->noautotune = 1;
1481 vol->sfu_remap = true;
1482 vol->remap = false; /* disable SFM mapping */
1484 case Opt_nomapchars:
1485 vol->sfu_remap = false;
1489 vol->sfu_remap = false; /* disable SFU mapping */
1491 case Opt_nomapposix:
1503 case Opt_posixpaths:
1504 vol->posix_paths = 1;
1506 case Opt_noposixpaths:
1507 vol->posix_paths = 0;
1510 vol->no_linux_ext = 1;
1521 * turn off mandatory locking in mode
1522 * if remote locking is turned off since the
1523 * local vfs will do advisory
1525 if (vol->file_mode ==
1526 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
1527 vol->file_mode = S_IALLUGO;
1529 case Opt_forcemandatorylock:
1538 case Opt_setuidfromacl:
1539 vol->setuidfromacl = 1;
1542 vol->dynperm = true;
1545 vol->dynperm = false;
1559 case Opt_nostrictsync:
1560 vol->nostrictsync = 1;
1562 case Opt_strictsync:
1563 vol->nostrictsync = 0;
1566 vol->server_ino = 1;
1568 case Opt_noserverino:
1569 vol->server_ino = 0;
1571 case Opt_rwpidforward:
1572 vol->rwpidforward = 1;
1581 vol->no_psx_acl = 0;
1584 vol->no_psx_acl = 1;
1586 case Opt_locallease:
1587 vol->local_lease = 1;
1593 /* we do not do the following in secFlags because seal
1594 * is a per tree connection (mount) not a per socket
1595 * or per-smb connection option in the protocol
1596 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1601 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
1604 #ifndef CONFIG_CIFS_FSCACHE
1605 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
1606 goto cifs_parse_mount_err;
1610 case Opt_mfsymlinks:
1611 vol->mfsymlinks = true;
1614 vol->multiuser = true;
1619 case Opt_nosharesock:
1620 vol->nosharesock = true;
1622 case Opt_nopersistent:
1623 vol->nopersistent = true;
1624 if (vol->persistent) {
1626 "persistenthandles mount options conflict\n");
1627 goto cifs_parse_mount_err;
1630 case Opt_persistent:
1631 vol->persistent = true;
1632 if ((vol->nopersistent) || (vol->resilient)) {
1634 "persistenthandles mount options conflict\n");
1635 goto cifs_parse_mount_err;
1639 vol->resilient = true;
1640 if (vol->persistent) {
1642 "persistenthandles mount options conflict\n");
1643 goto cifs_parse_mount_err;
1646 case Opt_noresilient:
1647 vol->resilient = false; /* already the default */
1649 case Opt_domainauto:
1650 vol->domainauto = true;
1653 /* Numeric Values */
1655 if (get_option_uid(args, &vol->backupuid)) {
1656 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1658 goto cifs_parse_mount_err;
1660 vol->backupuid_specified = true;
1663 if (get_option_gid(args, &vol->backupgid)) {
1664 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1666 goto cifs_parse_mount_err;
1668 vol->backupgid_specified = true;
1671 if (get_option_uid(args, &vol->linux_uid)) {
1672 cifs_dbg(VFS, "%s: Invalid uid value\n",
1674 goto cifs_parse_mount_err;
1676 uid_specified = true;
1679 if (get_option_uid(args, &vol->cred_uid)) {
1680 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1682 goto cifs_parse_mount_err;
1686 if (get_option_gid(args, &vol->linux_gid)) {
1687 cifs_dbg(VFS, "%s: Invalid gid value\n",
1689 goto cifs_parse_mount_err;
1691 gid_specified = true;
1694 if (get_option_ul(args, &option)) {
1695 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1697 goto cifs_parse_mount_err;
1699 vol->file_mode = option;
1702 if (get_option_ul(args, &option)) {
1703 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1705 goto cifs_parse_mount_err;
1707 vol->dir_mode = option;
1710 if (get_option_ul(args, &option) ||
1711 option > USHRT_MAX) {
1712 cifs_dbg(VFS, "%s: Invalid port value\n",
1714 goto cifs_parse_mount_err;
1716 port = (unsigned short)option;
1719 if (get_option_ul(args, &option)) {
1720 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1722 goto cifs_parse_mount_err;
1724 vol->rsize = option;
1727 if (get_option_ul(args, &option)) {
1728 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1730 goto cifs_parse_mount_err;
1732 vol->wsize = option;
1735 if (get_option_ul(args, &option)) {
1736 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1738 goto cifs_parse_mount_err;
1740 vol->actimeo = HZ * option;
1741 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
1742 cifs_dbg(VFS, "attribute cache timeout too large\n");
1743 goto cifs_parse_mount_err;
1746 case Opt_echo_interval:
1747 if (get_option_ul(args, &option)) {
1748 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1750 goto cifs_parse_mount_err;
1752 vol->echo_interval = option;
1755 if (get_option_ul(args, &option)) {
1756 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1758 goto cifs_parse_mount_err;
1760 vol->snapshot_time = option;
1762 case Opt_max_credits:
1763 if (get_option_ul(args, &option) || (option < 20) ||
1765 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1767 goto cifs_parse_mount_err;
1769 vol->max_credits = option;
1772 /* String Arguments */
1774 case Opt_blank_user:
1775 /* null user, ie. anonymous authentication */
1777 vol->username = NULL;
1780 string = match_strdup(args);
1784 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1785 CIFS_MAX_USERNAME_LEN) {
1786 pr_warn("CIFS: username too long\n");
1787 goto cifs_parse_mount_err;
1790 kfree(vol->username);
1791 vol->username = kstrdup(string, GFP_KERNEL);
1793 goto cifs_parse_mount_err;
1795 case Opt_blank_pass:
1796 /* passwords have to be handled differently
1797 * to allow the character used for deliminator
1798 * to be passed within them
1802 * Check if this is a case where the password
1803 * starts with a delimiter
1805 tmp_end = strchr(data, '=');
1807 if (!(tmp_end < end && tmp_end[1] == delim)) {
1808 /* No it is not. Set the password to NULL */
1809 kzfree(vol->password);
1810 vol->password = NULL;
1813 /* Yes it is. Drop down to Opt_pass below.*/
1815 /* Obtain the value string */
1816 value = strchr(data, '=');
1819 /* Set tmp_end to end of the string */
1820 tmp_end = (char *) value + strlen(value);
1822 /* Check if following character is the deliminator
1823 * If yes, we have encountered a double deliminator
1824 * reset the NULL character to the deliminator
1826 if (tmp_end < end && tmp_end[1] == delim) {
1829 /* Keep iterating until we get to a single
1830 * deliminator OR the end
1832 while ((tmp_end = strchr(tmp_end, delim))
1833 != NULL && (tmp_end[1] == delim)) {
1834 tmp_end = (char *) &tmp_end[2];
1837 /* Reset var options to point to next element */
1840 options = (char *) &tmp_end[1];
1842 /* Reached the end of the mount option
1847 kzfree(vol->password);
1848 /* Now build new password string */
1849 temp_len = strlen(value);
1850 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1851 if (vol->password == NULL) {
1852 pr_warn("CIFS: no memory for password\n");
1853 goto cifs_parse_mount_err;
1856 for (i = 0, j = 0; i < temp_len; i++, j++) {
1857 vol->password[j] = value[i];
1858 if ((value[i] == delim) &&
1859 value[i+1] == delim)
1860 /* skip the second deliminator */
1863 vol->password[j] = '\0';
1866 /* FIXME: should this be an error instead? */
1870 string = match_strdup(args);
1874 if (!cifs_convert_address(dstaddr, string,
1876 pr_err("CIFS: bad ip= option (%s).\n", string);
1877 goto cifs_parse_mount_err;
1882 string = match_strdup(args);
1886 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1887 == CIFS_MAX_DOMAINNAME_LEN) {
1888 pr_warn("CIFS: domain name too long\n");
1889 goto cifs_parse_mount_err;
1892 kfree(vol->domainname);
1893 vol->domainname = kstrdup(string, GFP_KERNEL);
1894 if (!vol->domainname) {
1895 pr_warn("CIFS: no memory for domainname\n");
1896 goto cifs_parse_mount_err;
1898 cifs_dbg(FYI, "Domain name set\n");
1901 string = match_strdup(args);
1905 if (!cifs_convert_address(
1906 (struct sockaddr *)&vol->srcaddr,
1907 string, strlen(string))) {
1908 pr_warn("CIFS: Could not parse srcaddr: %s\n",
1910 goto cifs_parse_mount_err;
1914 string = match_strdup(args);
1918 if (strnlen(string, 1024) >= 65) {
1919 pr_warn("CIFS: iocharset name too long.\n");
1920 goto cifs_parse_mount_err;
1923 if (strncasecmp(string, "default", 7) != 0) {
1924 kfree(vol->iocharset);
1925 vol->iocharset = kstrdup(string,
1927 if (!vol->iocharset) {
1928 pr_warn("CIFS: no memory for charset\n");
1929 goto cifs_parse_mount_err;
1932 /* if iocharset not set then load_nls_default
1935 cifs_dbg(FYI, "iocharset set to %s\n", string);
1937 case Opt_netbiosname:
1938 string = match_strdup(args);
1942 memset(vol->source_rfc1001_name, 0x20,
1945 * FIXME: are there cases in which a comma can
1946 * be valid in workstation netbios name (and
1947 * need special handling)?
1949 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1950 /* don't ucase netbiosname for user */
1953 vol->source_rfc1001_name[i] = string[i];
1955 /* The string has 16th byte zero still from
1956 * set at top of the function
1958 if (i == RFC1001_NAME_LEN && string[i] != 0)
1959 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
1962 /* servernetbiosname specified override *SMBSERVER */
1963 string = match_strdup(args);
1967 /* last byte, type, is 0x20 for servr type */
1968 memset(vol->target_rfc1001_name, 0x20,
1969 RFC1001_NAME_LEN_WITH_NULL);
1971 /* BB are there cases in which a comma can be
1972 valid in this workstation netbios name
1973 (and need special handling)? */
1975 /* user or mount helper must uppercase the
1977 for (i = 0; i < 15; i++) {
1980 vol->target_rfc1001_name[i] = string[i];
1982 /* The string has 16th byte zero still from
1983 set at top of the function */
1984 if (i == RFC1001_NAME_LEN && string[i] != 0)
1985 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
1988 /* version of mount userspace tools, not dialect */
1989 string = match_strdup(args);
1993 /* If interface changes in mount.cifs bump to new ver */
1994 if (strncasecmp(string, "1", 1) == 0) {
1995 if (strlen(string) > 1) {
1996 pr_warn("Bad mount helper ver=%s. Did "
1997 "you want SMB1 (CIFS) dialect "
1998 "and mean to type vers=1.0 "
1999 "instead?\n", string);
2000 goto cifs_parse_mount_err;
2002 /* This is the default */
2005 /* For all other value, error */
2006 pr_warn("CIFS: Invalid mount helper version specified\n");
2007 goto cifs_parse_mount_err;
2009 /* protocol version (dialect) */
2010 string = match_strdup(args);
2014 if (cifs_parse_smb_version(string, vol) != 0)
2015 goto cifs_parse_mount_err;
2019 string = match_strdup(args);
2023 if (cifs_parse_security_flavors(string, vol) != 0)
2024 goto cifs_parse_mount_err;
2027 string = match_strdup(args);
2031 if (cifs_parse_cache_flavor(string, vol) != 0)
2032 goto cifs_parse_mount_err;
2036 * An option we don't recognize. Save it off for later
2037 * if we haven't already found one
2043 /* Free up any allocated string */
2048 if (!sloppy && invalid) {
2049 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
2050 goto cifs_parse_mount_err;
2054 /* Muliuser mounts require CONFIG_KEYS support */
2055 if (vol->multiuser) {
2056 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
2057 goto cifs_parse_mount_err;
2061 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
2062 goto cifs_parse_mount_err;
2065 /* make sure UNC has a share name */
2066 if (!strchr(vol->UNC + 3, '\\')) {
2067 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
2068 goto cifs_parse_mount_err;
2075 /* No ip= option specified? Try to get it from UNC */
2076 /* Use the address part of the UNC. */
2077 slash = strchr(&vol->UNC[2], '\\');
2078 len = slash - &vol->UNC[2];
2079 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
2080 pr_err("Unable to determine destination address.\n");
2081 goto cifs_parse_mount_err;
2085 /* set the port that we got earlier */
2086 cifs_set_port(dstaddr, port);
2089 vol->override_uid = override_uid;
2090 else if (override_uid == 1)
2091 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
2094 vol->override_gid = override_gid;
2095 else if (override_gid == 1)
2096 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
2098 if (got_version == false)
2099 pr_warn("No dialect specified on mount. Default has changed to "
2100 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
2101 "(SMB1). To use the less secure SMB1 dialect to access "
2102 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2105 kfree(mountdata_copy);
2109 pr_warn("Could not allocate temporary buffer\n");
2110 cifs_parse_mount_err:
2112 kfree(mountdata_copy);
2116 /** Returns true if srcaddr isn't specified and rhs isn't
2117 * specified, or if srcaddr is specified and
2118 * matches the IP address of the rhs argument.
2121 srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2123 switch (srcaddr->sa_family) {
2125 return (rhs->sa_family == AF_UNSPEC);
2127 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2128 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2129 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2132 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
2133 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
2134 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2138 return false; /* don't expect to be here */
2143 * If no port is specified in addr structure, we try to match with 445 port
2144 * and if it fails - with 139 ports. It should be called only if address
2145 * families of server and addr are equal.
2148 match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2150 __be16 port, *sport;
2152 switch (addr->sa_family) {
2154 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2155 port = ((struct sockaddr_in *) addr)->sin_port;
2158 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2159 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2167 port = htons(CIFS_PORT);
2171 port = htons(RFC1001_PORT);
2174 return port == *sport;
2178 match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2179 struct sockaddr *srcaddr)
2181 switch (addr->sa_family) {
2183 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2184 struct sockaddr_in *srv_addr4 =
2185 (struct sockaddr_in *)&server->dstaddr;
2187 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
2192 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2193 struct sockaddr_in6 *srv_addr6 =
2194 (struct sockaddr_in6 *)&server->dstaddr;
2196 if (!ipv6_addr_equal(&addr6->sin6_addr,
2197 &srv_addr6->sin6_addr))
2199 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
2205 return false; /* don't expect to be here */
2208 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2215 match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2218 * The select_sectype function should either return the vol->sectype
2219 * that was specified, or "Unspecified" if that sectype was not
2220 * compatible with the given NEGOTIATE request.
2222 if (server->ops->select_sectype(server, vol->sectype)
2227 * Now check if signing mode is acceptable. No need to check
2228 * global_secflags at this point since if MUST_SIGN is set then
2229 * the server->sign had better be too.
2231 if (vol->sign && !server->sign)
2237 static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
2239 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2241 if (vol->nosharesock)
2244 /* BB update this for smb3any and default case */
2245 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2248 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2251 if (!match_address(server, addr,
2252 (struct sockaddr *)&vol->srcaddr))
2255 if (!match_port(server, addr))
2258 if (!match_security(server, vol))
2261 if (server->echo_interval != vol->echo_interval * HZ)
2267 static struct TCP_Server_Info *
2268 cifs_find_tcp_session(struct smb_vol *vol)
2270 struct TCP_Server_Info *server;
2272 spin_lock(&cifs_tcp_ses_lock);
2273 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
2274 if (!match_server(server, vol))
2277 ++server->srv_count;
2278 spin_unlock(&cifs_tcp_ses_lock);
2279 cifs_dbg(FYI, "Existing tcp session with server found\n");
2282 spin_unlock(&cifs_tcp_ses_lock);
2287 cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
2289 struct task_struct *task;
2291 spin_lock(&cifs_tcp_ses_lock);
2292 if (--server->srv_count > 0) {
2293 spin_unlock(&cifs_tcp_ses_lock);
2297 put_net(cifs_net_ns(server));
2299 list_del_init(&server->tcp_ses_list);
2300 spin_unlock(&cifs_tcp_ses_lock);
2302 cancel_delayed_work_sync(&server->echo);
2306 * Avoid deadlock here: reconnect work calls
2307 * cifs_put_tcp_session() at its end. Need to be sure
2308 * that reconnect work does nothing with server pointer after
2311 cancel_delayed_work(&server->reconnect);
2313 cancel_delayed_work_sync(&server->reconnect);
2315 spin_lock(&GlobalMid_Lock);
2316 server->tcpStatus = CifsExiting;
2317 spin_unlock(&GlobalMid_Lock);
2319 cifs_crypto_secmech_release(server);
2320 cifs_fscache_release_client_cookie(server);
2322 kfree(server->session_key.response);
2323 server->session_key.response = NULL;
2324 server->session_key.len = 0;
2326 task = xchg(&server->tsk, NULL);
2328 send_sig(SIGKILL, task, 1);
2331 static struct TCP_Server_Info *
2332 cifs_get_tcp_session(struct smb_vol *volume_info)
2334 struct TCP_Server_Info *tcp_ses = NULL;
2337 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
2339 /* see if we already have a matching tcp_ses */
2340 tcp_ses = cifs_find_tcp_session(volume_info);
2344 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2350 tcp_ses->ops = volume_info->ops;
2351 tcp_ses->vals = volume_info->vals;
2352 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
2353 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2354 if (IS_ERR(tcp_ses->hostname)) {
2355 rc = PTR_ERR(tcp_ses->hostname);
2356 goto out_err_crypto_release;
2359 tcp_ses->noblocksnd = volume_info->noblocksnd;
2360 tcp_ses->noautotune = volume_info->noautotune;
2361 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
2362 tcp_ses->in_flight = 0;
2363 tcp_ses->credits = 1;
2364 init_waitqueue_head(&tcp_ses->response_q);
2365 init_waitqueue_head(&tcp_ses->request_q);
2366 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2367 mutex_init(&tcp_ses->srv_mutex);
2368 memcpy(tcp_ses->workstation_RFC1001_name,
2369 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2370 memcpy(tcp_ses->server_RFC1001_name,
2371 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2372 tcp_ses->session_estab = false;
2373 tcp_ses->sequence_number = 0;
2374 tcp_ses->lstrp = jiffies;
2375 spin_lock_init(&tcp_ses->req_lock);
2376 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2377 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
2378 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
2379 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2380 mutex_init(&tcp_ses->reconnect_mutex);
2381 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2382 sizeof(tcp_ses->srcaddr));
2383 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2384 sizeof(tcp_ses->dstaddr));
2385 generate_random_uuid(tcp_ses->client_guid);
2387 * at this point we are the only ones with the pointer
2388 * to the struct since the kernel thread not created yet
2389 * no need to spinlock this init of tcpStatus or srv_count
2391 tcp_ses->tcpStatus = CifsNew;
2392 ++tcp_ses->srv_count;
2394 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2395 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2396 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2398 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
2400 rc = ip_connect(tcp_ses);
2402 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
2403 goto out_err_crypto_release;
2407 * since we're in a cifs function already, we know that
2408 * this will succeed. No need for try_module_get().
2410 __module_get(THIS_MODULE);
2411 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
2413 if (IS_ERR(tcp_ses->tsk)) {
2414 rc = PTR_ERR(tcp_ses->tsk);
2415 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
2416 module_put(THIS_MODULE);
2417 goto out_err_crypto_release;
2419 tcp_ses->tcpStatus = CifsNeedNegotiate;
2421 /* thread spawned, put it on the list */
2422 spin_lock(&cifs_tcp_ses_lock);
2423 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
2424 spin_unlock(&cifs_tcp_ses_lock);
2426 cifs_fscache_get_client_cookie(tcp_ses);
2428 /* queue echo request delayed work */
2429 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
2433 out_err_crypto_release:
2434 cifs_crypto_secmech_release(tcp_ses);
2436 put_net(cifs_net_ns(tcp_ses));
2440 if (!IS_ERR(tcp_ses->hostname))
2441 kfree(tcp_ses->hostname);
2442 if (tcp_ses->ssocket)
2443 sock_release(tcp_ses->ssocket);
2449 static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
2451 if (vol->sectype != Unspecified &&
2452 vol->sectype != ses->sectype)
2455 switch (ses->sectype) {
2457 if (!uid_eq(vol->cred_uid, ses->cred_uid))
2461 /* NULL username means anonymous session */
2462 if (ses->user_name == NULL) {
2468 /* anything else takes username/password */
2469 if (strncmp(ses->user_name,
2470 vol->username ? vol->username : "",
2471 CIFS_MAX_USERNAME_LEN))
2473 if ((vol->username && strlen(vol->username) != 0) &&
2474 ses->password != NULL &&
2475 strncmp(ses->password,
2476 vol->password ? vol->password : "",
2477 CIFS_MAX_PASSWORD_LEN))
2483 static struct cifs_ses *
2484 cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
2486 struct cifs_ses *ses;
2488 spin_lock(&cifs_tcp_ses_lock);
2489 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2490 if (ses->status == CifsExiting)
2492 if (!match_session(ses, vol))
2495 spin_unlock(&cifs_tcp_ses_lock);
2498 spin_unlock(&cifs_tcp_ses_lock);
2503 cifs_put_smb_ses(struct cifs_ses *ses)
2505 unsigned int rc, xid;
2506 struct TCP_Server_Info *server = ses->server;
2508 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
2510 spin_lock(&cifs_tcp_ses_lock);
2511 if (ses->status == CifsExiting) {
2512 spin_unlock(&cifs_tcp_ses_lock);
2515 if (--ses->ses_count > 0) {
2516 spin_unlock(&cifs_tcp_ses_lock);
2519 if (ses->status == CifsGood)
2520 ses->status = CifsExiting;
2521 spin_unlock(&cifs_tcp_ses_lock);
2523 if (ses->status == CifsExiting && server->ops->logoff) {
2525 rc = server->ops->logoff(xid, ses);
2527 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2532 spin_lock(&cifs_tcp_ses_lock);
2533 list_del_init(&ses->smb_ses_list);
2534 spin_unlock(&cifs_tcp_ses_lock);
2537 cifs_put_tcp_session(server, 0);
2542 /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2543 #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
2545 /* Populate username and pw fields from keyring if possible */
2547 cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2551 const char *delim, *payload;
2555 struct TCP_Server_Info *server = ses->server;
2556 struct sockaddr_in *sa;
2557 struct sockaddr_in6 *sa6;
2558 const struct user_key_payload *upayload;
2560 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2564 /* try to find an address key first */
2565 switch (server->dstaddr.ss_family) {
2567 sa = (struct sockaddr_in *)&server->dstaddr;
2568 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2571 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2572 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2575 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2576 server->dstaddr.ss_family);
2581 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2582 key = request_key(&key_type_logon, desc, "");
2584 if (!ses->domainName) {
2585 cifs_dbg(FYI, "domainName is NULL\n");
2590 /* didn't work, try to find a domain key */
2591 sprintf(desc, "cifs:d:%s", ses->domainName);
2592 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2593 key = request_key(&key_type_logon, desc, "");
2601 down_read(&key->sem);
2602 upayload = user_key_payload_locked(key);
2603 if (IS_ERR_OR_NULL(upayload)) {
2604 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
2608 /* find first : in payload */
2609 payload = upayload->data;
2610 delim = strnchr(payload, upayload->datalen, ':');
2611 cifs_dbg(FYI, "payload=%s\n", payload);
2613 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2619 len = delim - payload;
2620 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
2621 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2627 vol->username = kstrndup(payload, len, GFP_KERNEL);
2628 if (!vol->username) {
2629 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2634 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
2636 len = key->datalen - (len + 1);
2637 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
2638 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
2640 kfree(vol->username);
2641 vol->username = NULL;
2646 vol->password = kstrndup(delim, len, GFP_KERNEL);
2647 if (!vol->password) {
2648 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2651 kfree(vol->username);
2652 vol->username = NULL;
2657 * If we have a domain key then we must set the domainName in the
2660 if (is_domain && ses->domainName) {
2661 vol->domainname = kstrndup(ses->domainName,
2662 strlen(ses->domainName),
2664 if (!vol->domainname) {
2665 cifs_dbg(FYI, "Unable to allocate %zd bytes for "
2668 kfree(vol->username);
2669 vol->username = NULL;
2670 kzfree(vol->password);
2671 vol->password = NULL;
2681 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
2684 #else /* ! CONFIG_KEYS */
2686 cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2687 struct cifs_ses *ses __attribute__((unused)))
2691 #endif /* CONFIG_KEYS */
2693 static struct cifs_ses *
2694 cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2698 struct cifs_ses *ses;
2699 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2700 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2704 ses = cifs_find_smb_ses(server, volume_info);
2706 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2709 mutex_lock(&ses->session_mutex);
2710 rc = cifs_negotiate_protocol(xid, ses);
2712 mutex_unlock(&ses->session_mutex);
2713 /* problem -- put our ses reference */
2714 cifs_put_smb_ses(ses);
2718 if (ses->need_reconnect) {
2719 cifs_dbg(FYI, "Session needs reconnect\n");
2720 rc = cifs_setup_session(xid, ses,
2721 volume_info->local_nls);
2723 mutex_unlock(&ses->session_mutex);
2724 /* problem -- put our reference */
2725 cifs_put_smb_ses(ses);
2730 mutex_unlock(&ses->session_mutex);
2732 /* existing SMB ses has a server reference already */
2733 cifs_put_tcp_session(server, 0);
2740 cifs_dbg(FYI, "Existing smb sess not found\n");
2741 ses = sesInfoAlloc();
2745 /* new SMB session uses our server ref */
2746 ses->server = server;
2747 if (server->dstaddr.ss_family == AF_INET6)
2748 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
2750 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
2752 if (volume_info->username) {
2753 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2754 if (!ses->user_name)
2758 /* volume_info->password freed at unmount */
2759 if (volume_info->password) {
2760 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2764 if (volume_info->domainname) {
2765 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2766 if (!ses->domainName)
2769 if (volume_info->domainauto)
2770 ses->domainAuto = volume_info->domainauto;
2771 ses->cred_uid = volume_info->cred_uid;
2772 ses->linux_uid = volume_info->linux_uid;
2774 ses->sectype = volume_info->sectype;
2775 ses->sign = volume_info->sign;
2777 mutex_lock(&ses->session_mutex);
2778 rc = cifs_negotiate_protocol(xid, ses);
2780 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
2781 mutex_unlock(&ses->session_mutex);
2785 /* success, put it on the list */
2786 spin_lock(&cifs_tcp_ses_lock);
2787 list_add(&ses->smb_ses_list, &server->smb_ses_list);
2788 spin_unlock(&cifs_tcp_ses_lock);
2799 static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
2801 if (tcon->tidStatus == CifsExiting)
2803 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
2805 if (tcon->seal != volume_info->seal)
2807 if (tcon->snapshot_time != volume_info->snapshot_time)
2812 static struct cifs_tcon *
2813 cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
2815 struct list_head *tmp;
2816 struct cifs_tcon *tcon;
2818 spin_lock(&cifs_tcp_ses_lock);
2819 list_for_each(tmp, &ses->tcon_list) {
2820 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
2821 if (!match_tcon(tcon, volume_info))
2824 spin_unlock(&cifs_tcp_ses_lock);
2827 spin_unlock(&cifs_tcp_ses_lock);
2832 cifs_put_tcon(struct cifs_tcon *tcon)
2835 struct cifs_ses *ses = tcon->ses;
2837 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
2838 spin_lock(&cifs_tcp_ses_lock);
2839 if (--tcon->tc_count > 0) {
2840 spin_unlock(&cifs_tcp_ses_lock);
2844 list_del_init(&tcon->tcon_list);
2845 spin_unlock(&cifs_tcp_ses_lock);
2848 if (ses->server->ops->tree_disconnect)
2849 ses->server->ops->tree_disconnect(xid, tcon);
2852 cifs_fscache_release_super_cookie(tcon);
2854 cifs_put_smb_ses(ses);
2857 static struct cifs_tcon *
2858 cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
2861 struct cifs_tcon *tcon;
2863 tcon = cifs_find_tcon(ses, volume_info);
2865 cifs_dbg(FYI, "Found match on UNC path\n");
2866 /* existing tcon already has a reference */
2867 cifs_put_smb_ses(ses);
2871 if (!ses->server->ops->tree_connect) {
2876 tcon = tconInfoAlloc();
2882 if (volume_info->snapshot_time) {
2883 if (ses->server->vals->protocol_id == 0) {
2885 "Use SMB2 or later for snapshot mount option\n");
2889 tcon->snapshot_time = volume_info->snapshot_time;
2893 if (volume_info->password) {
2894 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
2895 if (!tcon->password) {
2902 * BB Do we need to wrap session_mutex around this TCon call and Unix
2903 * SetFS as we do on SessSetup and reconnect?
2906 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
2907 volume_info->local_nls);
2909 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
2913 if (volume_info->nodfs) {
2914 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
2915 cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
2917 tcon->use_persistent = false;
2918 /* check if SMB2 or later, CIFS does not support persistent handles */
2919 if (volume_info->persistent) {
2920 if (ses->server->vals->protocol_id == 0) {
2922 "SMB3 or later required for persistent handles\n");
2925 } else if (ses->server->capabilities &
2926 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2927 tcon->use_persistent = true;
2928 else /* persistent handles requested but not supported */ {
2930 "Persistent handles not supported on share\n");
2934 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2935 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2936 && (volume_info->nopersistent == false)) {
2937 cifs_dbg(FYI, "enabling persistent handles\n");
2938 tcon->use_persistent = true;
2939 } else if (volume_info->resilient) {
2940 if (ses->server->vals->protocol_id == 0) {
2942 "SMB2.1 or later required for resilient handles\n");
2946 tcon->use_resilient = true;
2949 if (volume_info->seal) {
2950 if (ses->server->vals->protocol_id == 0) {
2952 "SMB3 or later required for encryption\n");
2955 } else if (tcon->ses->server->capabilities &
2956 SMB2_GLOBAL_CAP_ENCRYPTION)
2959 cifs_dbg(VFS, "Encryption is not supported on share\n");
2966 * We can have only one retry value for a connection to a share so for
2967 * resources mounted more than once to the same server share the last
2968 * value passed in for the retry flag is used.
2970 tcon->retry = volume_info->retry;
2971 tcon->nocase = volume_info->nocase;
2972 tcon->local_lease = volume_info->local_lease;
2973 INIT_LIST_HEAD(&tcon->pending_opens);
2975 spin_lock(&cifs_tcp_ses_lock);
2976 list_add(&tcon->tcon_list, &ses->tcon_list);
2977 spin_unlock(&cifs_tcp_ses_lock);
2979 cifs_fscache_get_super_cookie(tcon);
2989 cifs_put_tlink(struct tcon_link *tlink)
2991 if (!tlink || IS_ERR(tlink))
2994 if (!atomic_dec_and_test(&tlink->tl_count) ||
2995 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2996 tlink->tl_time = jiffies;
3000 if (!IS_ERR(tlink_tcon(tlink)))
3001 cifs_put_tcon(tlink_tcon(tlink));
3006 static inline struct tcon_link *
3007 cifs_sb_master_tlink(struct cifs_sb_info *cifs_sb)
3009 return cifs_sb->master_tlink;
3013 compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3015 struct cifs_sb_info *old = CIFS_SB(sb);
3016 struct cifs_sb_info *new = mnt_data->cifs_sb;
3018 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3021 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
3022 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
3026 * We want to share sb only if we don't specify an r/wsize or
3027 * specified r/wsize is greater than or equal to existing one.
3029 if (new->wsize && new->wsize < old->wsize)
3032 if (new->rsize && new->rsize < old->rsize)
3035 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
3038 if (old->mnt_file_mode != new->mnt_file_mode ||
3039 old->mnt_dir_mode != new->mnt_dir_mode)
3042 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3045 if (old->actimeo != new->actimeo)
3052 match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3054 struct cifs_sb_info *old = CIFS_SB(sb);
3055 struct cifs_sb_info *new = mnt_data->cifs_sb;
3056 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
3058 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
3061 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
3063 else if (!old_set && !new_set)
3070 cifs_match_super(struct super_block *sb, void *data)
3072 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3073 struct smb_vol *volume_info;
3074 struct cifs_sb_info *cifs_sb;
3075 struct TCP_Server_Info *tcp_srv;
3076 struct cifs_ses *ses;
3077 struct cifs_tcon *tcon;
3078 struct tcon_link *tlink;
3081 spin_lock(&cifs_tcp_ses_lock);
3082 cifs_sb = CIFS_SB(sb);
3083 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3084 if (tlink == NULL) {
3085 /* can not match superblock if tlink were ever null */
3086 spin_unlock(&cifs_tcp_ses_lock);
3089 tcon = tlink_tcon(tlink);
3091 tcp_srv = ses->server;
3093 volume_info = mnt_data->vol;
3095 if (!match_server(tcp_srv, volume_info) ||
3096 !match_session(ses, volume_info) ||
3097 !match_tcon(tcon, volume_info) ||
3098 !match_prepath(sb, mnt_data)) {
3103 rc = compare_mount_options(sb, mnt_data);
3105 spin_unlock(&cifs_tcp_ses_lock);
3106 cifs_put_tlink(tlink);
3111 get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
3112 const struct nls_table *nls_codepage, unsigned int *num_referrals,
3113 struct dfs_info3_param **referrals, int remap)
3118 if (!ses->server->ops->tree_connect || !ses->server->ops->get_dfs_refer)
3124 if (ses->ipc_tid == 0) {
3125 temp_unc = kmalloc(2 /* for slashes */ +
3126 strnlen(ses->serverName, SERVER_NAME_LEN_WITH_NULL * 2)
3127 + 1 + 4 /* slash IPC$ */ + 2, GFP_KERNEL);
3128 if (temp_unc == NULL)
3132 strcpy(temp_unc + 2, ses->serverName);
3133 strcpy(temp_unc + 2 + strlen(ses->serverName), "\\IPC$");
3134 rc = ses->server->ops->tree_connect(xid, ses, temp_unc, NULL,
3136 cifs_dbg(FYI, "Tcon rc = %d ipc_tid = %d\n", rc, ses->ipc_tid);
3140 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
3141 referrals, num_referrals,
3142 nls_codepage, remap);
3144 * BB - map targetUNCs to dfs_info3 structures, here or in
3145 * ses->server->ops->get_dfs_refer.
3151 #ifdef CONFIG_DEBUG_LOCK_ALLOC
3152 static struct lock_class_key cifs_key[2];
3153 static struct lock_class_key cifs_slock_key[2];
3156 cifs_reclassify_socket4(struct socket *sock)
3158 struct sock *sk = sock->sk;
3159 BUG_ON(!sock_allow_reclassification(sk));
3160 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3161 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3165 cifs_reclassify_socket6(struct socket *sock)
3167 struct sock *sk = sock->sk;
3168 BUG_ON(!sock_allow_reclassification(sk));
3169 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3170 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3174 cifs_reclassify_socket4(struct socket *sock)
3179 cifs_reclassify_socket6(struct socket *sock)
3184 /* See RFC1001 section 14 on representation of Netbios names */
3185 static void rfc1002mangle(char *target, char *source, unsigned int length)
3189 for (i = 0, j = 0; i < (length); i++) {
3190 /* mask a nibble at a time and encode */
3191 target[j] = 'A' + (0x0F & (source[i] >> 4));
3192 target[j+1] = 'A' + (0x0F & source[i]);
3199 bind_socket(struct TCP_Server_Info *server)
3202 if (server->srcaddr.ss_family != AF_UNSPEC) {
3203 /* Bind to the specified local IP address */
3204 struct socket *socket = server->ssocket;
3205 rc = socket->ops->bind(socket,
3206 (struct sockaddr *) &server->srcaddr,
3207 sizeof(server->srcaddr));
3209 struct sockaddr_in *saddr4;
3210 struct sockaddr_in6 *saddr6;
3211 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3212 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3213 if (saddr6->sin6_family == AF_INET6)
3214 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3215 &saddr6->sin6_addr, rc);
3217 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3218 &saddr4->sin_addr.s_addr, rc);
3225 ip_rfc1001_connect(struct TCP_Server_Info *server)
3229 * some servers require RFC1001 sessinit before sending
3230 * negprot - BB check reconnection in case where second
3231 * sessinit is sent but no second negprot
3233 struct rfc1002_session_packet *ses_init_buf;
3234 struct smb_hdr *smb_buf;
3235 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3238 ses_init_buf->trailer.session_req.called_len = 32;
3240 if (server->server_RFC1001_name[0] != 0)
3241 rfc1002mangle(ses_init_buf->trailer.
3242 session_req.called_name,
3243 server->server_RFC1001_name,
3244 RFC1001_NAME_LEN_WITH_NULL);
3246 rfc1002mangle(ses_init_buf->trailer.
3247 session_req.called_name,
3248 DEFAULT_CIFS_CALLED_NAME,
3249 RFC1001_NAME_LEN_WITH_NULL);
3251 ses_init_buf->trailer.session_req.calling_len = 32;
3254 * calling name ends in null (byte 16) from old smb
3257 if (server->workstation_RFC1001_name[0] != 0)
3258 rfc1002mangle(ses_init_buf->trailer.
3259 session_req.calling_name,
3260 server->workstation_RFC1001_name,
3261 RFC1001_NAME_LEN_WITH_NULL);
3263 rfc1002mangle(ses_init_buf->trailer.
3264 session_req.calling_name,
3266 RFC1001_NAME_LEN_WITH_NULL);
3268 ses_init_buf->trailer.session_req.scope1 = 0;
3269 ses_init_buf->trailer.session_req.scope2 = 0;
3270 smb_buf = (struct smb_hdr *)ses_init_buf;
3272 /* sizeof RFC1002_SESSION_REQUEST with no scope */
3273 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
3274 rc = smb_send(server, smb_buf, 0x44);
3275 kfree(ses_init_buf);
3277 * RFC1001 layer in at least one server
3278 * requires very short break before negprot
3279 * presumably because not expecting negprot
3280 * to follow so fast. This is a simple
3281 * solution that works without
3282 * complicating the code and causes no
3283 * significant slowing down on mount
3286 usleep_range(1000, 2000);
3289 * else the negprot may still work without this
3290 * even though malloc failed
3297 generic_ip_connect(struct TCP_Server_Info *server)
3302 struct socket *socket = server->ssocket;
3303 struct sockaddr *saddr;
3305 saddr = (struct sockaddr *) &server->dstaddr;
3307 if (server->dstaddr.ss_family == AF_INET6) {
3308 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3309 slen = sizeof(struct sockaddr_in6);
3312 sport = ((struct sockaddr_in *) saddr)->sin_port;
3313 slen = sizeof(struct sockaddr_in);
3317 if (socket == NULL) {
3318 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3319 IPPROTO_TCP, &socket, 1);
3321 cifs_dbg(VFS, "Error %d creating socket\n", rc);
3322 server->ssocket = NULL;
3326 /* BB other socket options to set KEEPALIVE, NODELAY? */
3327 cifs_dbg(FYI, "Socket created\n");
3328 server->ssocket = socket;
3329 socket->sk->sk_allocation = GFP_NOFS;
3330 if (sfamily == AF_INET6)
3331 cifs_reclassify_socket6(socket);
3333 cifs_reclassify_socket4(socket);
3336 rc = bind_socket(server);
3341 * Eventually check for other socket options to change from
3342 * the default. sock_setsockopt not used because it expects
3345 socket->sk->sk_rcvtimeo = 7 * HZ;
3346 socket->sk->sk_sndtimeo = 5 * HZ;
3348 /* make the bufsizes depend on wsize/rsize and max requests */
3349 if (server->noautotune) {
3350 if (socket->sk->sk_sndbuf < (200 * 1024))
3351 socket->sk->sk_sndbuf = 200 * 1024;
3352 if (socket->sk->sk_rcvbuf < (140 * 1024))
3353 socket->sk->sk_rcvbuf = 140 * 1024;
3356 if (server->tcp_nodelay) {
3358 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3359 (char *)&val, sizeof(val));
3361 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3365 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
3366 socket->sk->sk_sndbuf,
3367 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3369 rc = socket->ops->connect(socket, saddr, slen, 0);
3371 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
3372 sock_release(socket);
3373 server->ssocket = NULL;
3377 if (sport == htons(RFC1001_PORT))
3378 rc = ip_rfc1001_connect(server);
3384 ip_connect(struct TCP_Server_Info *server)
3387 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3388 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3390 if (server->dstaddr.ss_family == AF_INET6)
3391 sport = &addr6->sin6_port;
3393 sport = &addr->sin_port;
3398 /* try with 445 port at first */
3399 *sport = htons(CIFS_PORT);
3401 rc = generic_ip_connect(server);
3405 /* if it failed, try with 139 port */
3406 *sport = htons(RFC1001_PORT);
3409 return generic_ip_connect(server);
3412 void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
3413 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
3415 /* if we are reconnecting then should we check to see if
3416 * any requested capabilities changed locally e.g. via
3417 * remount but we can not do much about it here
3418 * if they have (even if we could detect it by the following)
3419 * Perhaps we could add a backpointer to array of sb from tcon
3420 * or if we change to make all sb to same share the same
3421 * sb as NFS - then we only have one backpointer to sb.
3422 * What if we wanted to mount the server share twice once with
3423 * and once without posixacls or posix paths? */
3424 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3426 if (vol_info && vol_info->no_linux_ext) {
3427 tcon->fsUnixInfo.Capability = 0;
3428 tcon->unix_ext = 0; /* Unix Extensions disabled */
3429 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
3431 } else if (vol_info)
3432 tcon->unix_ext = 1; /* Unix Extensions supported */
3434 if (tcon->unix_ext == 0) {
3435 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
3439 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
3440 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3441 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
3442 /* check for reconnect case in which we do not
3443 want to change the mount behavior if we can avoid it */
3444 if (vol_info == NULL) {
3445 /* turn off POSIX ACL and PATHNAMES if not set
3446 originally at mount time */
3447 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3448 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3449 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3450 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3451 cifs_dbg(VFS, "POSIXPATH support change\n");
3452 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3453 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3454 cifs_dbg(VFS, "possible reconnect error\n");
3455 cifs_dbg(VFS, "server disabled POSIX path support\n");
3459 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3460 cifs_dbg(VFS, "per-share encryption not supported yet\n");
3462 cap &= CIFS_UNIX_CAP_MASK;
3463 if (vol_info && vol_info->no_psx_acl)
3464 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3465 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
3466 cifs_dbg(FYI, "negotiated posix acl support\n");
3468 cifs_sb->mnt_cifs_flags |=
3469 CIFS_MOUNT_POSIXACL;
3472 if (vol_info && vol_info->posix_paths == 0)
3473 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3474 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3475 cifs_dbg(FYI, "negotiate posix pathnames\n");
3477 cifs_sb->mnt_cifs_flags |=
3478 CIFS_MOUNT_POSIX_PATHS;
3481 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
3482 #ifdef CONFIG_CIFS_DEBUG2
3483 if (cap & CIFS_UNIX_FCNTL_CAP)
3484 cifs_dbg(FYI, "FCNTL cap\n");
3485 if (cap & CIFS_UNIX_EXTATTR_CAP)
3486 cifs_dbg(FYI, "EXTATTR cap\n");
3487 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3488 cifs_dbg(FYI, "POSIX path cap\n");
3489 if (cap & CIFS_UNIX_XATTR_CAP)
3490 cifs_dbg(FYI, "XATTR cap\n");
3491 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
3492 cifs_dbg(FYI, "POSIX ACL cap\n");
3493 if (cap & CIFS_UNIX_LARGE_READ_CAP)
3494 cifs_dbg(FYI, "very large read cap\n");
3495 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
3496 cifs_dbg(FYI, "very large write cap\n");
3497 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
3498 cifs_dbg(FYI, "transport encryption cap\n");
3499 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3500 cifs_dbg(FYI, "mandatory transport encryption cap\n");
3501 #endif /* CIFS_DEBUG2 */
3502 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
3503 if (vol_info == NULL) {
3504 cifs_dbg(FYI, "resetting capabilities failed\n");
3506 cifs_dbg(VFS, "Negotiating Unix capabilities with the server failed. Consider mounting with the Unix Extensions disabled if problems are found by specifying the nounix mount option.\n");
3512 int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
3513 struct cifs_sb_info *cifs_sb)
3515 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3517 spin_lock_init(&cifs_sb->tlink_tree_lock);
3518 cifs_sb->tlink_tree = RB_ROOT;
3521 * Temporarily set r/wsize for matching superblock. If we end up using
3522 * new sb then client will later negotiate it downward if needed.
3524 cifs_sb->rsize = pvolume_info->rsize;
3525 cifs_sb->wsize = pvolume_info->wsize;
3527 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3528 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3529 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3530 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
3531 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
3532 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
3534 cifs_sb->actimeo = pvolume_info->actimeo;
3535 cifs_sb->local_nls = pvolume_info->local_nls;
3537 if (pvolume_info->noperm)
3538 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3539 if (pvolume_info->setuids)
3540 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
3541 if (pvolume_info->setuidfromacl)
3542 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
3543 if (pvolume_info->server_ino)
3544 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3545 if (pvolume_info->remap)
3546 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3547 if (pvolume_info->sfu_remap)
3548 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3549 if (pvolume_info->no_xattr)
3550 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3551 if (pvolume_info->sfu_emul)
3552 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3553 if (pvolume_info->nobrl)
3554 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
3555 if (pvolume_info->nostrictsync)
3556 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
3557 if (pvolume_info->mand_lock)
3558 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
3559 if (pvolume_info->rwpidforward)
3560 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
3561 if (pvolume_info->cifs_acl)
3562 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
3563 if (pvolume_info->backupuid_specified) {
3564 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
3565 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3567 if (pvolume_info->backupgid_specified) {
3568 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
3569 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3571 if (pvolume_info->override_uid)
3572 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3573 if (pvolume_info->override_gid)
3574 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3575 if (pvolume_info->dynperm)
3576 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
3577 if (pvolume_info->fsc)
3578 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
3579 if (pvolume_info->multiuser)
3580 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3581 CIFS_MOUNT_NO_PERM);
3582 if (pvolume_info->strict_io)
3583 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
3584 if (pvolume_info->direct_io) {
3585 cifs_dbg(FYI, "mounting share using direct i/o\n");
3586 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3588 if (pvolume_info->mfsymlinks) {
3589 if (pvolume_info->sfu_emul) {
3591 * Our SFU ("Services for Unix" emulation does not allow
3592 * creating symlinks but does allow reading existing SFU
3593 * symlinks (it does allow both creating and reading SFU
3594 * style mknod and FIFOs though). When "mfsymlinks" and
3595 * "sfu" are both enabled at the same time, it allows
3596 * reading both types of symlinks, but will only create
3597 * them with mfsymlinks format. This allows better
3598 * Apple compatibility (probably better for Samba too)
3599 * while still recognizing old Windows style symlinks.
3601 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
3603 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
3606 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
3607 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
3609 if (pvolume_info->prepath) {
3610 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3611 if (cifs_sb->prepath == NULL)
3619 cleanup_volume_info_contents(struct smb_vol *volume_info)
3621 kfree(volume_info->username);
3622 kzfree(volume_info->password);
3623 kfree(volume_info->UNC);
3624 kfree(volume_info->domainname);
3625 kfree(volume_info->iocharset);
3626 kfree(volume_info->prepath);
3630 cifs_cleanup_volume_info(struct smb_vol *volume_info)
3634 cleanup_volume_info_contents(volume_info);
3639 #ifdef CONFIG_CIFS_DFS_UPCALL
3641 * cifs_build_path_to_root returns full path to root when we do not have an
3642 * exiting connection (tcon)
3645 build_unc_path_to_root(const struct smb_vol *vol,
3646 const struct cifs_sb_info *cifs_sb)
3648 char *full_path, *pos;
3649 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
3650 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
3652 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
3653 if (full_path == NULL)
3654 return ERR_PTR(-ENOMEM);
3656 strncpy(full_path, vol->UNC, unc_len);
3657 pos = full_path + unc_len;
3660 *pos = CIFS_DIR_SEP(cifs_sb);
3661 strncpy(pos + 1, vol->prepath, pplen);
3665 *pos = '\0'; /* add trailing null */
3666 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
3667 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
3672 * Perform a dfs referral query for a share and (optionally) prefix
3674 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3675 * to a string containing updated options for the submount. Otherwise it
3676 * will be left untouched.
3678 * Returns the rc from get_dfs_path to the caller, which can be used to
3679 * determine whether there were referrals.
3682 expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
3683 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
3687 unsigned int num_referrals = 0;
3688 struct dfs_info3_param *referrals = NULL;
3689 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3691 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3692 if (IS_ERR(full_path))
3693 return PTR_ERR(full_path);
3695 /* For DFS paths, skip the first '\' of the UNC */
3696 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3698 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
3699 &num_referrals, &referrals, cifs_remap(cifs_sb));
3701 if (!rc && num_referrals > 0) {
3702 char *fake_devname = NULL;
3704 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3705 full_path + 1, referrals,
3708 free_dfs_info_array(referrals, num_referrals);
3710 if (IS_ERR(mdata)) {
3711 rc = PTR_ERR(mdata);
3714 cleanup_volume_info_contents(volume_info);
3715 rc = cifs_setup_volume_info(volume_info, mdata,
3718 kfree(fake_devname);
3719 kfree(cifs_sb->mountdata);
3720 cifs_sb->mountdata = mdata;
3728 cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
3729 const char *devname)
3733 if (cifs_parse_mount_options(mount_data, devname, volume_info))
3736 if (volume_info->nullauth) {
3737 cifs_dbg(FYI, "Anonymous login\n");
3738 kfree(volume_info->username);
3739 volume_info->username = NULL;
3740 } else if (volume_info->username) {
3741 /* BB fixme parse for domain name here */
3742 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
3744 cifs_dbg(VFS, "No username specified\n");
3745 /* In userspace mount helper we can get user name from alternate
3746 locations such as env variables and files on disk */
3750 /* this is needed for ASCII cp to Unicode converts */
3751 if (volume_info->iocharset == NULL) {
3752 /* load_nls_default cannot return null */
3753 volume_info->local_nls = load_nls_default();
3755 volume_info->local_nls = load_nls(volume_info->iocharset);
3756 if (volume_info->local_nls == NULL) {
3757 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
3758 volume_info->iocharset);
3767 cifs_get_volume_info(char *mount_data, const char *devname)
3770 struct smb_vol *volume_info;
3772 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
3774 return ERR_PTR(-ENOMEM);
3776 rc = cifs_setup_volume_info(volume_info, mount_data, devname);
3778 cifs_cleanup_volume_info(volume_info);
3779 volume_info = ERR_PTR(rc);
3786 cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3788 struct cifs_tcon *tcon,
3789 struct cifs_sb_info *cifs_sb,
3796 sep = CIFS_DIR_SEP(cifs_sb);
3799 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3801 /* skip separators */
3806 /* next separator */
3807 while (*s && *s != sep)
3811 * temporarily null-terminate the path at the end of
3812 * the current component
3816 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3824 cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
3828 struct cifs_ses *ses;
3829 struct cifs_tcon *tcon;
3830 struct TCP_Server_Info *server;
3832 struct tcon_link *tlink;
3833 #ifdef CONFIG_CIFS_DFS_UPCALL
3834 int referral_walks_count = 0;
3837 #ifdef CONFIG_CIFS_DFS_UPCALL
3839 /* cleanup activities if we're chasing a referral */
3840 if (referral_walks_count) {
3842 cifs_put_tcon(tcon);
3844 cifs_put_smb_ses(ses);
3846 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3860 /* get a reference to a tcp session */
3861 server = cifs_get_tcp_session(volume_info);
3862 if (IS_ERR(server)) {
3863 rc = PTR_ERR(server);
3866 if ((volume_info->max_credits < 20) ||
3867 (volume_info->max_credits > 60000))
3868 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
3870 server->max_credits = volume_info->max_credits;
3871 /* get a reference to a SMB session */
3872 ses = cifs_get_smb_ses(server, volume_info);
3876 goto mount_fail_check;
3879 if ((volume_info->persistent == true) && ((ses->server->capabilities &
3880 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) == 0)) {
3881 cifs_dbg(VFS, "persistent handles not supported by server\n");
3883 goto mount_fail_check;
3886 /* search for existing tcon to this server share */
3887 tcon = cifs_get_tcon(ses, volume_info);
3892 goto mount_fail_check;
3894 goto remote_path_check;
3897 /* tell server which Unix caps we support */
3898 if (cap_unix(tcon->ses)) {
3899 /* reset of caps checks mount to see if unix extensions
3900 disabled for just this mount */
3901 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
3902 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3903 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3904 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3906 goto mount_fail_check;
3909 tcon->unix_ext = 0; /* server does not support them */
3911 /* do not care if a following call succeed - informational */
3912 if (!tcon->ipc && server->ops->qfs_tcon)
3913 server->ops->qfs_tcon(xid, tcon);
3915 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
3916 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
3919 #ifdef CONFIG_CIFS_DFS_UPCALL
3921 * Perform an unconditional check for whether there are DFS
3922 * referrals for this path without prefix, to provide support
3923 * for DFS referrals from w2k8 servers which don't seem to respond
3924 * with PATH_NOT_COVERED to requests that include the prefix.
3925 * Chase the referral if found, otherwise continue normally.
3927 if (referral_walks_count == 0) {
3928 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
3931 referral_walks_count++;
3932 goto try_mount_again;
3937 /* check if a whole path is not remote */
3939 if (!server->ops->is_path_accessible) {
3941 goto mount_fail_check;
3944 * cifs_build_path_to_root works only when we have a valid tcon
3946 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon,
3947 tcon->Flags & SMB_SHARE_IS_IN_DFS);
3948 if (full_path == NULL) {
3950 goto mount_fail_check;
3952 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3954 if (rc != 0 && rc != -EREMOTE) {
3956 goto mount_fail_check;
3959 if (rc != -EREMOTE) {
3960 rc = cifs_are_all_path_components_accessible(server,
3964 cifs_dbg(VFS, "cannot query dirs between root and final path, "
3965 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
3966 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3973 /* get referral if needed */
3974 if (rc == -EREMOTE) {
3975 #ifdef CONFIG_CIFS_DFS_UPCALL
3976 if (referral_walks_count > MAX_NESTED_LINKS) {
3978 * BB: when we implement proper loop detection,
3979 * we will remove this check. But now we need it
3980 * to prevent an indefinite loop if 'DFS tree' is
3981 * misconfigured (i.e. has loops).
3984 goto mount_fail_check;
3987 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
3990 referral_walks_count++;
3991 goto try_mount_again;
3993 goto mount_fail_check;
3994 #else /* No DFS support, return error on mount */
4000 goto mount_fail_check;
4002 /* now, hang the tcon off of the superblock */
4003 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
4004 if (tlink == NULL) {
4006 goto mount_fail_check;
4009 tlink->tl_uid = ses->linux_uid;
4010 tlink->tl_tcon = tcon;
4011 tlink->tl_time = jiffies;
4012 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
4013 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4015 cifs_sb->master_tlink = tlink;
4016 spin_lock(&cifs_sb->tlink_tree_lock);
4017 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4018 spin_unlock(&cifs_sb->tlink_tree_lock);
4020 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
4024 /* on error free sesinfo and tcon struct if needed */
4026 /* If find_unc succeeded then rc == 0 so we can not end */
4027 /* up accidentally freeing someone elses tcon struct */
4029 cifs_put_tcon(tcon);
4031 cifs_put_smb_ses(ses);
4033 cifs_put_tcp_session(server, 0);
4042 * Issue a TREE_CONNECT request. Note that for IPC$ shares, that the tcon
4043 * pointer may be NULL.
4046 CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
4047 const char *tree, struct cifs_tcon *tcon,
4048 const struct nls_table *nls_codepage)
4050 struct smb_hdr *smb_buffer;
4051 struct smb_hdr *smb_buffer_response;
4054 unsigned char *bcc_ptr;
4057 __u16 bytes_left, count;
4062 smb_buffer = cifs_buf_get();
4063 if (smb_buffer == NULL)
4066 smb_buffer_response = smb_buffer;
4068 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4069 NULL /*no tid */ , 4 /*wct */ );
4071 smb_buffer->Mid = get_next_mid(ses->server);
4072 smb_buffer->Uid = ses->Suid;
4073 pSMB = (TCONX_REQ *) smb_buffer;
4074 pSMBr = (TCONX_RSP *) smb_buffer_response;
4076 pSMB->AndXCommand = 0xFF;
4077 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
4078 bcc_ptr = &pSMB->Password[0];
4079 if (!tcon || (ses->server->sec_mode & SECMODE_USER)) {
4080 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
4081 *bcc_ptr = 0; /* password is null byte */
4082 bcc_ptr++; /* skip password */
4083 /* already aligned so no need to do it below */
4085 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
4086 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4087 specified as required (when that support is added to
4088 the vfs in the future) as only NTLM or the much
4089 weaker LANMAN (which we do not send by default) is accepted
4090 by Samba (not sure whether other servers allow
4091 NTLMv2 password here) */
4092 #ifdef CONFIG_CIFS_WEAK_PW_HASH
4093 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
4094 (ses->sectype == LANMAN))
4095 calc_lanman_hash(tcon->password, ses->server->cryptkey,
4096 ses->server->sec_mode &
4097 SECMODE_PW_ENCRYPT ? true : false,
4100 #endif /* CIFS_WEAK_PW_HASH */
4101 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
4102 bcc_ptr, nls_codepage);
4104 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4106 cifs_buf_release(smb_buffer);
4110 bcc_ptr += CIFS_AUTH_RESP_SIZE;
4111 if (ses->capabilities & CAP_UNICODE) {
4112 /* must align unicode strings */
4113 *bcc_ptr = 0; /* null byte password */
4118 if (ses->server->sign)
4119 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4121 if (ses->capabilities & CAP_STATUS32) {
4122 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4124 if (ses->capabilities & CAP_DFS) {
4125 smb_buffer->Flags2 |= SMBFLG2_DFS;
4127 if (ses->capabilities & CAP_UNICODE) {
4128 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4130 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
4131 6 /* max utf8 char length in bytes */ *
4132 (/* server len*/ + 256 /* share len */), nls_codepage);
4133 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
4134 bcc_ptr += 2; /* skip trailing null */
4135 } else { /* ASCII */
4136 strcpy(bcc_ptr, tree);
4137 bcc_ptr += strlen(tree) + 1;
4139 strcpy(bcc_ptr, "?????");
4140 bcc_ptr += strlen("?????");
4142 count = bcc_ptr - &pSMB->Password[0];
4143 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4144 pSMB->hdr.smb_buf_length) + count);
4145 pSMB->ByteCount = cpu_to_le16(count);
4147 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
4150 /* above now done in SendReceive */
4151 if ((rc == 0) && (tcon != NULL)) {
4154 tcon->tidStatus = CifsGood;
4155 tcon->need_reconnect = false;
4156 tcon->tid = smb_buffer_response->Tid;
4157 bcc_ptr = pByteArea(smb_buffer_response);
4158 bytes_left = get_bcc(smb_buffer_response);
4159 length = strnlen(bcc_ptr, bytes_left - 2);
4160 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4166 /* skip service field (NB: this field is always ASCII) */
4168 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4169 (bcc_ptr[2] == 'C')) {
4170 cifs_dbg(FYI, "IPC connection\n");
4173 } else if (length == 2) {
4174 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4175 /* the most common case */
4176 cifs_dbg(FYI, "disk share connection\n");
4179 bcc_ptr += length + 1;
4180 bytes_left -= (length + 1);
4181 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
4183 /* mostly informational -- no need to fail on error here */
4184 kfree(tcon->nativeFileSystem);
4185 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
4186 bytes_left, is_unicode,
4189 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
4191 if ((smb_buffer_response->WordCount == 3) ||
4192 (smb_buffer_response->WordCount == 7))
4193 /* field is in same location */
4194 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4197 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
4198 } else if ((rc == 0) && tcon == NULL) {
4199 /* all we need to save for IPC$ connection */
4200 ses->ipc_tid = smb_buffer_response->Tid;
4203 cifs_buf_release(smb_buffer);
4207 static void delayed_free(struct rcu_head *p)
4209 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4210 unload_nls(sbi->local_nls);
4215 cifs_umount(struct cifs_sb_info *cifs_sb)
4217 struct rb_root *root = &cifs_sb->tlink_tree;
4218 struct rb_node *node;
4219 struct tcon_link *tlink;
4221 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4223 spin_lock(&cifs_sb->tlink_tree_lock);
4224 while ((node = rb_first(root))) {
4225 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4226 cifs_get_tlink(tlink);
4227 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4228 rb_erase(node, root);
4230 spin_unlock(&cifs_sb->tlink_tree_lock);
4231 cifs_put_tlink(tlink);
4232 spin_lock(&cifs_sb->tlink_tree_lock);
4234 spin_unlock(&cifs_sb->tlink_tree_lock);
4236 kfree(cifs_sb->mountdata);
4237 kfree(cifs_sb->prepath);
4238 call_rcu(&cifs_sb->rcu, delayed_free);
4242 cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
4245 struct TCP_Server_Info *server = ses->server;
4247 if (!server->ops->need_neg || !server->ops->negotiate)
4250 /* only send once per connect */
4251 if (!server->ops->need_neg(server))
4254 set_credits(server, 1);
4256 rc = server->ops->negotiate(xid, ses);
4258 spin_lock(&GlobalMid_Lock);
4259 if (server->tcpStatus == CifsNeedNegotiate)
4260 server->tcpStatus = CifsGood;
4263 spin_unlock(&GlobalMid_Lock);
4270 cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4271 struct nls_table *nls_info)
4274 struct TCP_Server_Info *server = ses->server;
4276 ses->capabilities = server->capabilities;
4277 if (linuxExtEnabled == 0)
4278 ses->capabilities &= (~server->vals->cap_unix);
4280 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
4281 server->sec_mode, server->capabilities, server->timeAdj);
4283 if (ses->auth_key.response) {
4284 cifs_dbg(VFS, "Free previous auth_key.response = %p\n",
4285 ses->auth_key.response);
4286 kfree(ses->auth_key.response);
4287 ses->auth_key.response = NULL;
4288 ses->auth_key.len = 0;
4291 if (server->ops->sess_setup)
4292 rc = server->ops->sess_setup(xid, ses, nls_info);
4295 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
4301 cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4303 vol->sectype = ses->sectype;
4305 /* krb5 is special, since we don't need username or pw */
4306 if (vol->sectype == Kerberos)
4309 return cifs_set_cifscreds(vol, ses);
4312 static struct cifs_tcon *
4313 cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
4316 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4317 struct cifs_ses *ses;
4318 struct cifs_tcon *tcon = NULL;
4319 struct smb_vol *vol_info;
4321 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
4322 if (vol_info == NULL)
4323 return ERR_PTR(-ENOMEM);
4325 vol_info->local_nls = cifs_sb->local_nls;
4326 vol_info->linux_uid = fsuid;
4327 vol_info->cred_uid = fsuid;
4328 vol_info->UNC = master_tcon->treeName;
4329 vol_info->retry = master_tcon->retry;
4330 vol_info->nocase = master_tcon->nocase;
4331 vol_info->local_lease = master_tcon->local_lease;
4332 vol_info->resilient = master_tcon->use_resilient;
4333 vol_info->persistent = master_tcon->use_persistent;
4334 vol_info->no_linux_ext = !master_tcon->unix_ext;
4335 vol_info->sectype = master_tcon->ses->sectype;
4336 vol_info->sign = master_tcon->ses->sign;
4337 vol_info->seal = master_tcon->seal;
4339 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4345 /* get a reference for the same TCP session */
4346 spin_lock(&cifs_tcp_ses_lock);
4347 ++master_tcon->ses->server->srv_count;
4348 spin_unlock(&cifs_tcp_ses_lock);
4350 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4352 tcon = (struct cifs_tcon *)ses;
4353 cifs_put_tcp_session(master_tcon->ses->server, 0);
4357 tcon = cifs_get_tcon(ses, vol_info);
4359 cifs_put_smb_ses(ses);
4364 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
4366 kfree(vol_info->username);
4367 kzfree(vol_info->password);
4374 cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4376 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4379 /* find and return a tlink with given uid */
4380 static struct tcon_link *
4381 tlink_rb_search(struct rb_root *root, kuid_t uid)
4383 struct rb_node *node = root->rb_node;
4384 struct tcon_link *tlink;
4387 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4389 if (uid_gt(tlink->tl_uid, uid))
4390 node = node->rb_left;
4391 else if (uid_lt(tlink->tl_uid, uid))
4392 node = node->rb_right;
4399 /* insert a tcon_link into the tree */
4401 tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4403 struct rb_node **new = &(root->rb_node), *parent = NULL;
4404 struct tcon_link *tlink;
4407 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4410 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
4411 new = &((*new)->rb_left);
4413 new = &((*new)->rb_right);
4416 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4417 rb_insert_color(&new_tlink->tl_rbnode, root);
4421 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4424 * If the superblock doesn't refer to a multiuser mount, then just return
4425 * the master tcon for the mount.
4427 * First, search the rbtree for an existing tcon for this fsuid. If one
4428 * exists, then check to see if it's pending construction. If it is then wait
4429 * for construction to complete. Once it's no longer pending, check to see if
4430 * it failed and either return an error or retry construction, depending on
4433 * If one doesn't exist then insert a new tcon_link struct into the tree and
4434 * try to construct a new one.
4437 cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4440 kuid_t fsuid = current_fsuid();
4441 struct tcon_link *tlink, *newtlink;
4443 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4444 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4446 spin_lock(&cifs_sb->tlink_tree_lock);
4447 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
4449 cifs_get_tlink(tlink);
4450 spin_unlock(&cifs_sb->tlink_tree_lock);
4452 if (tlink == NULL) {
4453 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4454 if (newtlink == NULL)
4455 return ERR_PTR(-ENOMEM);
4456 newtlink->tl_uid = fsuid;
4457 newtlink->tl_tcon = ERR_PTR(-EACCES);
4458 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4459 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4460 cifs_get_tlink(newtlink);
4462 spin_lock(&cifs_sb->tlink_tree_lock);
4463 /* was one inserted after previous search? */
4464 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
4466 cifs_get_tlink(tlink);
4467 spin_unlock(&cifs_sb->tlink_tree_lock);
4469 goto wait_for_construction;
4472 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4473 spin_unlock(&cifs_sb->tlink_tree_lock);
4475 wait_for_construction:
4476 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
4477 TASK_INTERRUPTIBLE);
4479 cifs_put_tlink(tlink);
4480 return ERR_PTR(-ERESTARTSYS);
4483 /* if it's good, return it */
4484 if (!IS_ERR(tlink->tl_tcon))
4487 /* return error if we tried this already recently */
4488 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4489 cifs_put_tlink(tlink);
4490 return ERR_PTR(-EACCES);
4493 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4494 goto wait_for_construction;
4497 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4498 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4499 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4501 if (IS_ERR(tlink->tl_tcon)) {
4502 cifs_put_tlink(tlink);
4503 return ERR_PTR(-EACCES);
4510 * periodic workqueue job that scans tcon_tree for a superblock and closes
4514 cifs_prune_tlinks(struct work_struct *work)
4516 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4518 struct rb_root *root = &cifs_sb->tlink_tree;
4519 struct rb_node *node = rb_first(root);
4520 struct rb_node *tmp;
4521 struct tcon_link *tlink;
4524 * Because we drop the spinlock in the loop in order to put the tlink
4525 * it's not guarded against removal of links from the tree. The only
4526 * places that remove entries from the tree are this function and
4527 * umounts. Because this function is non-reentrant and is canceled
4528 * before umount can proceed, this is safe.
4530 spin_lock(&cifs_sb->tlink_tree_lock);
4531 node = rb_first(root);
4532 while (node != NULL) {
4534 node = rb_next(tmp);
4535 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4537 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4538 atomic_read(&tlink->tl_count) != 0 ||
4539 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4542 cifs_get_tlink(tlink);
4543 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4544 rb_erase(tmp, root);
4546 spin_unlock(&cifs_sb->tlink_tree_lock);
4547 cifs_put_tlink(tlink);
4548 spin_lock(&cifs_sb->tlink_tree_lock);
4550 spin_unlock(&cifs_sb->tlink_tree_lock);
4552 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,