Skip to content

Commit d8b6dc9

Browse files
namjaejeonsmfrench
authored andcommitted
ksmbd: add max ip connections parameter
This parameter set the maximum number of connections per ip address. The default is 8. Cc: [email protected] Fixes: c0d4111 ("ksmbd: extend the connection limiting mechanism to support IPv6") Signed-off-by: Namjae Jeon <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent 88daf2f commit d8b6dc9

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

fs/smb/server/ksmbd_netlink.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ struct ksmbd_startup_request {
112112
__u32 smbd_max_io_size; /* smbd read write size */
113113
__u32 max_connections; /* Number of maximum simultaneous connections */
114114
__s8 bind_interfaces_only;
115-
__s8 reserved[503]; /* Reserved room */
115+
__u32 max_ip_connections; /* Number of maximum connection per ip address */
116+
__s8 reserved[499]; /* Reserved room */
116117
__u32 ifc_list_sz; /* interfaces list size */
117118
__s8 ____payload[];
118-
};
119+
} __packed;
119120

120121
#define KSMBD_STARTUP_CONFIG_INTERFACES(s) ((s)->____payload)
121122

fs/smb/server/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct ksmbd_server_config {
4343
unsigned int auth_mechs;
4444
unsigned int max_connections;
4545
unsigned int max_inflight_req;
46+
unsigned int max_ip_connections;
4647

4748
char *conf[SERVER_CONF_WORK_GROUP + 1];
4849
struct task_struct *dh_task;

fs/smb/server/transport_ipc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ static int ipc_server_config_on_startup(struct ksmbd_startup_request *req)
335335
if (req->max_connections)
336336
server_conf.max_connections = req->max_connections;
337337

338+
if (req->max_ip_connections)
339+
server_conf.max_ip_connections = req->max_ip_connections;
340+
338341
ret = ksmbd_set_netbios_name(req->netbios_name);
339342
ret |= ksmbd_set_server_string(req->server_string);
340343
ret |= ksmbd_set_work_group(req->work_group);

fs/smb/server/transport_tcp.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ static int ksmbd_kthread_fn(void *p)
225225
struct interface *iface = (struct interface *)p;
226226
struct ksmbd_conn *conn;
227227
int ret;
228+
unsigned int max_ip_conns;
228229

229230
while (!kthread_should_stop()) {
230231
mutex_lock(&iface->sock_release_lock);
@@ -242,34 +243,38 @@ static int ksmbd_kthread_fn(void *p)
242243
continue;
243244
}
244245

246+
if (!server_conf.max_ip_connections)
247+
goto skip_max_ip_conns_limit;
248+
245249
/*
246250
* Limits repeated connections from clients with the same IP.
247251
*/
252+
max_ip_conns = 0;
248253
down_read(&conn_list_lock);
249-
list_for_each_entry(conn, &conn_list, conns_list)
254+
list_for_each_entry(conn, &conn_list, conns_list) {
250255
#if IS_ENABLED(CONFIG_IPV6)
251256
if (client_sk->sk->sk_family == AF_INET6) {
252257
if (memcmp(&client_sk->sk->sk_v6_daddr,
253-
&conn->inet6_addr, 16) == 0) {
254-
ret = -EAGAIN;
255-
break;
256-
}
258+
&conn->inet6_addr, 16) == 0)
259+
max_ip_conns++;
257260
} else if (inet_sk(client_sk->sk)->inet_daddr ==
258-
conn->inet_addr) {
259-
ret = -EAGAIN;
260-
break;
261-
}
261+
conn->inet_addr)
262+
max_ip_conns++;
262263
#else
263264
if (inet_sk(client_sk->sk)->inet_daddr ==
264-
conn->inet_addr) {
265+
conn->inet_addr)
266+
max_ip_conns++;
267+
#endif
268+
if (server_conf.max_ip_connections <= max_ip_conns) {
265269
ret = -EAGAIN;
266270
break;
267271
}
268-
#endif
272+
}
269273
up_read(&conn_list_lock);
270274
if (ret == -EAGAIN)
271275
continue;
272276

277+
skip_max_ip_conns_limit:
273278
if (server_conf.max_connections &&
274279
atomic_inc_return(&active_num_conn) >= server_conf.max_connections) {
275280
pr_info_ratelimited("Limit the maximum number of connections(%u)\n",

0 commit comments

Comments
 (0)