1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
6 #ifndef __KSMBD_CONNECTION_H__
7 #define __KSMBD_CONNECTION_H__
9 #include <linux/list.h>
13 #include <net/inet_connection_sock.h>
14 #include <net/request_sock.h>
15 #include <linux/kthread.h>
16 #include <linux/nls.h>
17 #include <linux/unicode.h>
19 #include "smb_common.h"
20 #include "ksmbd_work.h"
22 #define KSMBD_SOCKET_BACKLOG 16
28 KSMBD_SESS_NEED_RECONNECT,
29 KSMBD_SESS_NEED_NEGOTIATE,
34 atomic_t open_files_count;
35 atomic64_t request_served;
38 struct ksmbd_transport;
41 struct smb_version_values *vals;
42 struct smb_version_ops *ops;
43 struct smb_version_cmds *cmds;
44 unsigned int max_cmds;
45 struct mutex srv_mutex;
49 struct ksmbd_transport *transport;
50 struct nls_table *local_nls;
51 struct unicode_map *um;
52 struct list_head conns_list;
53 struct rw_semaphore session_lock;
54 /* smb session 1 per user */
55 struct xarray sessions;
56 unsigned long last_active;
57 /* How many request are running currently */
59 /* References which are made for this Server object*/
61 unsigned int total_credits;
62 unsigned int outstanding_credits;
63 spinlock_t credits_lock;
64 wait_queue_head_t req_running_q;
65 wait_queue_head_t r_count_q;
66 /* Lock to protect requests list*/
67 spinlock_t request_lock;
68 struct list_head requests;
69 struct list_head async_requests;
71 struct ksmbd_stats stats;
72 char ClientGUID[SMB2_CLIENT_GUID_SIZE];
73 struct ntlmssp_auth ntlmssp;
75 spinlock_t llist_lock;
76 struct list_head lock_list;
78 struct preauth_integrity_info *preauth_info;
81 unsigned int auth_mechs;
82 unsigned int preferred_auth_mech;
87 /* dialect index that server chose */
91 unsigned int mechTokenLen;
93 struct ksmbd_conn_ops *conn_ops;
95 /* Preauth Session Table */
96 struct list_head preauth_sess_table;
98 struct sockaddr_storage peer_addr;
100 /* Identifier for async message */
101 struct ida async_ida;
104 __le16 compress_algorithm;
105 bool posix_ext_supported;
106 bool signing_negotiated;
107 __le16 signing_algorithm;
111 struct ksmbd_conn_ops {
112 int (*process_fn)(struct ksmbd_conn *conn);
113 int (*terminate_fn)(struct ksmbd_conn *conn);
116 struct ksmbd_transport_ops {
117 int (*prepare)(struct ksmbd_transport *t);
118 void (*disconnect)(struct ksmbd_transport *t);
119 void (*shutdown)(struct ksmbd_transport *t);
120 int (*read)(struct ksmbd_transport *t, char *buf,
121 unsigned int size, int max_retries);
122 int (*writev)(struct ksmbd_transport *t, struct kvec *iovs, int niov,
123 int size, bool need_invalidate_rkey,
124 unsigned int remote_key);
125 int (*rdma_read)(struct ksmbd_transport *t,
126 void *buf, unsigned int len,
127 struct smb2_buffer_desc_v1 *desc,
128 unsigned int desc_len);
129 int (*rdma_write)(struct ksmbd_transport *t,
130 void *buf, unsigned int len,
131 struct smb2_buffer_desc_v1 *desc,
132 unsigned int desc_len);
135 struct ksmbd_transport {
136 struct ksmbd_conn *conn;
137 struct ksmbd_transport_ops *ops;
140 #define KSMBD_TCP_RECV_TIMEOUT (7 * HZ)
141 #define KSMBD_TCP_SEND_TIMEOUT (5 * HZ)
142 #define KSMBD_TCP_PEER_SOCKADDR(c) ((struct sockaddr *)&((c)->peer_addr))
144 extern struct list_head conn_list;
145 extern struct rw_semaphore conn_list_lock;
147 bool ksmbd_conn_alive(struct ksmbd_conn *conn);
148 void ksmbd_conn_wait_idle(struct ksmbd_conn *conn, u64 sess_id);
149 struct ksmbd_conn *ksmbd_conn_alloc(void);
150 void ksmbd_conn_free(struct ksmbd_conn *conn);
151 bool ksmbd_conn_lookup_dialect(struct ksmbd_conn *c);
152 int ksmbd_conn_write(struct ksmbd_work *work);
153 int ksmbd_conn_rdma_read(struct ksmbd_conn *conn,
154 void *buf, unsigned int buflen,
155 struct smb2_buffer_desc_v1 *desc,
156 unsigned int desc_len);
157 int ksmbd_conn_rdma_write(struct ksmbd_conn *conn,
158 void *buf, unsigned int buflen,
159 struct smb2_buffer_desc_v1 *desc,
160 unsigned int desc_len);
161 void ksmbd_conn_enqueue_request(struct ksmbd_work *work);
162 void ksmbd_conn_try_dequeue_request(struct ksmbd_work *work);
163 void ksmbd_conn_init_server_callbacks(struct ksmbd_conn_ops *ops);
164 int ksmbd_conn_handler_loop(void *p);
165 int ksmbd_conn_transport_init(void);
166 void ksmbd_conn_transport_destroy(void);
167 void ksmbd_conn_lock(struct ksmbd_conn *conn);
168 void ksmbd_conn_unlock(struct ksmbd_conn *conn);
173 * This is a hack. We will move status to a proper place once we land
174 * a multi-sessions support.
176 static inline bool ksmbd_conn_good(struct ksmbd_conn *conn)
178 return READ_ONCE(conn->status) == KSMBD_SESS_GOOD;
181 static inline bool ksmbd_conn_need_negotiate(struct ksmbd_conn *conn)
183 return READ_ONCE(conn->status) == KSMBD_SESS_NEED_NEGOTIATE;
186 static inline bool ksmbd_conn_need_reconnect(struct ksmbd_conn *conn)
188 return READ_ONCE(conn->status) == KSMBD_SESS_NEED_RECONNECT;
191 static inline bool ksmbd_conn_exiting(struct ksmbd_conn *conn)
193 return READ_ONCE(conn->status) == KSMBD_SESS_EXITING;
196 static inline bool ksmbd_conn_releasing(struct ksmbd_conn *conn)
198 return READ_ONCE(conn->status) == KSMBD_SESS_RELEASING;
201 static inline void ksmbd_conn_set_new(struct ksmbd_conn *conn)
203 WRITE_ONCE(conn->status, KSMBD_SESS_NEW);
206 static inline void ksmbd_conn_set_good(struct ksmbd_conn *conn)
208 WRITE_ONCE(conn->status, KSMBD_SESS_GOOD);
211 static inline void ksmbd_conn_set_need_negotiate(struct ksmbd_conn *conn)
213 WRITE_ONCE(conn->status, KSMBD_SESS_NEED_NEGOTIATE);
216 static inline void ksmbd_conn_set_need_reconnect(struct ksmbd_conn *conn)
218 WRITE_ONCE(conn->status, KSMBD_SESS_NEED_RECONNECT);
221 static inline void ksmbd_conn_set_exiting(struct ksmbd_conn *conn)
223 WRITE_ONCE(conn->status, KSMBD_SESS_EXITING);
226 static inline void ksmbd_conn_set_releasing(struct ksmbd_conn *conn)
228 WRITE_ONCE(conn->status, KSMBD_SESS_RELEASING);
231 void ksmbd_all_conn_set_status(u64 sess_id, u32 status);
232 #endif /* __CONNECTION_H__ */