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