cifs: set up recurring workqueue job to do SMB echo requests
[cascardo/linux.git] / fs / cifs / connect.c
index cc1a860..f38ca08 100644 (file)
@@ -52,6 +52,9 @@
 #define CIFS_PORT 445
 #define RFC1001_PORT 139
 
+/* SMB echo "timeout" -- FIXME: tunable? */
+#define SMB_ECHO_INTERVAL (60 * HZ)
+
 extern void SMBNTencrypt(unsigned char *passwd, unsigned char *c8,
                         unsigned char *p24);
 
@@ -64,8 +67,8 @@ struct smb_vol {
        char *UNC;
        char *UNCip;
        char *iocharset;  /* local code page for mapping to and from Unicode */
-       char source_rfc1001_name[16]; /* netbios name of client */
-       char target_rfc1001_name[16]; /* netbios name of server for Win9x/ME */
+       char source_rfc1001_name[RFC1001_NAME_LEN_WITH_NULL]; /* clnt nb name */
+       char target_rfc1001_name[RFC1001_NAME_LEN_WITH_NULL]; /* srvr nb name */
        uid_t cred_uid;
        uid_t linux_uid;
        gid_t linux_gid;
@@ -115,8 +118,8 @@ struct smb_vol {
 #define TLINK_ERROR_EXPIRE     (1 * HZ)
 #define TLINK_IDLE_EXPIRE      (600 * HZ)
 
-static int ipv4_connect(struct TCP_Server_Info *server);
-static int ipv6_connect(struct TCP_Server_Info *server);
+static int ip_connect(struct TCP_Server_Info *server);
+static int generic_ip_connect(struct TCP_Server_Info *server);
 static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
 static void cifs_prune_tlinks(struct work_struct *work);
 
@@ -152,6 +155,7 @@ cifs_reconnect(struct TCP_Server_Info *server)
 
        /* before reconnecting the tcp session, mark the smb session (uid)
                and the tid bad so they are not used until reconnected */
+       cFYI(1, "%s: marking sessions and tcons for reconnect", __func__);
        spin_lock(&cifs_tcp_ses_lock);
        list_for_each(tmp, &server->smb_ses_list) {
                ses = list_entry(tmp, struct cifsSesInfo, smb_ses_list);
@@ -163,7 +167,9 @@ cifs_reconnect(struct TCP_Server_Info *server)
                }
        }
        spin_unlock(&cifs_tcp_ses_lock);
+
        /* do not want to be sending data on a socket we are freeing */
+       cFYI(1, "%s: tearing down socket", __func__);
        mutex_lock(&server->srv_mutex);
        if (server->ssocket) {
                cFYI(1, "State: 0x%x Flags: 0x%lx", server->ssocket->state,
@@ -180,30 +186,26 @@ cifs_reconnect(struct TCP_Server_Info *server)
        kfree(server->session_key.response);
        server->session_key.response = NULL;
        server->session_key.len = 0;
+       mutex_unlock(&server->srv_mutex);
 
+       /* mark submitted MIDs for retry and issue callback */
+       cFYI(1, "%s: issuing mid callbacks", __func__);
        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->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 */
+       list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
+               mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
+               if (mid_entry->midState == MID_REQUEST_SUBMITTED)
                        mid_entry->midState = MID_RETRY_NEEDED;
-               }
+               list_del_init(&mid_entry->qhead);
+               mid_entry->callback(mid_entry);
        }
        spin_unlock(&GlobalMid_Lock);
-       mutex_unlock(&server->srv_mutex);
 
        while ((server->tcpStatus != CifsExiting) &&
               (server->tcpStatus != CifsGood)) {
                try_to_freeze();
-               if (server->addr.sockAddr6.sin6_family == AF_INET6)
-                       rc = ipv6_connect(server);
-               else
-                       rc = ipv4_connect(server);
+
+               /* we should try only the port we connected to before */
+               rc = generic_ip_connect(server);
                if (rc) {
                        cFYI(1, "reconnect error %d", rc);
                        msleep(3000);
@@ -213,10 +215,9 @@ cifs_reconnect(struct TCP_Server_Info *server)
                        if (server->tcpStatus != CifsExiting)
                                server->tcpStatus = CifsGood;
                        spin_unlock(&GlobalMid_Lock);
-       /*              atomic_set(&server->inFlight,0);*/
-                       wake_up(&server->response_q);
                }
        }
+
        return rc;
 }
 
@@ -335,6 +336,26 @@ static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
 
 }
 
