Debian Codegraph @

fs/smb/server/connection.h

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
4 */
5
6 #ifndef __KSMBD_CONNECTION_H__
7 #define __KSMBD_CONNECTION_H__
8
9 #include <linux/list.h>
10 #include <linux/inet.h>
11 #include <linux/ip.h>
12 #include <net/sock.h>
13 #include <net/tcp.h>
14 #include <net/inet_connection_sock.h>
15 #include <net/request_sock.h>
16 #include <linux/kthread.h>
17 #include <linux/nls.h>
18 #include <linux/unicode.h>
19
20 #include "smb_common.h"
21 #include "ksmbd_work.h"
22
23 struct smbdirect_buffer_descriptor_v1;
24
25 #define KSMBD_SOCKET_BACKLOG 16
26
27 enum {
28 KSMBD_SESS_NEW = 0,
29 KSMBD_SESS_GOOD,
30 KSMBD_SESS_EXITING,
31 KSMBD_SESS_NEED_RECONNECT,
32 KSMBD_SESS_NEED_NEGOTIATE,
33 KSMBD_SESS_NEED_SETUP,
34 KSMBD_SESS_RELEASING
35 };
36
37 struct ksmbd_conn_stats {
38 atomic_t open_files_count;
39 atomic64_t request_served;
40 };
41
42 struct ksmbd_transport;
43
44 struct ksmbd_conn {
45 struct smb_version_values *vals;
46 struct smb_version_ops *ops;
47 struct smb_version_cmds *cmds;
48 unsigned int max_cmds;
49 struct mutex srv_mutex;
50 int status;
51 unsigned int cli_cap;
52 bool stop_called;
53 union {
54 __be32 inet_addr;
55 #if IS_ENABLED(CONFIG_IPV6)
56 u8 inet6_addr[16];
57 #endif
58 };
59 unsigned int inet_hash;
60 char *request_buf;
61 struct ksmbd_transport *transport;
62 struct nls_table *local_nls;
63 struct unicode_map *um;
64 struct hlist_node hlist;
65 struct rw_semaphore session_lock;
66 /* smb session 1 per user */
67 struct xarray sessions;
68 unsigned long last_active;
69 /* How many request are running currently */
70 atomic_t req_running;
71 /* References which are made for this Server object*/
72 atomic_t r_count;
73 unsigned int total_credits;
74 unsigned int outstanding_credits;
75 spinlock_t credits_lock;
76 wait_queue_head_t req_running_q;
77 wait_queue_head_t r_count_q;
78 /* Lock to protect requests list*/
79 spinlock_t request_lock;
80 struct list_head requests;
81 struct list_head async_requests;
82 int connection_type;
83 struct ksmbd_conn_stats stats;
84 char ClientGUID[SMB2_CLIENT_GUID_SIZE];
85 struct ntlmssp_auth ntlmssp;
86
87 spinlock_t llist_lock;
88 struct list_head lock_list;
89
90 struct preauth_integrity_info *preauth_info;
91
92 bool need_neg;
93 unsigned int auth_mechs;
94 unsigned int preferred_auth_mech;
95 bool sign;
96 bool use_spnego:1;
97 __u16 cli_sec_mode;
98 __u16 srv_sec_mode;
99 /* dialect index that server chose */
100 __u16 dialect;
101
102 char *mechToken;
103 unsigned int mechTokenLen;
104
105 struct ksmbd_conn_ops *conn_ops;
106
107 /* Preauth Session Table */
108 struct list_head preauth_sess_table;
109
110 struct sockaddr_storage peer_addr;
111
112 /* Identifier for async message */
113 struct ida async_ida;
114
115 __le16 cipher_type;
116 __le16 compress_algorithm;
117 bool posix_ext_supported;
118 bool signing_negotiated;
119 __le16 signing_algorithm;
120 bool binding;
121 atomic_t refcnt;
122 bool is_aapl;
123 };
124
125 struct ksmbd_conn_ops {
126 int (*process_fn)(struct ksmbd_conn *conn);
127 int (*terminate_fn)(struct ksmbd_conn *conn);
128 };
129
130 struct ksmbd_transport_ops {
131 int (*prepare)(struct ksmbd_transport *t);
132 void (*disconnect)(struct ksmbd_transport *t);
133 void (*shutdown)(struct ksmbd_transport *t);
134 int (*read)(struct ksmbd_transport *t, char *buf,
135 unsigned int size, int max_retries);
136 int (*writev)(struct ksmbd_transport *t, struct kvec *iovs, int niov,
137 int size, bool need_invalidate_rkey,
138 unsigned int remote_key);
139 int (*rdma_read)(struct ksmbd_transport *t,
140 void *buf, unsigned int len,
141 struct smbdirect_buffer_descriptor_v1 *desc,
142 unsigned int desc_len);
143 int (*rdma_write)(struct ksmbd_transport *t,
144 void *buf, unsigned int len,
145 struct smbdirect_buffer_descriptor_v1 *desc,
146 unsigned int desc_len);
147 void (*free_transport)(struct ksmbd_transport *kt);
148 };
149
150 struct ksmbd_transport {
151 struct ksmbd_conn *conn;
152 const struct ksmbd_transport_ops *ops;
153 };
154
155 #define KSMBD_TCP_RECV_TIMEOUT (7 * HZ)
156 #define KSMBD_TCP_SEND_TIMEOUT (5 * HZ)
157 #define KSMBD_TCP_PEER_SOCKADDR(c) ((struct sockaddr *)&((c)->peer_addr))
158
159 #define CONN_HASH_BITS 12
160 extern DECLARE_HASHTABLE(conn_list, CONN_HASH_BITS);
161 extern struct rw_semaphore conn_list_lock;
162
163 bool ksmbd_conn_alive(struct ksmbd_conn *conn);
164 void ksmbd_conn_wait_idle(struct ksmbd_conn *conn);
165 int ksmbd_conn_wait_idle_sess_id(struct ksmbd_conn *curr_conn, u64 sess_id);
166 struct ksmbd_conn *ksmbd_conn_alloc(void);
167 void ksmbd_conn_free(struct ksmbd_conn *conn);
168 bool ksmbd_conn_lookup_dialect(struct ksmbd_conn *c);
169 int ksmbd_conn_write(struct ksmbd_work *work);
170 int ksmbd_conn_rdma_read(struct ksmbd_conn *conn,
171 void *buf, unsigned int buflen,
172 struct smbdirect_buffer_descriptor_v1 *desc,
173 unsigned int desc_len);
174 int ksmbd_conn_rdma_write(struct ksmbd_conn *conn,
175 void *buf, unsigned int buflen,
176 struct smbdirect_buffer_descriptor_v1 *desc,
177 unsigned int desc_len);
178 void ksmbd_conn_enqueue_request(struct ksmbd_work *work);
179 void ksmbd_conn_try_dequeue_request(struct ksmbd_work *work);
180 void ksmbd_conn_init_server_callbacks(struct ksmbd_conn_ops *ops);
181 int ksmbd_conn_handler_loop(void *p);
182 int ksmbd_conn_transport_init(void);
183 void ksmbd_conn_transport_destroy(void);
184 void ksmbd_conn_lock(struct ksmbd_conn *conn);
185 void ksmbd_conn_unlock(struct ksmbd_conn *conn);
186 void ksmbd_conn_r_count_inc(struct ksmbd_conn *conn);
187 void ksmbd_conn_r_count_dec(struct ksmbd_conn *conn);
188
189 /*
190 * WARNING
191 *
192 * This is a hack. We will move status to a proper place once we land
193 * a multi-sessions support.
194 */
195 static inline bool ksmbd_conn_good(struct ksmbd_conn *conn)
196 {
197 return READ_ONCE(conn->status) == KSMBD_SESS_GOOD;
198 }
199
200 static inline bool ksmbd_conn_need_negotiate(struct ksmbd_conn *conn)
201 {
202 return READ_ONCE(conn->status) == KSMBD_SESS_NEED_NEGOTIATE;
203 }
204
205 static inline bool ksmbd_conn_need_setup(struct ksmbd_conn *conn)
206 {
207 return READ_ONCE(conn->status) == KSMBD_SESS_NEED_SETUP;
208 }
209
210 static inline bool ksmbd_conn_need_reconnect(struct ksmbd_conn *conn)
211 {
212 return READ_ONCE(conn->status) == KSMBD_SESS_NEED_RECONNECT;
213 }
214
215 static inline bool ksmbd_conn_exiting(struct ksmbd_conn *conn)
216 {
217 return READ_ONCE(conn->status) == KSMBD_SESS_EXITING;
218 }
219
220 static inline bool ksmbd_conn_releasing(struct ksmbd_conn *conn)
221 {
222 return READ_ONCE(conn->status) == KSMBD_SESS_RELEASING;
223 }
224
225 static inline void ksmbd_conn_set_new(struct ksmbd_conn *conn)
226 {
227 WRITE_ONCE(conn->status, KSMBD_SESS_NEW);
228 }
229
230 static inline void ksmbd_conn_set_good(struct ksmbd_conn *conn)
231 {
232 WRITE_ONCE(conn->status, KSMBD_SESS_GOOD);
233 }
234
235 static inline void ksmbd_conn_set_need_negotiate(struct ksmbd_conn *conn)
236 {
237 WRITE_ONCE(conn->status, KSMBD_SESS_NEED_NEGOTIATE);
238 }
239
240 static inline void ksmbd_conn_set_need_setup(struct ksmbd_conn *conn)
241 {
242 WRITE_ONCE(conn->status, KSMBD_SESS_NEED_SETUP);
243 }
244
245 static inline void ksmbd_conn_set_need_reconnect(struct ksmbd_conn *conn)
246 {
247 WRITE_ONCE(conn->status, KSMBD_SESS_NEED_RECONNECT);
248 }
249
250 static inline void ksmbd_conn_set_exiting(struct ksmbd_conn *conn)
251 {
252 WRITE_ONCE(conn->status, KSMBD_SESS_EXITING);
253 }
254
255 static inline void ksmbd_conn_set_releasing(struct ksmbd_conn *conn)
256 {
257 WRITE_ONCE(conn->status, KSMBD_SESS_RELEASING);
258 }
259
260 void ksmbd_all_conn_set_status(u64 sess_id, u32 status);
261 #endif /* __CONNECTION_H__ */