FILE
connection.c
File preview with syntax highlighting.
linux-6.19.14/fs/smb/server/connection.c
1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Copyright (C) 2016 Namjae Jeon <namjae.jeon@protocolfreedom.org> 4 * Copyright (C) 2018 Samsung Electronics Co., Ltd. 5 */ 6 7#include <linux/mutex.h> 8#include <linux/freezer.h> 9#include <linux/module.h> 10 11#include "server.h" 12#include "smb_common.h" 13#include "mgmt/ksmbd_ida.h" 14#include "connection.h" 15#include "transport_tcp.h" 16#include "transport_rdma.h" 17 18static DEFINE_MUTEX(init_lock); 19 20static struct ksmbd_conn_ops default_conn_ops; 21 22DEFINE_HASHTABLE(conn_list, CONN_HASH_BITS); 23DECLARE_RWSEM(conn_list_lock); 24 25/** 26 * ksmbd_conn_free() - free resources of the connection instance 27 * 28 * @conn: connection instance to be cleaned up 29 * 30 * During the thread termination, the corresponding conn instance 31 * resources(sock/memory) are released and finally the conn object is freed. 32 */ 33void ksmbd_conn_free(struct ksmbd_conn *conn) 34{ 35 down_write(&conn_list_lock); 36 hash_del(&conn->hlist); 37 up_write(&conn_list_lock); 38 39 xa_destroy(&conn->sessions); 40 kvfree(conn->request_buf); 41 kfree(conn->preauth_info); 42 kfree(conn->mechToken); 43 if (atomic_dec_and_test(&conn->refcnt)) { 44 conn->transport->ops->free_transport(conn->transport); 45 kfree(conn); 46 } 47} 48 49/** 50 * ksmbd_conn_alloc() - initialize a new connection instance 51 * 52 * Return: ksmbd_conn struct on success, otherwise NULL 53 */ 54struct ksmbd_conn *ksmbd_conn_alloc(void) 55{ 56 struct ksmbd_conn *conn; 57 58 conn = kzalloc(sizeof(struct ksmbd_conn), KSMBD_DEFAULT_GFP); 59 if (!conn) 60 return NULL; 61 62 conn->need_neg = true; 63 ksmbd_conn_set_new(conn); 64 conn->local_nls = load_nls("utf8"); 65 if (!conn->local_nls) 66 conn->local_nls = load_nls_default(); 67 if (IS_ENABLED(CONFIG_UNICODE)) 68 conn->um = utf8_load(UNICODE_AGE(12, 1, 0)); 69 else 70 conn->um = ERR_PTR(-EOPNOTSUPP); 71 if (IS_ERR(conn->um)) 72 conn->um = NULL; 73 atomic_set(&conn->req_running, 0); 74 atomic_set(&conn->r_count, 0); 75 atomic_set(&conn->refcnt, 1); 76 conn->total_credits = 1; 77 conn->outstanding_credits = 0; 78 79 init_waitqueue_head(&conn->req_running_q); 80 init_waitqueue_head(&conn->r_count_q); 81 INIT_LIST_HEAD(&conn->requests); 82 INIT_LIST_HEAD(&conn->async_requests); 83 spin_lock_init(&conn->request_lock); 84 spin_lock_init(&conn->credits_lock); 85 ida_init(&conn->async_ida); 86 xa_init(&conn->sessions); 87 88 spin_lock_init(&conn->llist_lock); 89 INIT_LIST_HEAD(&conn->lock_list); 90 91 init_rwsem(&conn->session_lock); 92 93 return conn; 94} 95 96bool ksmbd_conn_lookup_dialect(struct ksmbd_conn *c) 97{ 98 struct ksmbd_conn *t; 99 int bkt; 100 bool ret = false; 101 102 down_read(&conn_list_lock); 103 hash_for_each(conn_list, bkt, t, hlist) { 104 if (memcmp(t->ClientGUID, c->ClientGUID, SMB2_CLIENT_GUID_SIZE)) 105 continue; 106 107 ret = true; 108 break; 109 } 110 up_read(&conn_list_lock); 111 return ret; 112} 113 114void ksmbd_conn_enqueue_request(struct ksmbd_work *work) 115{ 116 struct ksmbd_conn *conn = work->conn; 117 struct list_head *requests_queue = NULL; 118 119 if (conn->ops->get_cmd_val(work) != SMB2_CANCEL_HE) 120 requests_queue = &conn->requests; 121 122 atomic_inc(&conn->req_running); 123 if (requests_queue) { 124 spin_lock(&conn->request_lock); 125 list_add_tail(&work->request_entry, requests_queue); 126 spin_unlock(&conn->request_lock); 127 } 128} 129 130void ksmbd_conn_try_dequeue_request(struct ksmbd_work *work) 131{ 132 struct ksmbd_conn *conn = work->conn; 133 134 atomic_dec(&conn->req_running); 135 if (waitqueue_active(&conn->req_running_q)) 136 wake_up(&conn->req_running_q); 137 138 if (list_empty(&work->request_entry) && 139 list_empty(&work->async_request_entry)) 140 return; 141 142 spin_lock(&conn->request_lock); 143 list_del_init(&work->request_entry); 144 spin_unlock(&conn->request_lock); 145 if (work->asynchronous) 146 release_async_work(work); 147 148 wake_up_all(&conn->req_running_q); 149} 150 151void ksmbd_conn_lock(struct ksmbd_conn *conn) 152{ 153 mutex_lock(&conn->srv_mutex); 154} 155 156void ksmbd_conn_unlock(struct ksmbd_conn *conn) 157{ 158 mutex_unlock(&conn->srv_mutex); 159} 160 161void ksmbd_all_conn_set_status(u64 sess_id, u32 status) 162{ 163 struct ksmbd_conn *conn; 164 int bkt; 165 166 down_read(&conn_list_lock); 167 hash_for_each(conn_list, bkt, conn, hlist) { 168 if (conn->binding || xa_load(&conn->sessions, sess_id)) 169 WRITE_ONCE(conn->status, status); 170 } 171 up_read(&conn_list_lock); 172} 173 174void ksmbd_conn_wait_idle(struct ksmbd_conn *conn) 175{ 176 wait_event(conn->req_running_q, atomic_read(&conn->req_running) < 2); 177} 178 179int ksmbd_conn_wait_idle_sess_id(struct ksmbd_conn *curr_conn, u64 sess_id) 180{ 181 struct ksmbd_conn *conn; 182 int rc, retry_count = 0, max_timeout = 120; 183 int rcount = 1, bkt; 184 185retry_idle: 186 if (retry_count >= max_timeout) 187 return -EIO; 188 189 down_read(&conn_list_lock); 190 hash_for_each(conn_list, bkt, conn, hlist) { 191 if (conn->binding || xa_load(&conn->sessions, sess_id)) { 192 if (conn == curr_conn) 193 rcount = 2; 194 if (atomic_read(&conn->req_running) >= rcount) { 195 rc = wait_event_timeout(conn->req_running_q, 196 atomic_read(&conn->req_running) < rcount, 197 HZ); 198 if (!rc) { 199 up_read(&conn_list_lock); 200 retry_count++; 201 goto retry_idle; 202 } 203 } 204 } 205 } 206 up_read(&conn_list_lock); 207 208 return 0; 209} 210 211int ksmbd_conn_write(struct ksmbd_work *work) 212{ 213 struct ksmbd_conn *conn = work->conn; 214 int sent; 215 216 if (!work->response_buf) { 217 pr_err("NULL response header\n"); 218 return -EINVAL; 219 } 220 221 if (work->send_no_response) 222 return 0; 223 224 if (!work->iov_idx) 225 return -EINVAL; 226 227 ksmbd_conn_lock(conn); 228 sent = conn->transport->ops->writev(conn->transport, work->iov, 229 work->iov_cnt, 230 get_rfc1002_len(work->iov[0].iov_base) + 4, 231 work->need_invalidate_rkey, 232 work->remote_key); 233 ksmbd_conn_unlock(conn); 234 235 if (sent < 0) { 236 pr_err("Failed to send message: %d\n", sent); 237 return sent; 238 } 239 240 return 0; 241} 242 243int ksmbd_conn_rdma_read(struct ksmbd_conn *conn, 244 void *buf, unsigned int buflen, 245 struct smbdirect_buffer_descriptor_v1 *desc, 246 unsigned int desc_len) 247{ 248 int ret = -EINVAL; 249 250 if (conn->transport->ops->rdma_read) 251 ret = conn->transport->ops->rdma_read(conn->transport, 252 buf, buflen, 253 desc, desc_len); 254 return ret; 255} 256 257int ksmbd_conn_rdma_write(struct ksmbd_conn *conn, 258 void *buf, unsigned int buflen, 259 struct smbdirect_buffer_descriptor_v1 *desc, 260 unsigned int desc_len) 261{ 262 int ret = -EINVAL; 263 264 if (conn->transport->ops->rdma_write) 265 ret = conn->transport->ops->rdma_write(conn->transport, 266 buf, buflen, 267 desc, desc_len); 268 return ret; 269} 270 271bool ksmbd_conn_alive(struct ksmbd_conn *conn) 272{ 273 if (!ksmbd_server_running()) 274 return false; 275 276 if (ksmbd_conn_exiting(conn)) 277 return false; 278 279 if (kthread_should_stop()) 280 return false; 281 282 if (atomic_read(&conn->stats.open_files_count) > 0) 283 return true; 284 285 /* 286 * Stop current session if the time that get last request from client 287 * is bigger than deadtime user configured and opening file count is 288 * zero. 289 */ 290 if (server_conf.deadtime > 0 && 291 time_after(jiffies, conn->last_active + server_conf.deadtime)) { 292 ksmbd_debug(CONN, "No response from client in %lu minutes\n", 293 server_conf.deadtime / SMB_ECHO_INTERVAL); 294 return false; 295 } 296 return true; 297} 298 299/* "+2" for BCC field (ByteCount, 2 bytes) */ 300#define SMB1_MIN_SUPPORTED_PDU_SIZE (sizeof(struct smb_hdr) + 2) 301#define SMB2_MIN_SUPPORTED_PDU_SIZE (sizeof(struct smb2_pdu)) 302 303/** 304 * ksmbd_conn_handler_loop() - session thread to listen on new smb requests 305 * @p: connection instance 306 * 307 * One thread each per connection 308 * 309 * Return: 0 on success 310 */ 311int ksmbd_conn_handler_loop(void *p) 312{ 313 struct ksmbd_conn *conn = (struct ksmbd_conn *)p; 314 struct ksmbd_transport *t = conn->transport; 315 unsigned int pdu_size, max_allowed_pdu_size, max_req; 316 char hdr_buf[4] = {0,}; 317 int size; 318 319 mutex_init(&conn->srv_mutex); 320 __module_get(THIS_MODULE); 321 322 if (t->ops->prepare && t->ops->prepare(t)) 323 goto out; 324 325 max_req = server_conf.max_inflight_req; 326 conn->last_active = jiffies; 327 set_freezable(); 328 while (ksmbd_conn_alive(conn)) { 329 if (try_to_freeze()) 330 continue; 331 332 kvfree(conn->request_buf); 333 conn->request_buf = NULL; 334 335recheck: 336 if (atomic_read(&conn->req_running) + 1 > max_req) { 337 wait_event_interruptible(conn->req_running_q, 338 atomic_read(&conn->req_running) < max_req); 339 goto recheck; 340 } 341 342 size = t->ops->read(t, hdr_buf, sizeof(hdr_buf), -1); 343 if (size != sizeof(hdr_buf)) 344 break; 345 346 pdu_size = get_rfc1002_len(hdr_buf); 347 ksmbd_debug(CONN, "RFC1002 header %u bytes\n", pdu_size); 348 349 if (ksmbd_conn_good(conn)) 350 max_allowed_pdu_size = 351 SMB3_MAX_MSGSIZE + conn->vals->max_write_size; 352 else 353 max_allowed_pdu_size = SMB3_MAX_MSGSIZE; 354 355 if (pdu_size > max_allowed_pdu_size) { 356 pr_err_ratelimited("PDU length(%u) exceeded maximum allowed pdu size(%u) on connection(%d)\n", 357 pdu_size, max_allowed_pdu_size, 358 READ_ONCE(conn->status)); 359 break; 360 } 361 362 /* 363 * Check maximum pdu size(0x00FFFFFF). 364 */ 365 if (pdu_size > MAX_STREAM_PROT_LEN) 366 break; 367 368 if (pdu_size < SMB1_MIN_SUPPORTED_PDU_SIZE) 369 break; 370 371 /* 4 for rfc1002 length field */ 372 /* 1 for implied bcc[0] */ 373 size = pdu_size + 4 + 1; 374 conn->request_buf = kvmalloc(size, KSMBD_DEFAULT_GFP); 375 if (!conn->request_buf) 376 break; 377 378 memcpy(conn->request_buf, hdr_buf, sizeof(hdr_buf)); 379 380 /* 381 * We already read 4 bytes to find out PDU size, now 382 * read in PDU 383 */ 384 size = t->ops->read(t, conn->request_buf + 4, pdu_size, 2); 385 if (size < 0) { 386 pr_err("sock_read failed: %d\n", size); 387 break; 388 } 389 390 if (size != pdu_size) { 391 pr_err("PDU error. Read: %d, Expected: %d\n", 392 size, pdu_size); 393 continue; 394 } 395 396 if (!ksmbd_smb_request(conn)) 397 break; 398 399 if (((struct smb2_hdr *)smb_get_msg(conn->request_buf))->ProtocolId == 400 SMB2_PROTO_NUMBER) { 401 if (pdu_size < SMB2_MIN_SUPPORTED_PDU_SIZE) 402 break; 403 } 404 405 if (!default_conn_ops.process_fn) { 406 pr_err("No connection request callback\n"); 407 break; 408 } 409 410 if (default_conn_ops.process_fn(conn)) { 411 pr_err("Cannot handle request\n"); 412 break; 413 } 414 } 415 416out: 417 ksmbd_conn_set_releasing(conn); 418 /* Wait till all reference dropped to the Server object*/ 419 ksmbd_debug(CONN, "Wait for all pending requests(%d)\n", atomic_read(&conn->r_count)); 420 wait_event(conn->r_count_q, atomic_read(&conn->r_count) == 0); 421 422 if (IS_ENABLED(CONFIG_UNICODE)) 423 utf8_unload(conn->um); 424 unload_nls(conn->local_nls); 425 if (default_conn_ops.terminate_fn) 426 default_conn_ops.terminate_fn(conn); 427 t->ops->disconnect(t); 428 module_put(THIS_MODULE); 429 return 0; 430} 431 432void ksmbd_conn_init_server_callbacks(struct ksmbd_conn_ops *ops) 433{ 434 default_conn_ops.process_fn = ops->process_fn; 435 default_conn_ops.terminate_fn = ops->terminate_fn; 436} 437 438void ksmbd_conn_r_count_inc(struct ksmbd_conn *conn) 439{ 440 atomic_inc(&conn->r_count); 441} 442 443void ksmbd_conn_r_count_dec(struct ksmbd_conn *conn) 444{ 445 /* 446 * Checking waitqueue to dropping pending requests on 447 * disconnection. waitqueue_active is safe because it 448 * uses atomic operation for condition. 449 */ 450 atomic_inc(&conn->refcnt); 451 if (!atomic_dec_return(&conn->r_count) && waitqueue_active(&conn->r_count_q)) 452 wake_up(&conn->r_count_q); 453 454 if (atomic_dec_and_test(&conn->refcnt)) 455 kfree(conn); 456} 457 458int ksmbd_conn_transport_init(void) 459{ 460 int ret; 461 462 mutex_lock(&init_lock); 463 ret = ksmbd_tcp_init(); 464 if (ret) { 465 pr_err("Failed to init TCP subsystem: %d\n", ret); 466 goto out; 467 } 468 469 ret = ksmbd_rdma_init(); 470 if (ret) { 471 pr_err("Failed to init RDMA subsystem: %d\n", ret); 472 goto out; 473 } 474out: 475 mutex_unlock(&init_lock); 476 return ret; 477} 478 479static void stop_sessions(void) 480{ 481 struct ksmbd_conn *conn; 482 struct ksmbd_transport *t; 483 int bkt; 484 485again: 486 down_read(&conn_list_lock); 487 hash_for_each(conn_list, bkt, conn, hlist) { 488 t = conn->transport; 489 ksmbd_conn_set_exiting(conn); 490 if (t->ops->shutdown) { 491 up_read(&conn_list_lock); 492 t->ops->shutdown(t); 493 down_read(&conn_list_lock); 494 } 495 } 496 up_read(&conn_list_lock); 497 498 if (!hash_empty(conn_list)) { 499 msleep(100); 500 goto again; 501 } 502} 503 504void ksmbd_conn_transport_destroy(void) 505{ 506 mutex_lock(&init_lock); 507 ksmbd_tcp_destroy(); 508 ksmbd_rdma_stop_listening(); 509 stop_sessions(); 510 ksmbd_rdma_destroy(); 511 mutex_unlock(&init_lock); 512}
~/
Read-only file preview.