+static void
+cifs_echo_request(struct work_struct *work)
+{
+       int rc;
+       struct TCP_Server_Info *server = container_of(work,
+                                       struct TCP_Server_Info, echo.work);
+
+       /* no need to ping if we got a response recently */
+       if (time_before(jiffies, server->lstrp + SMB_ECHO_INTERVAL - HZ))
+               goto requeue_echo;
+
+       rc = CIFSSMBEcho(server);
+       if (rc)
+               cFYI(1, "Unable to send echo request to server: %s",
+                       server->hostname);
+
+requeue_echo:
+       queue_delayed_work(system_nrt_wq, &server->echo, SMB_ECHO_INTERVAL);
+}
+
 static int
 cifs_demultiplex_thread(struct TCP_Server_Info *server)
 {
@@ -346,8 +367,7 @@ cifs_demultiplex_thread(struct TCP_Server_Info *server)
        struct msghdr smb_msg;
        struct kvec iov;
        struct socket *csocket = server->ssocket;
-       struct list_head *tmp;
-       struct cifsSesInfo *ses;
+       struct list_head *tmp, *tmp2;
        struct task_struct *task_to_wake = NULL;
        struct mid_q_entry *mid_entry;
        char temp;
@@ -477,7 +497,7 @@ incomplete_rcv:
                         * initialize frame)
                         */
                        cifs_set_port((struct sockaddr *)
-                                       &server->addr.sockAddr, CIFS_PORT);
+                                       &server->dstaddr, CIFS_PORT);
                        cifs_reconnect(server);
                        csocket = server->ssocket;
                        wake_up(&server->response_q);
@@ -560,10 +580,9 @@ incomplete_rcv:
                        continue;
                }
 
-
-               task_to_wake = NULL;
+               mid_entry = NULL;
                spin_lock(&GlobalMid_Lock);
-               list_for_each(tmp, &server->pending_mid_q) {
+               list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
                        mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
 
                        if ((mid_entry->mid == smb_buffer->Mid) &&
@@ -604,8 +623,9 @@ incomplete_rcv:
                                mid_entry->resp_buf = smb_buffer;
                                mid_entry->largeBuf = isLargeBuf;
 multi_t2_fnd:
-                               task_to_wake = mid_entry->tsk;
                                mid_entry->midState = MID_RESPONSE_RECEIVED;
+                               list_del_init(&mid_entry->qhead);
+                               mid_entry->callback(mid_entry);
 #ifdef CONFIG_CIFS_STATS2
                                mid_entry->when_received = jiffies;
 #endif
@@ -615,9 +635,11 @@ multi_t2_fnd:
                                server->lstrp = jiffies;
                                break;
                        }
+                       mid_entry = NULL;
                }
                spin_unlock(&GlobalMid_Lock);
-               if (task_to_wake) {
+
+               if (mid_entry != NULL) {
                        /* Was previous buf put in mpx struct for multi-rsp? */
                        if (!isMultiRsp) {
                                /* smb buffer will be freed by user thread */
@@ -626,11 +648,10 @@ multi_t2_fnd:
                                else
                                        smallbuf = NULL;
                        }
-                       wake_up_process(task_to_wake);
                } else if (!is_valid_oplock_break(smb_buffer, server) &&
                           !isMultiRsp) {
                        cERROR(1, "No task to wake, unknown frame received! "
-                                  "NumMids %d", midCount.counter);
+                                  "NumMids %d", atomic_read(&midCount));
                        cifs_dump_mem("Received Data is: ", (char *)smb_buffer,
                                      sizeof(struct smb_hdr));
 #ifdef CONFIG_CIFS_DEBUG2
@@ -678,44 +699,16 @@ multi_t2_fnd:
        if (smallbuf) /* no sense logging a debug message if NULL */
                cifs_small_buf_release(smallbuf);
 
-       /*
-        * BB: we shouldn't have to do any of this. It shouldn't be
-        * possible to exit from the thread with active SMB sessions
-        */
-       spin_lock(&cifs_tcp_ses_lock);
-       if (list_empty(&server->pending_mid_q)) {
-               /* loop through server session structures attached to this and
-                   mark them dead */
-               list_for_each(tmp, &server->smb_ses_list) {
-                       ses = list_entry(tmp, struct cifsSesInfo,
-                                        smb_ses_list);
-                       ses->status = CifsExiting;
-                       ses->server = NULL;
-               }
-               spin_unlock(&cifs_tcp_ses_lock);
-       } else {
-               /* although we can not zero the server struct pointer yet,
-               since there are active requests which may depnd on them,
-               mark the corresponding SMB sessions as exiting too */
-               list_for_each(tmp, &server->smb_ses_list) {
-                       ses = list_entry(tmp, struct cifsSesInfo,
-                                        smb_ses_list);
-                       ses->status = CifsExiting;
-               }
-
+       if (!list_empty(&server->pending_mid_q)) {
                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->midState == MID_REQUEST_SUBMITTED) {
-                               cFYI(1, "Clearing Mid 0x%x - waking up ",
+               list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
+                       mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
+                       cFYI(1, "Clearing Mid 0x%x - issuing callback",
                                         mid_entry->mid);
-                               task_to_wake = mid_entry->tsk;
-                               if (task_to_wake)
-                                       wake_up_process(task_to_wake);
-                       }
+                       list_del_init(&mid_entry->qhead);
+                       mid_entry->callback(mid_entry);
                }
                spin_unlock(&GlobalMid_Lock);
-               spin_unlock(&cifs_tcp_ses_lock);
                /* 1/8th of sec is more than enough time for them to exit */
                msleep(125);
        }
@@ -733,18 +726,6 @@ multi_t2_fnd:
                coming home not much else we can do but free the memory */
        }
 
