connection.c 13 KB

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