Merge tag 'trace-v5.15-3' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[platform/kernel/linux-starfive.git] / fs / ksmbd / smb2ops.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
5  */
6
7 #include <linux/slab.h>
8 #include "glob.h"
9 #include "smb2pdu.h"
10
11 #include "auth.h"
12 #include "connection.h"
13 #include "smb_common.h"
14 #include "server.h"
15
16 static struct smb_version_values smb21_server_values = {
17         .version_string = SMB21_VERSION_STRING,
18         .protocol_id = SMB21_PROT_ID,
19         .capabilities = SMB2_GLOBAL_CAP_LARGE_MTU,
20         .max_read_size = SMB21_DEFAULT_IOSIZE,
21         .max_write_size = SMB21_DEFAULT_IOSIZE,
22         .max_trans_size = SMB21_DEFAULT_IOSIZE,
23         .large_lock_type = 0,
24         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
25         .shared_lock_type = SMB2_LOCKFLAG_SHARED,
26         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
27         .header_size = sizeof(struct smb2_hdr),
28         .max_header_size = MAX_SMB2_HDR_SIZE,
29         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
30         .lock_cmd = SMB2_LOCK,
31         .cap_unix = 0,
32         .cap_nt_find = SMB2_NT_FIND,
33         .cap_large_files = SMB2_LARGE_FILES,
34         .create_lease_size = sizeof(struct create_lease),
35         .create_durable_size = sizeof(struct create_durable_rsp),
36         .create_mxac_size = sizeof(struct create_mxac_rsp),
37         .create_disk_id_size = sizeof(struct create_disk_id_rsp),
38         .create_posix_size = sizeof(struct create_posix_rsp),
39 };
40
41 static struct smb_version_values smb30_server_values = {
42         .version_string = SMB30_VERSION_STRING,
43         .protocol_id = SMB30_PROT_ID,
44         .capabilities = SMB2_GLOBAL_CAP_LARGE_MTU,
45         .max_read_size = SMB3_DEFAULT_IOSIZE,
46         .max_write_size = SMB3_DEFAULT_IOSIZE,
47         .max_trans_size = SMB3_DEFAULT_TRANS_SIZE,
48         .large_lock_type = 0,
49         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
50         .shared_lock_type = SMB2_LOCKFLAG_SHARED,
51         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
52         .header_size = sizeof(struct smb2_hdr),
53         .max_header_size = MAX_SMB2_HDR_SIZE,
54         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
55         .lock_cmd = SMB2_LOCK,
56         .cap_unix = 0,
57         .cap_nt_find = SMB2_NT_FIND,
58         .cap_large_files = SMB2_LARGE_FILES,
59         .create_lease_size = sizeof(struct create_lease_v2),
60         .create_durable_size = sizeof(struct create_durable_rsp),
61         .create_durable_v2_size = sizeof(struct create_durable_v2_rsp),
62         .create_mxac_size = sizeof(struct create_mxac_rsp),
63         .create_disk_id_size = sizeof(struct create_disk_id_rsp),
64         .create_posix_size = sizeof(struct create_posix_rsp),
65 };
66
67 static struct smb_version_values smb302_server_values = {
68         .version_string = SMB302_VERSION_STRING,
69         .protocol_id = SMB302_PROT_ID,
70         .capabilities = SMB2_GLOBAL_CAP_LARGE_MTU,
71         .max_read_size = SMB3_DEFAULT_IOSIZE,
72         .max_write_size = SMB3_DEFAULT_IOSIZE,
73         .max_trans_size = SMB3_DEFAULT_TRANS_SIZE,
74         .large_lock_type = 0,
75         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
76         .shared_lock_type = SMB2_LOCKFLAG_SHARED,
77         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
78         .header_size = sizeof(struct smb2_hdr),
79         .max_header_size = MAX_SMB2_HDR_SIZE,
80         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
81         .lock_cmd = SMB2_LOCK,
82         .cap_unix = 0,
83         .cap_nt_find = SMB2_NT_FIND,
84         .cap_large_files = SMB2_LARGE_FILES,
85         .create_lease_size = sizeof(struct create_lease_v2),
86         .create_durable_size = sizeof(struct create_durable_rsp),
87         .create_durable_v2_size = sizeof(struct create_durable_v2_rsp),
88         .create_mxac_size = sizeof(struct create_mxac_rsp),
89         .create_disk_id_size = sizeof(struct create_disk_id_rsp),
90         .create_posix_size = sizeof(struct create_posix_rsp),
91 };
92
93 static struct smb_version_values smb311_server_values = {
94         .version_string = SMB311_VERSION_STRING,
95         .protocol_id = SMB311_PROT_ID,
96         .capabilities = SMB2_GLOBAL_CAP_LARGE_MTU,
97         .max_read_size = SMB3_DEFAULT_IOSIZE,
98         .max_write_size = SMB3_DEFAULT_IOSIZE,
99         .max_trans_size = SMB3_DEFAULT_TRANS_SIZE,
100         .large_lock_type = 0,
101         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
102         .shared_lock_type = SMB2_LOCKFLAG_SHARED,
103         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
104         .header_size = sizeof(struct smb2_hdr),
105         .max_header_size = MAX_SMB2_HDR_SIZE,
106         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
107         .lock_cmd = SMB2_LOCK,
108         .cap_unix = 0,
109         .cap_nt_find = SMB2_NT_FIND,
110         .cap_large_files = SMB2_LARGE_FILES,
111         .create_lease_size = sizeof(struct create_lease_v2),
112         .create_durable_size = sizeof(struct create_durable_rsp),
113         .create_durable_v2_size = sizeof(struct create_durable_v2_rsp),
114         .create_mxac_size = sizeof(struct create_mxac_rsp),
115         .create_disk_id_size = sizeof(struct create_disk_id_rsp),
116         .create_posix_size = sizeof(struct create_posix_rsp),
117 };
118
119 static struct smb_version_ops smb2_0_server_ops = {
120         .get_cmd_val            =       get_smb2_cmd_val,
121         .init_rsp_hdr           =       init_smb2_rsp_hdr,
122         .set_rsp_status         =       set_smb2_rsp_status,
123         .allocate_rsp_buf       =       smb2_allocate_rsp_buf,
124         .set_rsp_credits        =       smb2_set_rsp_credits,
125         .check_user_session     =       smb2_check_user_session,
126         .get_ksmbd_tcon         =       smb2_get_ksmbd_tcon,
127         .is_sign_req            =       smb2_is_sign_req,
128         .check_sign_req         =       smb2_check_sign_req,
129         .set_sign_rsp           =       smb2_set_sign_rsp
130 };
131
132 static struct smb_version_ops smb3_0_server_ops = {
133         .get_cmd_val            =       get_smb2_cmd_val,
134         .init_rsp_hdr           =       init_smb2_rsp_hdr,
135         .set_rsp_status         =       set_smb2_rsp_status,
136         .allocate_rsp_buf       =       smb2_allocate_rsp_buf,
137         .set_rsp_credits        =       smb2_set_rsp_credits,
138         .check_user_session     =       smb2_check_user_session,
139         .get_ksmbd_tcon         =       smb2_get_ksmbd_tcon,
140         .is_sign_req            =       smb2_is_sign_req,
141         .check_sign_req         =       smb3_check_sign_req,
142         .set_sign_rsp           =       smb3_set_sign_rsp,
143         .generate_signingkey    =       ksmbd_gen_smb30_signingkey,
144         .generate_encryptionkey =       ksmbd_gen_smb30_encryptionkey,
145         .is_transform_hdr       =       smb3_is_transform_hdr,
146         .decrypt_req            =       smb3_decrypt_req,
147         .encrypt_resp           =       smb3_encrypt_resp
148 };
149
150 static struct smb_version_ops smb3_11_server_ops = {
151         .get_cmd_val            =       get_smb2_cmd_val,
152         .init_rsp_hdr           =       init_smb2_rsp_hdr,
153         .set_rsp_status         =       set_smb2_rsp_status,
154         .allocate_rsp_buf       =       smb2_allocate_rsp_buf,
155         .set_rsp_credits        =       smb2_set_rsp_credits,
156         .check_user_session     =       smb2_check_user_session,
157         .get_ksmbd_tcon         =       smb2_get_ksmbd_tcon,
158         .is_sign_req            =       smb2_is_sign_req,
159         .check_sign_req         =       smb3_check_sign_req,
160         .set_sign_rsp           =       smb3_set_sign_rsp,
161         .generate_signingkey    =       ksmbd_gen_smb311_signingkey,
162         .generate_encryptionkey =       ksmbd_gen_smb311_encryptionkey,
163         .is_transform_hdr       =       smb3_is_transform_hdr,
164         .decrypt_req            =       smb3_decrypt_req,
165         .encrypt_resp           =       smb3_encrypt_resp
166 };
167
168 static struct smb_version_cmds smb2_0_server_cmds[NUMBER_OF_SMB2_COMMANDS] = {
169         [SMB2_NEGOTIATE_HE]     =       { .proc = smb2_negotiate_request, },
170         [SMB2_SESSION_SETUP_HE] =       { .proc = smb2_sess_setup, },
171         [SMB2_TREE_CONNECT_HE]  =       { .proc = smb2_tree_connect,},
172         [SMB2_TREE_DISCONNECT_HE]  =    { .proc = smb2_tree_disconnect,},
173         [SMB2_LOGOFF_HE]        =       { .proc = smb2_session_logoff,},
174         [SMB2_CREATE_HE]        =       { .proc = smb2_open},
175         [SMB2_QUERY_INFO_HE]    =       { .proc = smb2_query_info},
176         [SMB2_QUERY_DIRECTORY_HE] =     { .proc = smb2_query_dir},
177         [SMB2_CLOSE_HE]         =       { .proc = smb2_close},
178         [SMB2_ECHO_HE]          =       { .proc = smb2_echo},
179         [SMB2_SET_INFO_HE]      =       { .proc = smb2_set_info},
180         [SMB2_READ_HE]          =       { .proc = smb2_read},
181         [SMB2_WRITE_HE]         =       { .proc = smb2_write},
182         [SMB2_FLUSH_HE]         =       { .proc = smb2_flush},
183         [SMB2_CANCEL_HE]        =       { .proc = smb2_cancel},
184         [SMB2_LOCK_HE]          =       { .proc = smb2_lock},
185         [SMB2_IOCTL_HE]         =       { .proc = smb2_ioctl},
186         [SMB2_OPLOCK_BREAK_HE]  =       { .proc = smb2_oplock_break},
187         [SMB2_CHANGE_NOTIFY_HE] =       { .proc = smb2_notify},
188 };
189
190 int init_smb2_0_server(struct ksmbd_conn *conn)
191 {
192         return -EOPNOTSUPP;
193 }
194
195 /**
196  * init_smb2_1_server() - initialize a smb server connection with smb2.1
197  *                      command dispatcher
198  * @conn:       connection instance
199  */
200 void init_smb2_1_server(struct ksmbd_conn *conn)
201 {
202         conn->vals = &smb21_server_values;
203         conn->ops = &smb2_0_server_ops;
204         conn->cmds = smb2_0_server_cmds;
205         conn->max_cmds = ARRAY_SIZE(smb2_0_server_cmds);
206         conn->max_credits = SMB2_MAX_CREDITS;
207         conn->signing_algorithm = SIGNING_ALG_HMAC_SHA256;
208
209         if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_LEASES)
210                 conn->vals->capabilities |= SMB2_GLOBAL_CAP_LEASING;
211 }
212
213 /**
214  * init_smb3_0_server() - initialize a smb server connection with smb3.0
215  *                      command dispatcher
216  * @conn:       connection instance
217  */
218 void init_smb3_0_server(struct ksmbd_conn *conn)
219 {
220         conn->vals = &smb30_server_values;
221         conn->ops = &smb3_0_server_ops;
222         conn->cmds = smb2_0_server_cmds;
223         conn->max_cmds = ARRAY_SIZE(smb2_0_server_cmds);
224         conn->max_credits = SMB2_MAX_CREDITS;
225         conn->signing_algorithm = SIGNING_ALG_AES_CMAC;
226
227         if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_LEASES)
228                 conn->vals->capabilities |= SMB2_GLOBAL_CAP_LEASING;
229
230         if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION &&
231             conn->cli_cap & SMB2_GLOBAL_CAP_ENCRYPTION)
232                 conn->vals->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
233
234         if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL)
235                 conn->vals->capabilities |= SMB2_GLOBAL_CAP_MULTI_CHANNEL;
236 }
237
238 /**
239  * init_smb3_02_server() - initialize a smb server connection with smb3.02
240  *                      command dispatcher
241  * @conn:       connection instance
242  */
243 void init_smb3_02_server(struct ksmbd_conn *conn)
244 {
245         conn->vals = &smb302_server_values;
246         conn->ops = &smb3_0_server_ops;
247         conn->cmds = smb2_0_server_cmds;
248         conn->max_cmds = ARRAY_SIZE(smb2_0_server_cmds);
249         conn->max_credits = SMB2_MAX_CREDITS;
250         conn->signing_algorithm = SIGNING_ALG_AES_CMAC;
251
252         if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_LEASES)
253                 conn->vals->capabilities |= SMB2_GLOBAL_CAP_LEASING;
254
255         if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION &&
256             conn->cli_cap & SMB2_GLOBAL_CAP_ENCRYPTION)
257                 conn->vals->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
258
259         if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL)
260                 conn->vals->capabilities |= SMB2_GLOBAL_CAP_MULTI_CHANNEL;
261 }
262
263 /**
264  * init_smb3_11_server() - initialize a smb server connection with smb3.11
265  *                      command dispatcher
266  * @conn:       connection instance
267  */
268 int init_smb3_11_server(struct ksmbd_conn *conn)
269 {
270         conn->vals = &smb311_server_values;
271         conn->ops = &smb3_11_server_ops;
272         conn->cmds = smb2_0_server_cmds;
273         conn->max_cmds = ARRAY_SIZE(smb2_0_server_cmds);
274         conn->max_credits = SMB2_MAX_CREDITS;
275         conn->signing_algorithm = SIGNING_ALG_AES_CMAC;
276
277         if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_LEASES)
278                 conn->vals->capabilities |= SMB2_GLOBAL_CAP_LEASING;
279
280         if (conn->cipher_type)
281                 conn->vals->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
282
283         if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL)
284                 conn->vals->capabilities |= SMB2_GLOBAL_CAP_MULTI_CHANNEL;
285
286         INIT_LIST_HEAD(&conn->preauth_sess_table);
287         return 0;
288 }
289
290 void init_smb2_max_read_size(unsigned int sz)
291 {
292         smb21_server_values.max_read_size = sz;
293         smb30_server_values.max_read_size = sz;
294         smb302_server_values.max_read_size = sz;
295         smb311_server_values.max_read_size = sz;
296 }
297
298 void init_smb2_max_write_size(unsigned int sz)
299 {
300         smb21_server_values.max_write_size = sz;
301         smb30_server_values.max_write_size = sz;
302         smb302_server_values.max_write_size = sz;
303         smb311_server_values.max_write_size = sz;
304 }
305
306 void init_smb2_max_trans_size(unsigned int sz)
307 {
308         smb21_server_values.max_trans_size = sz;
309         smb30_server_values.max_trans_size = sz;
310         smb302_server_values.max_trans_size = sz;
311         smb311_server_values.max_trans_size = sz;
312 }