📄 connect.c
字号:
/* * fs/cifs/connect.c * * Copyright (C) International Business Machines Corp., 2002,2004 * Author(s): Steve French (sfrench@us.ibm.com) * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See * the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <linux/fs.h>#include <linux/net.h>#include <linux/string.h>#include <linux/list.h>#include <linux/wait.h>#include <linux/version.h>#include <linux/ipv6.h>#include <linux/pagemap.h>#include <linux/ctype.h>#include <linux/utsname.h>#include <linux/mempool.h>#include <asm/uaccess.h>#include <asm/processor.h>#include "cifspdu.h"#include "cifsglob.h"#include "cifsproto.h"#include "cifs_unicode.h"#include "cifs_debug.h"#include "cifs_fs_sb.h"#include "ntlmssp.h"#include "nterr.h"#include "rfc1002pdu.h"#define CIFS_PORT 445#define RFC1001_PORT 139extern void SMBencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24);extern void SMBNTencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24);extern int cifs_inet_pton(int, const char *, void *dst);extern mempool_t *cifs_req_poolp;struct smb_vol { char *username; char *password; char *domainname; char *UNC; char *UNCip; char *iocharset; /* local code page for mapping to and from Unicode */ char source_rfc1001_name[16]; /* netbios name of client */ uid_t linux_uid; gid_t linux_gid; mode_t file_mode; mode_t dir_mode; unsigned rw:1; unsigned retry:1; unsigned intr:1; unsigned setuids:1; unsigned noperm:1; unsigned int rsize; unsigned int wsize; unsigned int sockopt; unsigned short int port;};static int ipv4_connect(struct sockaddr_in *psin_server, struct socket **csocket, char * netb_name);static int ipv6_connect(struct sockaddr_in6 *psin_server, struct socket **csocket); /* * cifs tcp session reconnection * * mark tcp session as reconnecting so temporarily locked * mark all smb sessions as reconnecting for tcp session * reconnect tcp session * wake up waiters on reconnection? - (not needed currently) */intcifs_reconnect(struct TCP_Server_Info *server){ int rc = 0; struct list_head *tmp; struct cifsSesInfo *ses; struct cifsTconInfo *tcon; struct mid_q_entry * mid_entry; spin_lock(&GlobalMid_Lock); if(server->tcpStatus == CifsExiting) { /* the demux thread will exit normally next time through the loop */ spin_unlock(&GlobalMid_Lock); return rc; } else server->tcpStatus = CifsNeedReconnect; spin_unlock(&GlobalMid_Lock); server->maxBuf = 0; cFYI(1, ("Reconnecting tcp session ")); /* before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they are not used until reconnected */ read_lock(&GlobalSMBSeslock); list_for_each(tmp, &GlobalSMBSessionList) { ses = list_entry(tmp, struct cifsSesInfo, cifsSessionList); if (ses->server) { if (ses->server == server) { ses->status = CifsNeedReconnect; ses->ipc_tid = 0; } } /* else tcp and smb sessions need reconnection */ } list_for_each(tmp, &GlobalTreeConnectionList) { tcon = list_entry(tmp, struct cifsTconInfo, cifsConnectionList); if((tcon) && (tcon->ses) && (tcon->ses->server == server)) { tcon->tidStatus = CifsNeedReconnect; } } read_unlock(&GlobalSMBSeslock); /* do not want to be sending data on a socket we are freeing */ down(&server->tcpSem); if(server->ssocket) { cFYI(1,("State: 0x%x Flags: 0x%lx", server->ssocket->state, server->ssocket->flags)); server->ssocket->ops->shutdown(server->ssocket,SEND_SHUTDOWN); cFYI(1,("Post shutdown state: 0x%x Flags: 0x%lx", server->ssocket->state, server->ssocket->flags)); sock_release(server->ssocket); server->ssocket = NULL; } spin_lock(&GlobalMid_Lock); list_for_each(tmp, &server->pending_mid_q) { mid_entry = list_entry(tmp, struct mid_q_entry, qhead); if(mid_entry) { if(mid_entry->midState == MID_REQUEST_SUBMITTED) { /* Mark other intransit requests as needing retry so we do not immediately mark the session bad again (ie after we reconnect below) as they timeout too */ mid_entry->midState = MID_RETRY_NEEDED; } } } spin_unlock(&GlobalMid_Lock); up(&server->tcpSem); while ((server->tcpStatus != CifsExiting) && (server->tcpStatus != CifsGood)) { if(server->protocolType == IPV6) { rc = ipv6_connect(&server->addr.sockAddr6,&server->ssocket); } else { rc = ipv4_connect(&server->addr.sockAddr, &server->ssocket, server->workstation_RFC1001_name); } if(rc) { set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(3 * HZ); } else { atomic_inc(&tcpSesReconnectCount); spin_lock(&GlobalMid_Lock); if(server->tcpStatus != CifsExiting) server->tcpStatus = CifsGood; spin_unlock(&GlobalMid_Lock); /* atomic_set(&server->inFlight,0);*/ wake_up(&server->response_q); } } return rc;}static intcifs_demultiplex_thread(struct TCP_Server_Info *server){ int length; unsigned int pdu_length, total_read; struct smb_hdr *smb_buffer = NULL; struct msghdr smb_msg; struct kvec iov; struct socket *csocket = server->ssocket; struct list_head *tmp; struct cifsSesInfo *ses; struct task_struct *task_to_wake = NULL; struct mid_q_entry *mid_entry; char *temp; daemonize("cifsd"); allow_signal(SIGKILL); current->flags |= PF_MEMALLOC; server->tsk = current; /* save process info to wake at shutdown */ cFYI(1, ("Demultiplex PID: %d", current->pid)); write_lock(&GlobalSMBSeslock); atomic_inc(&tcpSesAllocCount); length = tcpSesAllocCount.counter; write_unlock(&GlobalSMBSeslock); if(length > 1) { mempool_resize(cifs_req_poolp, length + CIFS_MIN_RCV_POOL, GFP_KERNEL); } while (server->tcpStatus != CifsExiting) { if (smb_buffer == NULL) smb_buffer = cifs_buf_get(); else memset(smb_buffer, 0, sizeof (struct smb_hdr)); if (smb_buffer == NULL) { cERROR(1,("Can not get memory for SMB response")); set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(HZ * 3); /* give system time to free memory */ continue; } iov.iov_base = smb_buffer; iov.iov_len = sizeof (struct smb_hdr) - 1; /* 1 byte less above since wct is not always returned in error cases */ smb_msg.msg_control = NULL; smb_msg.msg_controllen = 0; length = kernel_recvmsg(csocket, &smb_msg, &iov, 1, sizeof (struct smb_hdr) - 1 /* RFC1001 header and SMB header */ , MSG_PEEK /* flags see socket.h */ ); if(server->tcpStatus == CifsExiting) { break; } else if (server->tcpStatus == CifsNeedReconnect) { cFYI(1,("Reconnecting after server stopped responding")); cifs_reconnect(server); cFYI(1,("call to reconnect done")); csocket = server->ssocket; continue; } else if ((length == -ERESTARTSYS) || (length == -EAGAIN)) { set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(1); /* minimum sleep to prevent looping allowing socket to clear and app threads to set tcpStatus CifsNeedReconnect if server hung */ continue; } else if (length <= 0) { if(server->tcpStatus == CifsNew) { cFYI(1,("tcp session abended prematurely (after SMBnegprot)")); /* some servers kill tcp session rather than returning smb negprot error in which case reconnecting here is not going to help - return error to mount */ break; } if(length == -EINTR) { cFYI(1,("cifsd thread killed")); break; } cFYI(1,("Reconnecting after unexpected peek error %d",length)); cifs_reconnect(server); csocket = server->ssocket; wake_up(&server->response_q); continue; } pdu_length = 4 + ntohl(smb_buffer->smb_buf_length); /* Ony read pdu_length after below checks for too short (due to e.g. int overflow) and too long ie beyond end of buf */ cFYI(1, ("Peek length rcvd: 0x%x beginning 0x%x)", length, pdu_length)); temp = (char *) smb_buffer; if (length > 3) { if (temp[0] == (char) RFC1002_SESSION_KEEP_ALIVE) { iov.iov_base = smb_buffer; iov.iov_len = 4; length = kernel_recvmsg(csocket, &smb_msg, &iov, 1, 4, 0); cFYI(0,("Received 4 byte keep alive packet")); } else if (temp[0] == (char) RFC1002_POSITIVE_SESSION_RESPONSE) { iov.iov_base = smb_buffer; iov.iov_len = 4; length = kernel_recvmsg(csocket, &smb_msg, &iov, 1, 4, 0); cFYI(1,("Good RFC 1002 session rsp")); } else if ((temp[0] == (char)RFC1002_NEGATIVE_SESSION_RESPONSE) && (length == 5)) { /* we get this from Windows 98 instead of error on SMB negprot response */ cFYI(1,("Negative RFC 1002 Session Response Error 0x%x)",temp[4])); if(server->tcpStatus == CifsNew) { /* if nack on negprot (rather than ret of smb negprot error) reconnecting not going to help, ret error to mount */ break; } else { /* give server a second to clean up before reconnect attempt */ set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(HZ); /* always try 445 first on reconnect since we get NACK on some if we ever connected to port 139 (the NACK is since we do not begin with RFC1001 session initialize frame) */ server->addr.sockAddr.sin_port = htons(CIFS_PORT); cifs_reconnect(server); csocket = server->ssocket; wake_up(&server->response_q); continue; } } else if (temp[0] != (char) 0) { cERROR(1,("Unknown RFC 1002 frame")); cifs_dump_mem(" Received Data: ", temp, length); cifs_reconnect(server); csocket = server->ssocket; continue; } else { if ((length != sizeof (struct smb_hdr) - 1) || (pdu_length > CIFS_MAX_MSGSIZE + MAX_CIFS_HDR_SIZE) || (pdu_length < sizeof (struct smb_hdr) - 1) || (checkSMBhdr (smb_buffer, smb_buffer->Mid))) { cERROR(1, ("Invalid size or format for SMB found with length %d and pdu_lenght %d", length, pdu_length)); cifs_dump_mem("Received Data is: ",temp,sizeof(struct smb_hdr)); /* could we fix this network corruption by finding next smb header (instead of killing the session) and restart reading from next valid SMB found? */ cifs_reconnect(server); csocket = server->ssocket; continue; } else { /* length ok */ length = 0; iov.iov_base = smb_buffer; iov.iov_len = pdu_length; for (total_read = 0; total_read < pdu_length; total_read += length) { length = kernel_recvmsg(csocket, &smb_msg, &iov, 1, pdu_length - total_read, 0); if (length == 0) { cERROR(1, ("Zero length receive when expecting %d ", pdu_length - total_read)); cifs_reconnect(server); csocket = server->ssocket; continue; } } } dump_smb(smb_buffer, length); if (checkSMB (smb_buffer, smb_buffer->Mid, total_read)) { cERROR(1, ("Bad SMB Received ")); continue; } task_to_wake = NULL; spin_lock(&GlobalMid_Lock); list_for_each(tmp, &server->pending_mid_q) { mid_entry = list_entry(tmp, struct mid_q_entry, qhead); if ((mid_entry->mid == smb_buffer->Mid) && (mid_entry->midState == MID_REQUEST_SUBMITTED)) { cFYI(1, (" Mid 0x%x matched - waking up ",mid_entry->mid)); task_to_wake = mid_entry->tsk; mid_entry->resp_buf = smb_buffer; mid_entry->midState = MID_RESPONSE_RECEIVED; } } spin_unlock(&GlobalMid_Lock); if (task_to_wake) { smb_buffer = NULL; /* will be freed by users thread after he is done */ wake_up_process(task_to_wake); } else if (is_valid_oplock_break(smb_buffer) == FALSE) { cERROR(1, ("No task to wake, unknown frame rcvd!")); cifs_dump_mem("Received Data is: ",temp,sizeof(struct smb_hdr)); } } } else { cFYI(0, ("Frame less than four bytes received %d bytes long.", length)); if (length > 0) { length = kernel_recvmsg(csocket, &smb_msg, &iov, 1, length, 0); /* throw away junk frame */ cFYI(1, (" with junk 0x%x in it ", *(__u32 *) smb_buffer)); } } } spin_lock(&GlobalMid_Lock); server->tcpStatus = CifsExiting; server->tsk = NULL; atomic_set(&server->inFlight, 0); spin_unlock(&GlobalMid_Lock); /* Although there should not be any requests blocked on this queue it can not hurt to be paranoid and try to wake up requests that may haven been blocked when more than 50 at time were on the wire to the same server - they now will see the session is in exit state and get out of SendReceive. */ wake_up_all(&server->request_q); /* give those requests time to exit */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -