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