-       /* last chance to mark ses pointers invalid
-       if there are any pointing to this (e.g
-       if a crazy root user tried to kill cifsd
-       kernel thread explicitly this might happen) */
-       /* BB: This shouldn't be necessary, see above */
-       spin_lock(&cifs_tcp_ses_lock);
-       list_for_each(tmp, &server->smb_ses_list) {
-               ses = list_entry(tmp, struct cifsSesInfo, smb_ses_list);
-               ses->server = NULL;
-       }
-       spin_unlock(&cifs_tcp_ses_lock);
-
        kfree(server->hostname);
        task_to_wake = xchg(&server->tsk, NULL);
        kfree(server);
@@ -817,11 +798,11 @@ cifs_parse_mount_options(char *options, const char *devname,
         * informational, only used for servers that do not support
         * port 445 and it can be overridden at mount time
         */
-       memset(vol->source_rfc1001_name, 0x20, 15);
-       for (i = 0; i < strnlen(nodename, 15); i++)
+       memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
+       for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
                vol->source_rfc1001_name[i] = toupper(nodename[i]);
 
-       vol->source_rfc1001_name[15] = 0;
+       vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
        /* null target name indicates to use *SMBSERVR default called name
           if we end up sending RFC1001 session initialize */
        vol->target_rfc1001_name[0] = 0;
@@ -985,13 +966,11 @@ cifs_parse_mount_options(char *options, const char *devname,
                                return 1;
                        } else if (strnicmp(value, "krb5", 4) == 0) {
                                vol->secFlg |= CIFSSEC_MAY_KRB5;
-#ifdef CONFIG_CIFS_EXPERIMENTAL
                        } else if (strnicmp(value, "ntlmsspi", 8) == 0) {
                                vol->secFlg |= CIFSSEC_MAY_NTLMSSP |
                                        CIFSSEC_MUST_SIGN;
                        } else if (strnicmp(value, "ntlmssp", 7) == 0) {
                                vol->secFlg |= CIFSSEC_MAY_NTLMSSP;
-#endif
                        } else if (strnicmp(value, "ntlmv2i", 7) == 0) {
                                vol->secFlg |= CIFSSEC_MAY_NTLMV2 |
                                        CIFSSEC_MUST_SIGN;
@@ -1116,6 +1095,8 @@ cifs_parse_mount_options(char *options, const char *devname,
                } else if (!strnicmp(data, "uid", 3) && value && *value) {
                        vol->linux_uid = simple_strtoul(value, &value, 0);
                        uid_specified = true;
+               } else if (!strnicmp(data, "cruid", 5) && value && *value) {
+                       vol->cred_uid = simple_strtoul(value, &value, 0);
                } else if (!strnicmp(data, "forceuid", 8)) {
                        override_uid = 1;
                } else if (!strnicmp(data, "noforceuid", 10)) {
@@ -1168,22 +1149,22 @@ cifs_parse_mount_options(char *options, const char *devname,
                        if (!value || !*value || (*value == ' ')) {
                                cFYI(1, "invalid (empty) netbiosname");
                        } else {
-                               memset(vol->source_rfc1001_name, 0x20, 15);
-                               for (i = 0; i < 15; i++) {
-                               /* BB are there cases in which a comma can be
-                               valid in this workstation netbios name (and need
-                               special handling)? */
-
-                               /* We do not uppercase netbiosname for user */
+                               memset(vol->source_rfc1001_name, 0x20,
+                                       RFC1001_NAME_LEN);
+                               /*
+                                * FIXME: are there cases in which a comma can
+                                * be valid in workstation netbios name (and
+                                * need special handling)?
+                                */
+                               for (i = 0; i < RFC1001_NAME_LEN; i++) {
+                                       /* don't ucase netbiosname for user */
                                        if (value[i] == 0)
                                                break;
-                                       else
-                                               vol->source_rfc1001_name[i] =
-                                                               value[i];
+                                       vol->source_rfc1001_name[i] = value[i];
                                }
                                /* The string has 16th byte zero still from
                                set at top of the function  */
-                               if ((i == 15) && (value[i] != 0))
+                               if (i == RFC1001_NAME_LEN && value[i] != 0)
                                        printk(KERN_WARNING "CIFS: netbiosname"
                                                " longer than 15 truncated.\n");
                        }
@@ -1193,7 +1174,8 @@ cifs_parse_mount_options(char *options, const char *devname,
                                cFYI(1, "empty server netbiosname specified");
                        } else {
                                /* last byte, type, is 0x20 for servr type */
-                               memset(vol->target_rfc1001_name, 0x20, 16);
+                               memset(vol->target_rfc1001_name, 0x20,
+                                       RFC1001_NAME_LEN_WITH_NULL);
 
                                for (i = 0; i < 15; i++) {
                                /* BB are there cases in which a comma can be
@@ -1210,7 +1192,7 @@ cifs_parse_mount_options(char *options, const char *devname,
                                }
                                /* The string has 16th byte zero still from
                                   set at top of the function  */
-                               if ((i == 15) && (value[i] != 0))
+                               if (i == RFC1001_NAME_LEN && value[i] != 0)
                                        printk(KERN_WARNING "CIFS: server net"
                                        "biosname longer than 15 truncated.\n");
                        }
@@ -1341,10 +1323,8 @@ cifs_parse_mount_options(char *options, const char *devname,
                        vol->no_psx_acl = 0;
                } else if (strnicmp(data, "noacl", 5) == 0) {
                        vol->no_psx_acl = 1;
-#ifdef CONFIG_CIFS_EXPERIMENTAL
                } else if (strnicmp(data, "locallease", 6) == 0) {
                        vol->local_lease = 1;
-#endif
                } else if (strnicmp(data, "sign", 4) == 0) {
                        vol->secFlg |= CIFSSEC_MUST_SIGN;
                } else if (strnicmp(data, "seal", 4) == 0) {
@@ -1454,35 +1434,71 @@ srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
        }
 }
 
+/*
+ * If no port is specified in addr structure, we try to match with 445 port
+ * and if it fails - with 139 ports. It should be called only if address
+ * families of server and addr are equal.
+ */
+static bool
+match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
+{
+       unsigned short int port, *sport;
+
+       switch (addr->sa_family) {
+       case AF_INET:
+               sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
+               port = ((struct sockaddr_in *) addr)->sin_port;
+               break;
+       case AF_INET6:
+               sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
+               port = ((struct sockaddr_in6 *) addr)->sin6_port;
+               break;
+       default:
+               WARN_ON(1);
+               return false;
+       }
+
+       if (!port) {
+               port = htons(CIFS_PORT);
+               if (port == *sport)
+                       return true;
+
+               port = htons(RFC1001_PORT);
+       }
+
+       return port == *sport;
+}
 
 static bool
 match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
              struct sockaddr *srcaddr)
 {
-       struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
-       struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
-
        switch (addr->sa_family) {
-       case AF_INET:
-               if (addr4->sin_addr.s_addr !=
-                   server->addr.sockAddr.sin_addr.s_addr)
-                       return false;
-               if (addr4->sin_port &&
-                   addr4->sin_port != server->addr.sockAddr.sin_port)
+       case AF_INET: {
+               struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
+               struct sockaddr_in *srv_addr4 =
+                                       (struct sockaddr_in *)&server->dstaddr;
+
+               if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
                        return false;
                break;
-       case AF_INET6:
+       }
+       case AF_INET6: {
+               struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
+               struct sockaddr_in6 *srv_addr6 =
+                                       (struct sockaddr_in6 *)&server->dstaddr;
+
                if (!ipv6_addr_equal(&addr6->sin6_addr,
-                                    &server->addr.sockAddr6.sin6_addr))
+                                    &srv_addr6->sin6_addr))
                        return false;
-               if (addr6->sin6_scope_id !=
-                   server->addr.sockAddr6.sin6_scope_id)
-                       return false;
-               if (addr6->sin6_port &&
-                   addr6->sin6_port != server->addr.sockAddr6.sin6_port)
+               if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
                        return false;
                break;
        }
+       default:
+               WARN_ON(1);
+               return false; /* don't expect to be here */
+       }
 
        if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
                return false;
@@ -1549,6 +1565,9 @@ cifs_find_tcp_session(struct sockaddr *addr, struct smb_vol *vol)
                                   (struct sockaddr *)&vol->srcaddr))
                        continue;
 
+               if (!match_port(server, addr))
+                       continue;
+
                if (!match_security(server, vol))
                        continue;
 
@@ -1575,6 +1594,8 @@ cifs_put_tcp_session(struct TCP_Server_Info *server)
        list_del_init(&server->tcp_ses_list);
        spin_unlock(&cifs_tcp_ses_lock);
 
+       cancel_delayed_work_sync(&server->echo);
+
        spin_lock(&GlobalMid_Lock);
        server->tcpStatus = CifsExiting;
        spin_unlock(&GlobalMid_Lock);
@@ -1666,6 +1687,7 @@ cifs_get_tcp_session(struct smb_vol *volume_info)
        tcp_ses->sequence_number = 0;
        INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
        INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
+       INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
 
        /*
         * at this point we are the only ones with the pointer
@@ -1681,14 +1703,13 @@ cifs_get_tcp_session(struct smb_vol *volume_info)
                cFYI(1, "attempting ipv6 connect");
                /* BB should we allow ipv6 on port 139? */
                /* other OS never observed in Wild doing 139 with v6 */
-               memcpy(&tcp_ses->addr.sockAddr6, sin_server6,
-                       sizeof(struct sockaddr_in6));
-               rc = ipv6_connect(tcp_ses);
-       } else {
-               memcpy(&tcp_ses->addr.sockAddr, sin_server,
-                       sizeof(struct sockaddr_in));
-               rc = ipv4_connect(tcp_ses);
-       }
+               memcpy(&tcp_ses->dstaddr, sin_server6,
+                      sizeof(struct sockaddr_in6));
+       } else
+               memcpy(&tcp_ses->dstaddr, sin_server,
+                      sizeof(struct sockaddr_in));
+
+       rc = ip_connect(tcp_ses);
        if (rc < 0) {
                cERROR(1, "Error connecting to socket. Aborting operation");
                goto out_err_crypto_release;
@@ -1715,6 +1736,9 @@ cifs_get_tcp_session(struct smb_vol *volume_info)
 
        cifs_fscache_get_client_cookie(tcp_ses);
 
+       /* queue echo request delayed work */
+       queue_delayed_work(system_nrt_wq, &tcp_ses->echo, SMB_ECHO_INTERVAL);
+
        return tcp_ses;
 
 out_err_crypto_release:
@@ -1793,6 +1817,8 @@ cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
 {
        int rc = -ENOMEM, xid;
        struct cifsSesInfo *ses;
+       struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
+       struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
 
        xid = GetXid();
 
@@ -1836,12 +1862,10 @@ cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
 
        /* new SMB session uses our server ref */
        ses->server = server;
-       if (server->addr.sockAddr6.sin6_family == AF_INET6)
-               sprintf(ses->serverName, "%pI6",
-                       &server->addr.sockAddr6.sin6_addr);
+       if (server->dstaddr.ss_family == AF_INET6)
+               sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
        else
-               sprintf(ses->serverName, "%pI4",
-                       &server->addr.sockAddr.sin_addr.s_addr);
+               sprintf(ses->serverName, "%pI4", &addr->sin_addr);
 
        if (volume_info->username)
                strncpy(ses->userName, volume_info->username,
@@ -2136,19 +2160,106 @@ bind_socket(struct TCP_Server_Info *server)
 }
 
 static int
-ipv4_connect(struct TCP_Server_Info *server)
+ip_rfc1001_connect(struct TCP_Server_Info *server)
 {
        int rc = 0;
-       int val;
-       bool connected = false;
-       __be16 orig_port = 0;
+       /*
+        * some servers require RFC1001 sessinit before sending
+        * negprot - BB check reconnection in case where second
+        * sessinit is sent but no second negprot
+        */
+       struct rfc1002_session_packet *ses_init_buf;
+       struct smb_hdr *smb_buf;
+       ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
+                              GFP_KERNEL);
+       if (ses_init_buf) {
+               ses_init_buf->trailer.session_req.called_len = 32;
+
+               if (server->server_RFC1001_name &&
+                   server->server_RFC1001_name[0] != 0)
+                       rfc1002mangle(ses_init_buf->trailer.
+                                     session_req.called_name,
+                                     server->server_RFC1001_name,
+                                     RFC1001_NAME_LEN_WITH_NULL);
+               else
+                       rfc1002mangle(ses_init_buf->trailer.
+                                     session_req.called_name,
+                                     DEFAULT_CIFS_CALLED_NAME,
+                                     RFC1001_NAME_LEN_WITH_NULL);
+
+               ses_init_buf->trailer.session_req.calling_len = 32;
+
+               /*
+                * calling name ends in null (byte 16) from old smb
+                * convention.
+                */
+               if (server->workstation_RFC1001_name &&
+                   server->workstation_RFC1001_name[0] != 0)
+                       rfc1002mangle(ses_init_buf->trailer.
+                                     session_req.calling_name,
+                                     server->workstation_RFC1001_name,
+                                     RFC1001_NAME_LEN_WITH_NULL);
+               else
+                       rfc1002mangle(ses_init_buf->trailer.
+                                     session_req.calling_name,
+                                     "LINUX_CIFS_CLNT",
+                                     RFC1001_NAME_LEN_WITH_NULL);
+
+               ses_init_buf->trailer.session_req.scope1 = 0;
+               ses_init_buf->trailer.session_req.scope2 = 0;
+               smb_buf = (struct smb_hdr *)ses_init_buf;
+
+               /* sizeof RFC1002_SESSION_REQUEST with no scope */
+               smb_buf->smb_buf_length = 0x81000044;
+               rc = smb_send(server, smb_buf, 0x44);
+               kfree(ses_init_buf);
+               /*
+                * RFC1001 layer in at least one server
+                * requires very short break before negprot
+                * presumably because not expecting negprot
+                * to follow so fast.  This is a simple
+                * solution that works without
+                * complicating the code and causes no
+                * significant slowing down on mount
+                * for everyone else
+                */
+               usleep_range(1000, 2000);
+       }
+       /*
+        * else the negprot may still work without this
+        * even though malloc failed
+        */
+
+       return rc;
+}
+
+static int
+generic_ip_connect(struct TCP_Server_Info *server)
+{
+       int rc = 0;
+       unsigned short int sport;
+       int slen, sfamily;
        struct socket *socket = server->ssocket;
+       struct sockaddr *saddr;
+
+       saddr = (struct sockaddr *) &server->dstaddr;
+
+       if (server->dstaddr.ss_family == AF_INET6) {
+               sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
+               slen = sizeof(struct sockaddr_in6);
+               sfamily = AF_INET6;
+       } else {
+               sport = ((struct sockaddr_in *) saddr)->sin_port;
+               slen = sizeof(struct sockaddr_in);
+               sfamily = AF_INET;
+       }
 
        if (socket == NULL) {
-               rc = sock_create_kern(PF_INET, SOCK_STREAM,
+               rc = sock_create_kern(sfamily, SOCK_STREAM,
                                      IPPROTO_TCP, &socket);
                if (rc < 0) {
                        cERROR(1, "Error %d creating socket", rc);
+                       server->ssocket = NULL;
                        return rc;
                }
 
@@ -2156,63 +2267,28 @@ ipv4_connect(struct TCP_Server_Info *server)
                cFYI(1, "Socket created");
                server->ssocket = socket;
                socket->sk->sk_allocation = GFP_NOFS;
-               cifs_reclassify_socket4(socket);
+               if (sfamily == AF_INET6)
+                       cifs_reclassify_socket6(socket);
+               else
+                       cifs_reclassify_socket4(socket);
        }
 
        rc = bind_socket(server);
        if (rc < 0)
                return rc;
 
-       /* user overrode default port */
-       if (server->addr.sockAddr.sin_port) {
-               rc = socket->ops->connect(socket, (struct sockaddr *)
-                                         &server->addr.sockAddr,
-                                         sizeof(struct sockaddr_in), 0);
-               if (rc >= 0)
-                       connected = true;
-       }
-
-       if (!connected) {
-               /* save original port so we can retry user specified port
-                       later if fall back ports fail this time  */
-               orig_port = server->addr.sockAddr.sin_port;
-
-               /* do not retry on the same port we just failed on */
-               if (server->addr.sockAddr.sin_port != htons(CIFS_PORT)) {
-                       server->addr.sockAddr.sin_port = htons(CIFS_PORT);
-                       rc = socket->ops->connect(socket,
-                                               (struct sockaddr *)
-                                               &server->addr.sockAddr,
-                                               sizeof(struct sockaddr_in), 0);
-                       if (rc >= 0)
-                               connected = true;
-               }
-       }
-       if (!connected) {
-               server->addr.sockAddr.sin_port = htons(RFC1001_PORT);
-               rc = socket->ops->connect(socket, (struct sockaddr *)
-                                             &server->addr.sockAddr,
-                                             sizeof(struct sockaddr_in), 0);
-               if (rc >= 0)
-                       connected = true;
-       }
-
-       /* give up here - unless we want to retry on different
-               protocol families some day */
-       if (!connected) {
-               if (orig_port)
-                       server->addr.sockAddr.sin_port = orig_port;
-               cFYI(1, "Error %d connecting to server via ipv4", rc);
+       rc = socket->ops->connect(socket, saddr, slen, 0);
+       if (rc < 0) {
+               cFYI(1, "Error %d connecting to server", rc);
                sock_release(socket);
                server->ssocket = NULL;
                return rc;
        }
 
-
        /*
         * Eventually check for other socket options to change from
-        *  the default. sock_setsockopt not used because it expects
-        *  user space buffer
+        * the default. sock_setsockopt not used because it expects
+        * user space buffer
         */
        socket->sk->sk_rcvtimeo = 7 * HZ;
        socket->sk->sk_sndtimeo = 5 * HZ;
@@ -2226,7 +2302,7 @@ ipv4_connect(struct TCP_Server_Info *server)
        }
 
        if (server->tcp_nodelay) {
-               val = 1;
+               int val = 1;
                rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
                                (char *)&val, sizeof(val));
                if (rc)
@@ -2237,161 +2313,39 @@ ipv4_connect(struct TCP_Server_Info *server)
                 socket->sk->sk_sndbuf,
                 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
 
-       /* send RFC1001 sessinit */
-       if (server->addr.sockAddr.sin_port == htons(RFC1001_PORT)) {
-               /* some servers require RFC1001 sessinit before sending
-               negprot - BB check reconnection in case where second
-               sessinit is sent but no second negprot */
-               struct rfc1002_session_packet *ses_init_buf;
-               struct smb_hdr *smb_buf;
-               ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
-                                      GFP_KERNEL);
-               if (ses_init_buf) {
-                       ses_init_buf->trailer.session_req.called_len = 32;
-                       if (server->server_RFC1001_name &&
-                           server->server_RFC1001_name[0] != 0)
-                               rfc1002mangle(ses_init_buf->trailer.
-                                               session_req.called_name,
-                                             server->server_RFC1001_name,
-                                             RFC1001_NAME_LEN_WITH_NULL);
-                       else
-                               rfc1002mangle(ses_init_buf->trailer.
-                                               session_req.called_name,
-                                             DEFAULT_CIFS_CALLED_NAME,
-                                             RFC1001_NAME_LEN_WITH_NULL);
-
-                       ses_init_buf->trailer.session_req.calling_len = 32;
-
-                       /* calling name ends in null (byte 16) from old smb
-                       convention. */
-                       if (server->workstation_RFC1001_name &&
-                           server->workstation_RFC1001_name[0] != 0)
-                               rfc1002mangle(ses_init_buf->trailer.
-                                               session_req.calling_name,
-                                             server->workstation_RFC1001_name,
-                                             RFC1001_NAME_LEN_WITH_NULL);
-                       else
-                               rfc1002mangle(ses_init_buf->trailer.
-                                               session_req.calling_name,
-                                             "LINUX_CIFS_CLNT",
-                                             RFC1001_NAME_LEN_WITH_NULL);
-
-                       ses_init_buf->trailer.session_req.scope1 = 0;
-                       ses_init_buf->trailer.session_req.scope2 = 0;
-                       smb_buf = (struct smb_hdr *)ses_init_buf;
-                       /* sizeof RFC1002_SESSION_REQUEST with no scope */
-                       smb_buf->smb_buf_length = 0x81000044;
-                       rc = smb_send(server, smb_buf, 0x44);
-                       kfree(ses_init_buf);
-                       msleep(1); /* RFC1001 layer in at least one server
-                                     requires very short break before negprot
-                                     presumably because not expecting negprot
-                                     to follow so fast.  This is a simple
-                                     solution that works without
-                                     complicating the code and causes no
-                                     significant slowing down on mount
-                                     for everyone else */
-               }
-               /* else the negprot may still work without this
-               even though malloc failed */
-
-       }
+       if (sport == htons(RFC1001_PORT))
+               rc = ip_rfc1001_connect(server);
 
        return rc;
 }
 
 static int
-ipv6_connect(struct TCP_Server_Info *server)
+ip_connect(struct TCP_Server_Info *server)
 {
-       int rc = 0;
-       int val;
-       bool connected = false;
-       __be16 orig_port = 0;
-       struct socket *socket = server->ssocket;
+       unsigned short int *sport;
+       struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
+       struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
 
-       if (socket == NULL) {
-               rc = sock_create_kern(PF_INET6, SOCK_STREAM,
-                                     IPPROTO_TCP, &socket);
-               if (rc < 0) {
-                       cERROR(1, "Error %d creating ipv6 socket", rc);
-                       socket = NULL;
-                       return rc;
-               }
+       if (server->dstaddr.ss_family == AF_INET6)
+               sport = &addr6->sin6_port;
+       else
+               sport = &addr->sin_port;
 
-               /* BB other socket options to set KEEPALIVE, NODELAY? */
-               cFYI(1, "ipv6 Socket created");
-               server->ssocket = socket;
-               socket->sk->sk_allocation = GFP_NOFS;
-               cifs_reclassify_socket6(socket);
-       }
+       if (*sport == 0) {
+               int rc;
 
-       rc = bind_socket(server);
-       if (rc < 0)
-               return rc;
+               /* try with 445 port at first */
+               *sport = htons(CIFS_PORT);
 
-       /* user overrode default port */
-       if (server->addr.sockAddr6.sin6_port) {
-               rc = socket->ops->connect(socket,
-                               (struct sockaddr *) &server->addr.sockAddr6,
-                               sizeof(struct sockaddr_in6), 0);
+               rc = generic_ip_connect(server);
                if (rc >= 0)
-                       connected = true;
-       }
-
-       if (!connected) {
-               /* save original port so we can retry user specified port
-                       later if fall back ports fail this time  */
-
-               orig_port = server->addr.sockAddr6.sin6_port;
-               /* do not retry on the same port we just failed on */
-               if (server->addr.sockAddr6.sin6_port != htons(CIFS_PORT)) {
-                       server->addr.sockAddr6.sin6_port = htons(CIFS_PORT);
-                       rc = socket->ops->connect(socket, (struct sockaddr *)
-                                       &server->addr.sockAddr6,
-                                       sizeof(struct sockaddr_in6), 0);
-                       if (rc >= 0)
-                               connected = true;
-               }
-       }
-       if (!connected) {
-               server->addr.sockAddr6.sin6_port = htons(RFC1001_PORT);
-               rc = socket->ops->connect(socket, (struct sockaddr *)
-                               &server->addr.sockAddr6,
-                               sizeof(struct sockaddr_in6), 0);
-               if (rc >= 0)
-                       connected = true;
-       }
-
-       /* give up here - unless we want to retry on different
-               protocol families some day */
-       if (!connected) {
-               if (orig_port)
-                       server->addr.sockAddr6.sin6_port = orig_port;
-               cFYI(1, "Error %d connecting to server via ipv6", rc);
-               sock_release(socket);
-               server->ssocket = NULL;
-               return rc;
-       }
-
-       /*
-        * Eventually check for other socket options to change from
-        * the default. sock_setsockopt not used because it expects
-        * user space buffer
-        */
-       socket->sk->sk_rcvtimeo = 7 * HZ;
-       socket->sk->sk_sndtimeo = 5 * HZ;
+                       return rc;
 
-       if (server->tcp_nodelay) {
-               val = 1;
-               rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
-                               (char *)&val, sizeof(val));
-               if (rc)
-                       cFYI(1, "set TCP_NODELAY socket option error %d", rc);
+               /* if it failed, try with 139 port */
+               *sport = htons(RFC1001_PORT);
        }
 
-       server->ssocket = socket;
-
-       return rc;
+       return generic_ip_connect(server);
 }
 
 void reset_cifs_unix_caps(int xid, struct cifsTconInfo *tcon,
@@ -2999,7 +2953,7 @@ CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
                bcc_ptr++;              /* skip password */
                /* already aligned so no need to do it below */
        } else {
-               pSMB->PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE);
+               pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
                /* BB FIXME add code to fail this if NTLMv2 or Kerberos
                   specified as required (when that support is added to
                   the vfs in the future) as only NTLM or the much
@@ -3017,7 +2971,7 @@ CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
 #endif /* CIFS_WEAK_PW_HASH */
                SMBNTencrypt(tcon->password, ses->server->cryptkey, bcc_ptr);
 
-               bcc_ptr += CIFS_SESS_KEY_SIZE;
+               bcc_ptr += CIFS_AUTH_RESP_SIZE;
                if (ses->capabilities & CAP_UNICODE) {
                        /* must align unicode strings */
                        *bcc_ptr = 0; /* null byte password */