Skip to content

Commit 055a8fd

Browse files
committed
ksmbd: limit repeated connections from clients with the same IP
Repeated connections from clients with the same IP address may exhaust the max connections and prevent other normal client connections. This patch limit repeated connections from clients with the same IP. Reported-by: tianshuo han <[email protected]> Signed-off-by: Namjae Jeon <[email protected]>
1 parent 9ed254a commit 055a8fd

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

connection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct ksmbd_conn {
4646
struct mutex srv_mutex;
4747
int status;
4848
unsigned int cli_cap;
49+
__be32 inet_addr;
4950
char *request_buf;
5051
struct ksmbd_transport *transport;
5152
struct nls_table *local_nls;

transport_tcp.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ static struct tcp_transport *alloc_transport(struct socket *client_sk)
115115
return NULL;
116116
}
117117

118+
conn->inet_addr = inet_sk(client_sk->sk)->inet_daddr;
118119
conn->transport = KSMBD_TRANS(t);
119120
KSMBD_TRANS(t)->conn = conn;
120121
KSMBD_TRANS(t)->ops = &ksmbd_tcp_transport_ops;
@@ -258,6 +259,8 @@ static int ksmbd_kthread_fn(void *p)
258259
{
259260
struct socket *client_sk = NULL;
260261
struct interface *iface = (struct interface *)p;
262+
struct inet_sock *csk_inet;
263+
struct ksmbd_conn *conn;
261264
int ret;
262265

263266
while (!kthread_should_stop()) {
@@ -276,6 +279,20 @@ static int ksmbd_kthread_fn(void *p)
276279
continue;
277280
}
278281

282+
/*
283+
* Limits repeated connections from clients with the same IP.
284+
*/
285+
csk_inet = inet_sk(client_sk->sk);
286+
down_read(&conn_list_lock);
287+
list_for_each_entry(conn, &conn_list, conns_list)
288+
if (csk_inet->inet_daddr == conn->inet_addr) {
289+
ret = -EAGAIN;
290+
break;
291+
}
292+
up_read(&conn_list_lock);
293+
if (ret == -EAGAIN)
294+
continue;
295+
279296
if (server_conf.max_connections &&
280297
atomic_inc_return(&active_num_conn) >= server_conf.max_connections) {
281298
pr_info_ratelimited("Limit the maximum number of connections(%u)\n",

0 commit comments

Comments
 (0)