connection.c 12 KB

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