1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 #include <linux/freezer.h>
9 #include "smb_common.h"
12 #include "connection.h"
13 #include "transport_tcp.h"
15 #define IFACE_STATE_DOWN BIT(0)
16 #define IFACE_STATE_CONFIGURED BIT(1)
18 static atomic_t active_num_conn;
21 struct task_struct *ksmbd_kthread;
22 struct socket *ksmbd_socket;
23 struct list_head entry;
25 struct mutex sock_release_lock;
29 static LIST_HEAD(iface_list);
31 static int bind_additional_ifaces;
33 struct tcp_transport {
34 struct ksmbd_transport transport;
40 static struct ksmbd_transport_ops ksmbd_tcp_transport_ops;
42 static void tcp_stop_kthread(struct task_struct *kthread);
43 static struct interface *alloc_iface(char *ifname);
45 #define KSMBD_TRANS(t) (&(t)->transport)
46 #define TCP_TRANS(t) ((struct tcp_transport *)container_of(t, \
47 struct tcp_transport, transport))
49 static inline void ksmbd_tcp_nodelay(struct socket *sock)
51 tcp_sock_set_nodelay(sock->sk);
54 static inline void ksmbd_tcp_reuseaddr(struct socket *sock)
56 sock_set_reuseaddr(sock->sk);
59 static inline void ksmbd_tcp_rcv_timeout(struct socket *sock, s64 secs)
62 if (secs && secs < MAX_SCHEDULE_TIMEOUT / HZ - 1)
63 sock->sk->sk_rcvtimeo = secs * HZ;
65 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
66 release_sock(sock->sk);
69 static inline void ksmbd_tcp_snd_timeout(struct socket *sock, s64 secs)
71 sock_set_sndtimeo(sock->sk, secs);
74 static struct tcp_transport *alloc_transport(struct socket *client_sk)
76 struct tcp_transport *t;
77 struct ksmbd_conn *conn;
79 t = kzalloc(sizeof(*t), GFP_KERNEL);
84 conn = ksmbd_conn_alloc();
90 conn->transport = KSMBD_TRANS(t);
91 KSMBD_TRANS(t)->conn = conn;
92 KSMBD_TRANS(t)->ops = &ksmbd_tcp_transport_ops;
96 static void free_transport(struct tcp_transport *t)
98 kernel_sock_shutdown(t->sock, SHUT_RDWR);
99 sock_release(t->sock);
102 ksmbd_conn_free(KSMBD_TRANS(t)->conn);
108 * kvec_array_init() - initialize a IO vector segment
109 * @new: IO vector to be initialized
110 * @iov: base IO vector
111 * @nr_segs: number of segments in base iov
112 * @bytes: total iovec length so far for read
114 * Return: Number of IO segments
116 static unsigned int kvec_array_init(struct kvec *new, struct kvec *iov,
117 unsigned int nr_segs, size_t bytes)
121 while (bytes || !iov->iov_len) {
122 int copy = min(bytes, iov->iov_len);
126 if (iov->iov_len == base) {
133 memcpy(new, iov, sizeof(*iov) * nr_segs);
134 new->iov_base += base;
135 new->iov_len -= base;
140 * get_conn_iovec() - get connection iovec for reading from socket
141 * @t: TCP transport instance
142 * @nr_segs: number of segments in iov
144 * Return: return existing or newly allocate iovec
146 static struct kvec *get_conn_iovec(struct tcp_transport *t, unsigned int nr_segs)
148 struct kvec *new_iov;
150 if (t->iov && nr_segs <= t->nr_iov)
153 /* not big enough -- allocate a new one and release the old */
154 new_iov = kmalloc_array(nr_segs, sizeof(*new_iov), GFP_KERNEL);
163 static unsigned short ksmbd_tcp_get_port(const struct sockaddr *sa)
165 switch (sa->sa_family) {
167 return ntohs(((struct sockaddr_in *)sa)->sin_port);
169 return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
175 * ksmbd_tcp_new_connection() - create a new tcp session on mount
176 * @client_sk: socket associated with new connection
178 * whenever a new connection is requested, create a conn thread
179 * (session thread) to handle new incoming smb requests from the connection
181 * Return: 0 on success, otherwise error
183 static int ksmbd_tcp_new_connection(struct socket *client_sk)
185 struct sockaddr *csin;
187 struct tcp_transport *t;
189 t = alloc_transport(client_sk);
191 sock_release(client_sk);
195 csin = KSMBD_TCP_PEER_SOCKADDR(KSMBD_TRANS(t)->conn);
196 if (kernel_getpeername(client_sk, csin) < 0) {
197 pr_err("client ip resolution failed\n");
202 KSMBD_TRANS(t)->handler = kthread_run(ksmbd_conn_handler_loop,
203 KSMBD_TRANS(t)->conn,
205 ksmbd_tcp_get_port(csin));
206 if (IS_ERR(KSMBD_TRANS(t)->handler)) {
207 pr_err("cannot start conn thread\n");
208 rc = PTR_ERR(KSMBD_TRANS(t)->handler);
219 * ksmbd_kthread_fn() - listen to new SMB connections and callback server
220 * @p: arguments to forker thread
222 * Return: 0 on success, error number otherwise
224 static int ksmbd_kthread_fn(void *p)
226 struct socket *client_sk = NULL;
227 struct interface *iface = (struct interface *)p;
230 while (!kthread_should_stop()) {
231 mutex_lock(&iface->sock_release_lock);
232 if (!iface->ksmbd_socket) {
233 mutex_unlock(&iface->sock_release_lock);
236 ret = kernel_accept(iface->ksmbd_socket, &client_sk,
238 mutex_unlock(&iface->sock_release_lock);
241 /* check for new connections every 100 msecs */
242 schedule_timeout_interruptible(HZ / 10);
246 if (server_conf.max_connections &&
247 atomic_inc_return(&active_num_conn) >= server_conf.max_connections) {
248 pr_info_ratelimited("Limit the maximum number of connections(%u)\n",
249 atomic_read(&active_num_conn));
250 atomic_dec(&active_num_conn);
251 sock_release(client_sk);
255 ksmbd_debug(CONN, "connect success: accepted new connection\n");
256 client_sk->sk->sk_rcvtimeo = KSMBD_TCP_RECV_TIMEOUT;
257 client_sk->sk->sk_sndtimeo = KSMBD_TCP_SEND_TIMEOUT;
259 ksmbd_tcp_new_connection(client_sk);
262 ksmbd_debug(CONN, "releasing socket\n");
267 * ksmbd_tcp_run_kthread() - start forker thread
268 * @iface: pointer to struct interface
270 * start forker thread(ksmbd/0) at module init time to listen
271 * on port 445 for new SMB connection requests. It creates per connection
272 * server threads(ksmbd/x)
274 * Return: 0 on success or error number
276 static int ksmbd_tcp_run_kthread(struct interface *iface)
279 struct task_struct *kthread;
281 kthread = kthread_run(ksmbd_kthread_fn, (void *)iface, "ksmbd-%s",
283 if (IS_ERR(kthread)) {
284 rc = PTR_ERR(kthread);
287 iface->ksmbd_kthread = kthread;
293 * ksmbd_tcp_readv() - read data from socket in given iovec
294 * @t: TCP transport instance
295 * @iov_orig: base IO vector
296 * @nr_segs: number of segments in base iov
297 * @to_read: number of bytes to read from socket
299 * Return: on success return number of bytes read from socket,
300 * otherwise return error number
302 static int ksmbd_tcp_readv(struct tcp_transport *t, struct kvec *iov_orig,
303 unsigned int nr_segs, unsigned int to_read)
308 struct msghdr ksmbd_msg;
310 struct ksmbd_conn *conn = KSMBD_TRANS(t)->conn;
313 iov = get_conn_iovec(t, nr_segs);
317 ksmbd_msg.msg_control = NULL;
318 ksmbd_msg.msg_controllen = 0;
320 for (total_read = 0; to_read; total_read += length, to_read -= length) {
323 if (!ksmbd_conn_alive(conn)) {
324 total_read = -ESHUTDOWN;
327 segs = kvec_array_init(iov, iov_orig, nr_segs, total_read);
329 length = kernel_recvmsg(t->sock, &ksmbd_msg,
330 iov, segs, to_read, 0);
332 if (length == -EINTR) {
333 total_read = -ESHUTDOWN;
335 } else if (conn->status == KSMBD_SESS_NEED_RECONNECT) {
336 total_read = -EAGAIN;
338 } else if ((length == -ERESTARTSYS || length == -EAGAIN) &&
340 usleep_range(1000, 2000);
344 } else if (length <= 0) {
345 total_read = -EAGAIN;
353 * ksmbd_tcp_read() - read data from socket in given buffer
354 * @t: TCP transport instance
355 * @buf: buffer to store read data from socket
356 * @to_read: number of bytes to read from socket
358 * Return: on success return number of bytes read from socket,
359 * otherwise return error number
361 static int ksmbd_tcp_read(struct ksmbd_transport *t, char *buf, unsigned int to_read)
366 iov.iov_len = to_read;
368 return ksmbd_tcp_readv(TCP_TRANS(t), &iov, 1, to_read);
371 static int ksmbd_tcp_writev(struct ksmbd_transport *t, struct kvec *iov,
372 int nvecs, int size, bool need_invalidate,
373 unsigned int remote_key)
376 struct msghdr smb_msg = {.msg_flags = MSG_NOSIGNAL};
378 return kernel_sendmsg(TCP_TRANS(t)->sock, &smb_msg, iov, nvecs, size);
381 static void ksmbd_tcp_disconnect(struct ksmbd_transport *t)
383 free_transport(TCP_TRANS(t));
384 if (server_conf.max_connections)
385 atomic_dec(&active_num_conn);
388 static void tcp_destroy_socket(struct socket *ksmbd_socket)
395 /* set zero to timeout */
396 ksmbd_tcp_rcv_timeout(ksmbd_socket, 0);
397 ksmbd_tcp_snd_timeout(ksmbd_socket, 0);
399 ret = kernel_sock_shutdown(ksmbd_socket, SHUT_RDWR);
401 pr_err("Failed to shutdown socket: %d\n", ret);
402 sock_release(ksmbd_socket);
406 * create_socket - create socket for ksmbd/0
408 * Return: 0 on success, error number otherwise
410 static int create_socket(struct interface *iface)
413 struct sockaddr_in6 sin6;
414 struct sockaddr_in sin;
415 struct socket *ksmbd_socket;
418 ret = sock_create(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &ksmbd_socket);
420 if (ret != -EAFNOSUPPORT)
421 pr_err("Can't create socket for ipv6, fallback to ipv4: %d\n", ret);
422 ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP,
425 pr_err("Can't create socket for ipv4: %d\n", ret);
429 sin.sin_family = PF_INET;
430 sin.sin_addr.s_addr = htonl(INADDR_ANY);
431 sin.sin_port = htons(server_conf.tcp_port);
434 sin6.sin6_family = PF_INET6;
435 sin6.sin6_addr = in6addr_any;
436 sin6.sin6_port = htons(server_conf.tcp_port);
439 ksmbd_tcp_nodelay(ksmbd_socket);
440 ksmbd_tcp_reuseaddr(ksmbd_socket);
442 ret = sock_setsockopt(ksmbd_socket,
445 KERNEL_SOCKPTR(iface->name),
446 strlen(iface->name));
447 if (ret != -ENODEV && ret < 0) {
448 pr_err("Failed to set SO_BINDTODEVICE: %d\n", ret);
453 ret = kernel_bind(ksmbd_socket, (struct sockaddr *)&sin,
456 ret = kernel_bind(ksmbd_socket, (struct sockaddr *)&sin6,
459 pr_err("Failed to bind socket: %d\n", ret);
463 ksmbd_socket->sk->sk_rcvtimeo = KSMBD_TCP_RECV_TIMEOUT;
464 ksmbd_socket->sk->sk_sndtimeo = KSMBD_TCP_SEND_TIMEOUT;
466 ret = kernel_listen(ksmbd_socket, KSMBD_SOCKET_BACKLOG);
468 pr_err("Port listen() error: %d\n", ret);
472 iface->ksmbd_socket = ksmbd_socket;
473 ret = ksmbd_tcp_run_kthread(iface);
475 pr_err("Can't start ksmbd main kthread: %d\n", ret);
478 iface->state = IFACE_STATE_CONFIGURED;
483 tcp_destroy_socket(ksmbd_socket);
485 iface->ksmbd_socket = NULL;
489 static int ksmbd_netdev_event(struct notifier_block *nb, unsigned long event,
492 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
493 struct interface *iface;
498 if (netif_is_bridge_port(netdev))
501 list_for_each_entry(iface, &iface_list, entry) {
502 if (!strcmp(iface->name, netdev->name)) {
504 if (iface->state != IFACE_STATE_DOWN)
506 ret = create_socket(iface);
512 if (!found && bind_additional_ifaces) {
513 iface = alloc_iface(kstrdup(netdev->name, GFP_KERNEL));
516 ret = create_socket(iface);
522 list_for_each_entry(iface, &iface_list, entry) {
523 if (!strcmp(iface->name, netdev->name) &&
524 iface->state == IFACE_STATE_CONFIGURED) {
525 tcp_stop_kthread(iface->ksmbd_kthread);
526 iface->ksmbd_kthread = NULL;
527 mutex_lock(&iface->sock_release_lock);
528 tcp_destroy_socket(iface->ksmbd_socket);
529 iface->ksmbd_socket = NULL;
530 mutex_unlock(&iface->sock_release_lock);
532 iface->state = IFACE_STATE_DOWN;
542 static struct notifier_block ksmbd_netdev_notifier = {
543 .notifier_call = ksmbd_netdev_event,
546 int ksmbd_tcp_init(void)
548 register_netdevice_notifier(&ksmbd_netdev_notifier);
553 static void tcp_stop_kthread(struct task_struct *kthread)
560 ret = kthread_stop(kthread);
562 pr_err("failed to stop forker thread\n");
565 void ksmbd_tcp_destroy(void)
567 struct interface *iface, *tmp;
569 unregister_netdevice_notifier(&ksmbd_netdev_notifier);
571 list_for_each_entry_safe(iface, tmp, &iface_list, entry) {
572 list_del(&iface->entry);
578 static struct interface *alloc_iface(char *ifname)
580 struct interface *iface;
585 iface = kzalloc(sizeof(struct interface), GFP_KERNEL);
591 iface->name = ifname;
592 iface->state = IFACE_STATE_DOWN;
593 list_add(&iface->entry, &iface_list);
594 mutex_init(&iface->sock_release_lock);
598 int ksmbd_tcp_set_interfaces(char *ifc_list, int ifc_list_sz)
603 struct net_device *netdev;
606 for_each_netdev(&init_net, netdev) {
607 if (netif_is_bridge_port(netdev))
609 if (!alloc_iface(kstrdup(netdev->name, GFP_KERNEL)))
613 bind_additional_ifaces = 1;
617 while (ifc_list_sz > 0) {
618 if (!alloc_iface(kstrdup(ifc_list, GFP_KERNEL)))
621 sz = strlen(ifc_list);
626 ifc_list_sz -= (sz + 1);
629 bind_additional_ifaces = 0;
634 static struct ksmbd_transport_ops ksmbd_tcp_transport_ops = {
635 .read = ksmbd_tcp_read,
636 .writev = ksmbd_tcp_writev,
637 .disconnect = ksmbd_tcp_disconnect,