Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: LGPL-2.1
2/*
3 *
4 * Copyright (C) International Business Machines Corp., 2002,2008
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 */
8
9#include "smb1proto.h"
10#include "smberr.h"
11#include "nterr.h"
12#include "cifs_debug.h"
13
14/* NB: MID can not be set if treeCon not passed in, in that
15 case it is responsibility of caller to set the mid */
16unsigned int
17header_assemble(struct smb_hdr *buffer, char smb_command,
18 const struct cifs_tcon *treeCon, int word_count
19 /* length of fixed section (word count) in two byte units */)
20{
21 unsigned int in_len;
22 char *temp = (char *) buffer;
23
24 memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
25
26 in_len = (2 * word_count) + sizeof(struct smb_hdr) +
27 2 /* for bcc field itself */;
28
29 buffer->Protocol[0] = 0xFF;
30 buffer->Protocol[1] = 'S';
31 buffer->Protocol[2] = 'M';
32 buffer->Protocol[3] = 'B';
33 buffer->Command = smb_command;
34 buffer->Flags = 0x00; /* case sensitive */
35 buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
36 buffer->Pid = cpu_to_le16((__u16)current->tgid);
37 buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
38 if (treeCon) {
39 buffer->Tid = treeCon->tid;
40 if (treeCon->ses) {
41 if (treeCon->ses->capabilities & CAP_UNICODE)
42 buffer->Flags2 |= SMBFLG2_UNICODE;
43 if (treeCon->ses->capabilities & CAP_STATUS32)
44 buffer->Flags2 |= SMBFLG2_ERR_STATUS;
45
46 /* Uid is not converted */
47 buffer->Uid = treeCon->ses->Suid;
48 if (treeCon->ses->server)
49 buffer->Mid = get_next_mid(treeCon->ses->server);
50 }
51 if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
52 buffer->Flags2 |= SMBFLG2_DFS;
53 if (treeCon->nocase)
54 buffer->Flags |= SMBFLG_CASELESS;
55 if ((treeCon->ses) && (treeCon->ses->server))
56 if (treeCon->ses->server->sign)
57 buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
58 }
59
60/* endian conversion of flags is now done just before sending */
61 buffer->WordCount = (char) word_count;
62 return in_len;
63}
64
65bool
66is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
67{
68 struct smb_hdr *buf = (struct smb_hdr *)buffer;
69 struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
70 struct TCP_Server_Info *pserver;
71 struct cifs_ses *ses;
72 struct cifs_tcon *tcon;
73 struct cifsInodeInfo *pCifsInode;
74 struct cifsFileInfo *netfile;
75
76 cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
77 if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
78 (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
79 struct smb_com_transaction_change_notify_rsp *pSMBr =
80 (struct smb_com_transaction_change_notify_rsp *)buf;
81 struct file_notify_information *pnotify;
82 __u32 data_offset = 0;
83 size_t len = srv->total_read - srv->pdu_size;
84
85 if (get_bcc(buf) > sizeof(struct file_notify_information)) {
86 data_offset = le32_to_cpu(pSMBr->DataOffset);
87
88 if (data_offset >
89 len - sizeof(struct file_notify_information)) {
90 cifs_dbg(FYI, "Invalid data_offset %u\n",
91 data_offset);
92 return true;
93 }
94 pnotify = (struct file_notify_information *)
95 ((char *)&pSMBr->hdr.Protocol + data_offset);
96 cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
97 pnotify->FileName, pnotify->Action);
98 /* cifs_dump_mem("Rcvd notify Data: ",buf,
99 sizeof(struct smb_hdr)+60); */
100 return true;
101 }
102 if (pSMBr->hdr.Status.CifsError) {
103 cifs_dbg(FYI, "notify err 0x%x\n",
104 pSMBr->hdr.Status.CifsError);
105 return true;
106 }
107 return false;
108 }
109 if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
110 return false;
111 if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
112 /* no sense logging error on invalid handle on oplock
113 break - harmless race between close request and oplock
114 break response is expected from time to time writing out
115 large dirty files cached on the client */
116 if ((NT_STATUS_INVALID_HANDLE) ==
117 le32_to_cpu(pSMB->hdr.Status.CifsError)) {
118 cifs_dbg(FYI, "Invalid handle on oplock break\n");
119 return true;
120 } else if (ERRbadfid ==
121 le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
122 return true;
123 } else {
124 return false; /* on valid oplock brk we get "request" */
125 }
126 }
127 if (pSMB->hdr.WordCount != 8)
128 return false;
129
130 cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
131 pSMB->LockType, pSMB->OplockLevel);
132 if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
133 return false;
134
135 /* If server is a channel, select the primary channel */
136 pserver = SERVER_IS_CHAN(srv) ? srv->primary_server : srv;
137
138 /* look up tcon based on tid & uid */
139 spin_lock(&cifs_tcp_ses_lock);
140 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
141 if (cifs_ses_exiting(ses))
142 continue;
143 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
144 if (tcon->tid != buf->Tid)
145 continue;
146
147 cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
148 spin_lock(&tcon->open_file_lock);
149 list_for_each_entry(netfile, &tcon->openFileList, tlist) {
150 if (pSMB->Fid != netfile->fid.netfid)
151 continue;
152
153 cifs_dbg(FYI, "file id match, oplock break\n");
154 pCifsInode = CIFS_I(d_inode(netfile->dentry));
155
156 set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
157 &pCifsInode->flags);
158
159 netfile->oplock_epoch = 0;
160 netfile->oplock_level = pSMB->OplockLevel;
161 netfile->oplock_break_cancelled = false;
162 cifs_queue_oplock_break(netfile);
163
164 spin_unlock(&tcon->open_file_lock);
165 spin_unlock(&cifs_tcp_ses_lock);
166 return true;
167 }
168 spin_unlock(&tcon->open_file_lock);
169 spin_unlock(&cifs_tcp_ses_lock);
170 cifs_dbg(FYI, "No matching file for oplock break\n");
171 return true;
172 }
173 }
174 spin_unlock(&cifs_tcp_ses_lock);
175 cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
176 return true;
177}
178
179/*
180 * calculate the size of the SMB message based on the fixed header
181 * portion, the number of word parameters and the data portion of the message
182 */
183unsigned int
184smbCalcSize(void *buf)
185{
186 struct smb_hdr *ptr = buf;
187 return (sizeof(struct smb_hdr) + (2 * ptr->WordCount) +
188 2 /* size of the bcc field */ + get_bcc(ptr));
189}