root/fs/smb/client/smb1misc.c
// SPDX-License-Identifier: LGPL-2.1
/*
 *
 *   Copyright (C) International Business Machines  Corp., 2002,2008
 *   Author(s): Steve French (sfrench@us.ibm.com)
 *
 */

#include "smb1proto.h"
#include "smberr.h"
#include "nterr.h"
#include "cifs_debug.h"

/* NB: MID can not be set if treeCon not passed in, in that
   case it is responsibility of caller to set the mid */
unsigned int
header_assemble(struct smb_hdr *buffer, char smb_command,
                const struct cifs_tcon *treeCon, int word_count
                /* length of fixed section (word count) in two byte units  */)
{
        unsigned int in_len;
        char *temp = (char *) buffer;

        memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */

        in_len = (2 * word_count) + sizeof(struct smb_hdr) +
                2 /* for bcc field itself */;

        buffer->Protocol[0] = 0xFF;
        buffer->Protocol[1] = 'S';
        buffer->Protocol[2] = 'M';
        buffer->Protocol[3] = 'B';
        buffer->Command = smb_command;
        buffer->Flags = 0x00;   /* case sensitive */
        buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
        buffer->Pid = cpu_to_le16((__u16)current->tgid);
        buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
        if (treeCon) {
                buffer->Tid = treeCon->tid;
                if (treeCon->ses) {
                        if (treeCon->ses->capabilities & CAP_UNICODE)
                                buffer->Flags2 |= SMBFLG2_UNICODE;
                        if (treeCon->ses->capabilities & CAP_STATUS32)
                                buffer->Flags2 |= SMBFLG2_ERR_STATUS;

                        /* Uid is not converted */
                        buffer->Uid = treeCon->ses->Suid;
                        if (treeCon->ses->server)
                                buffer->Mid = get_next_mid(treeCon->ses->server);
                }
                if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
                        buffer->Flags2 |= SMBFLG2_DFS;
                if (treeCon->nocase)
                        buffer->Flags  |= SMBFLG_CASELESS;
                if ((treeCon->ses) && (treeCon->ses->server))
                        if (treeCon->ses->server->sign)
                                buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
        }

/*  endian conversion of flags is now done just before sending */
        buffer->WordCount = (char) word_count;
        return in_len;
}

bool
is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
{
        struct smb_hdr *buf = (struct smb_hdr *)buffer;
        struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
        struct TCP_Server_Info *pserver;
        struct cifs_ses *ses;
        struct cifs_tcon *tcon;
        struct cifsInodeInfo *pCifsInode;
        struct cifsFileInfo *netfile;

        cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
        if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
           (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
                struct smb_com_transaction_change_notify_rsp *pSMBr =
                        (struct smb_com_transaction_change_notify_rsp *)buf;
                struct file_notify_information *pnotify;
                __u32 data_offset = 0;
                size_t len = srv->total_read - srv->pdu_size;

                if (get_bcc(buf) > sizeof(struct file_notify_information)) {
                        data_offset = le32_to_cpu(pSMBr->DataOffset);

                        if (data_offset >
                            len - sizeof(struct file_notify_information)) {
                                cifs_dbg(FYI, "Invalid data_offset %u\n",
                                         data_offset);
                                return true;
                        }
                        pnotify = (struct file_notify_information *)
                                ((char *)&pSMBr->hdr.Protocol + data_offset);
                        cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
                                 pnotify->FileName, pnotify->Action);
                        /*   cifs_dump_mem("Rcvd notify Data: ",buf,
                                sizeof(struct smb_hdr)+60); */
                        return true;
                }
                if (pSMBr->hdr.Status.CifsError) {
                        cifs_dbg(FYI, "notify err 0x%x\n",
                                 pSMBr->hdr.Status.CifsError);
                        return true;
                }
                return false;
        }
        if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
                return false;
        if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
                /* no sense logging error on invalid handle on oplock
                   break - harmless race between close request and oplock
                   break response is expected from time to time writing out
                   large dirty files cached on the client */
                if ((NT_STATUS_INVALID_HANDLE) ==
                   le32_to_cpu(pSMB->hdr.Status.CifsError)) {
                        cifs_dbg(FYI, "Invalid handle on oplock break\n");
                        return true;
                } else if (ERRbadfid ==
                   le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
                        return true;
                } else {
                        return false; /* on valid oplock brk we get "request" */
                }
        }
        if (pSMB->hdr.WordCount != 8)
                return false;

        cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
                 pSMB->LockType, pSMB->OplockLevel);
        if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
                return false;

        /* If server is a channel, select the primary channel */
        pserver = SERVER_IS_CHAN(srv) ? srv->primary_server : srv;

        /* look up tcon based on tid & uid */
        spin_lock(&cifs_tcp_ses_lock);
        list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
                if (cifs_ses_exiting(ses))
                        continue;
                list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
                        if (tcon->tid != buf->Tid)
                                continue;

                        cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
                        spin_lock(&tcon->open_file_lock);
                        list_for_each_entry(netfile, &tcon->openFileList, tlist) {
                                if (pSMB->Fid != netfile->fid.netfid)
                                        continue;

                                cifs_dbg(FYI, "file id match, oplock break\n");
                                pCifsInode = CIFS_I(d_inode(netfile->dentry));

                                set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
                                        &pCifsInode->flags);

                                netfile->oplock_epoch = 0;
                                netfile->oplock_level = pSMB->OplockLevel;
                                netfile->oplock_break_cancelled = false;
                                cifs_queue_oplock_break(netfile);

                                spin_unlock(&tcon->open_file_lock);
                                spin_unlock(&cifs_tcp_ses_lock);
                                return true;
                        }
                        spin_unlock(&tcon->open_file_lock);
                        spin_unlock(&cifs_tcp_ses_lock);
                        cifs_dbg(FYI, "No matching file for oplock break\n");
                        return true;
                }
        }
        spin_unlock(&cifs_tcp_ses_lock);
        cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
        return true;
}

/*
 * calculate the size of the SMB message based on the fixed header
 * portion, the number of word parameters and the data portion of the message
 */
unsigned int
smbCalcSize(void *buf)
{
        struct smb_hdr *ptr = buf;
        return (sizeof(struct smb_hdr) + (2 * ptr->WordCount) +
                2 /* size of the bcc field */ + get_bcc(ptr));
}