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)
19 struct task_struct *ksmbd_kthread;
20 struct socket *ksmbd_socket;
21 struct list_head entry;
23 struct mutex sock_release_lock;
27 static LIST_HEAD(iface_list);
29 static int bind_additional_ifaces;
31 struct tcp_transport {
32 struct ksmbd_transport transport;
38 static struct ksmbd_transport_ops ksmbd_tcp_transport_ops;
40 static void tcp_stop_kthread(struct task_struct *kthread);
41 static struct interface *alloc_iface(char *ifname);
43 #define KSMBD_TRANS(t) (&(t)->transport)
44 #define TCP_TRANS(t) ((struct tcp_transport *)container_of(t, \
45 struct tcp_transport, transport))
47 static inline void ksmbd_tcp_nodelay(struct socket *sock)
49 tcp_sock_set_nodelay(sock->sk);
52 static inline void ksmbd_tcp_reuseaddr(struct socket *sock)
54 sock_set_reuseaddr(sock->sk);
57 static inline void ksmbd_tcp_rcv_timeout(struct socket *sock, s64 secs)
60 if (secs && secs < MAX_SCHEDULE_TIMEOUT / HZ - 1)
61 sock->sk->sk_rcvtimeo = secs * HZ;
63 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
64 release_sock(sock->sk);
67 static inline void ksmbd_tcp_snd_timeout(struct socket *sock, s64 secs)
69 sock_set_sndtimeo(sock->sk, secs);
72 static struct tcp_transport *alloc_transport(struct socket *client_sk)
74 struct tcp_transport *t;
75 struct ksmbd_conn *conn;
77 t = kzalloc(sizeof(*t), GFP_KERNEL);
82 conn = ksmbd_conn_alloc();
88 conn->transport = KSMBD_TRANS(t);
89 KSMBD_TRANS(t)->conn = conn;
90 KSMBD_TRANS(t)->ops = &ksmbd_tcp_transport_ops;
94 static void free_transport(struct tcp_transport *t)
96 kernel_sock_shutdown(t->sock, SHUT_RDWR);
97 sock_release(t->sock);
100 ksmbd_conn_free(KSMBD_TRANS(t)->conn);
106 * kvec_array_init() - initialize a IO vector segment
107 * @new: IO vector to be initialized
108 * @iov: base IO vector
109 * @nr_segs: number of segments in base iov
110 * @bytes: total iovec length so far for read
112 * Return: Number of IO segments
114 static unsigned int kvec_array_init(struct kvec *new, struct kvec *iov,
115 unsigned int nr_segs, size_t bytes)
119 while (bytes || !iov->iov_len) {
120 int copy = min(bytes, iov->iov_len);
124 if (iov->iov_len == base) {
131 memcpy(new, iov, sizeof(*iov) * nr_segs);
132 new->iov_base += base;
133 new->iov_len -= base;
138 * get_conn_iovec() - get connection iovec for reading from socket
139 * @t: TCP transport instance
140 * @nr_segs: number of segments in iov
142 * Return: return existing or newly allocate iovec
144 static struct kvec *get_conn_iovec(struct tcp_transport *t, unsigned int nr_segs)
146 struct kvec *new_iov;
148 if (t->iov && nr_segs <= t->nr_iov)
151 /* not big enough -- allocate a new one and release the old */
152 new_iov = kmalloc_array(nr_segs, sizeof(*new_iov), GFP_KERNEL);
161 static unsigned short ksmbd_tcp_get_port(const struct sockaddr *sa)
163 switch (sa->sa_family) {
165 return ntohs(((struct sockaddr_in *)sa)->sin_port);
167 return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
173 * ksmbd_tcp_new_connection() - create a new tcp session on mount
174 * @client_sk: socket associated with new connection
176 * whenever a new connection is requested, create a conn thread
177 * (session thread) to handle new incoming smb requests from the connection
179 * Return: 0 on success, otherwise error
181 static int ksmbd_tcp_new_connection(struct socket *client_sk)
183 struct sockaddr *csin;
185 struct tcp_transport *t;
187 t = alloc_transport(client_sk);
191 csin = KSMBD_TCP_PEER_SOCKADDR(KSMBD_TRANS(t)->conn);
192 if (kernel_getpeername(client_sk, csin) < 0) {
193 pr_err("client ip resolution failed\n");
198 KSMBD_TRANS(t)->handler = kthread_run(ksmbd_conn_handler_loop,
199 KSMBD_TRANS(t)->conn,
201 ksmbd_tcp_get_port(csin));
202 if (IS_ERR(KSMBD_TRANS(t)->handler)) {
203 pr_err("cannot start conn thread\n");
204 rc = PTR_ERR(KSMBD_TRANS(t)->handler);
215 * ksmbd_kthread_fn() - listen to new SMB connections and callback server
216 * @p: arguments to forker thread
218 * Return: 0 on success, error number otherwise
220 static int ksmbd_kthread_fn(void *p)
222 struct socket *client_sk = NULL;
223 struct interface *iface = (struct interface *)p;
226 while (!kthread_should_stop()) {
227 mutex_lock(&iface->sock_release_lock);
228 if (!iface->ksmbd_socket) {
229 mutex_unlock(&iface->sock_release_lock);
232 ret = kernel_accept(iface->ksmbd_socket, &client_sk,
234 mutex_unlock(&iface->sock_release_lock);
237 /* check for new connections every 100 msecs */
238 schedule_timeout_interruptible(HZ / 10);
242 ksmbd_debug(CONN, "connect success: accepted new connection\n");
243 client_sk->sk->sk_rcvtimeo = KSMBD_TCP_RECV_TIMEOUT;
244 client_sk->sk->sk_sndtimeo = KSMBD_TCP_SEND_TIMEOUT;
246 ksmbd_tcp_new_connection(client_sk);
249 ksmbd_debug(CONN, "releasing socket\n");
254 * ksmbd_tcp_run_kthread() - start forker thread
255 * @iface: pointer to struct interface
257 * start forker thread(ksmbd/0) at module init time to listen
258 * on port 445 for new SMB connection requests. It creates per connection
259 * server threads(ksmbd/x)
261 * Return: 0 on success or error number
263 static int ksmbd_tcp_run_kthread(struct interface *iface)
266 struct task_struct *kthread;
268 kthread = kthread_run(ksmbd_kthread_fn, (void *)iface, "ksmbd-%s",
270 if (IS_ERR(kthread)) {
271 rc = PTR_ERR(kthread);
274 iface->ksmbd_kthread = kthread;
280 * ksmbd_tcp_readv() - read data from socket in given iovec
281 * @t: TCP transport instance
282 * @iov_orig: base IO vector
283 * @nr_segs: number of segments in base iov
284 * @to_read: number of bytes to read from socket
286 * Return: on success return number of bytes read from socket,
287 * otherwise return error number
289 static int ksmbd_tcp_readv(struct tcp_transport *t, struct kvec *iov_orig,
290 unsigned int nr_segs, unsigned int to_read)
295 struct msghdr ksmbd_msg;
297 struct ksmbd_conn *conn = KSMBD_TRANS(t)->conn;
299 iov = get_conn_iovec(t, nr_segs);
303 ksmbd_msg.msg_control = NULL;
304 ksmbd_msg.msg_controllen = 0;
306 for (total_read = 0; to_read; total_read += length, to_read -= length) {
309 if (!ksmbd_conn_alive(conn)) {
310 total_read = -ESHUTDOWN;
313 segs = kvec_array_init(iov, iov_orig, nr_segs, total_read);
315 length = kernel_recvmsg(t->sock, &ksmbd_msg,
316 iov, segs, to_read, 0);
318 if (length == -EINTR) {
319 total_read = -ESHUTDOWN;
321 } else if (conn->status == KSMBD_SESS_NEED_RECONNECT) {
322 total_read = -EAGAIN;
324 } else if (length == -ERESTARTSYS || length == -EAGAIN) {
325 usleep_range(1000, 2000);
328 } else if (length <= 0) {
329 total_read = -EAGAIN;
337 * ksmbd_tcp_read() - read data from socket in given buffer
338 * @t: TCP transport instance
339 * @buf: buffer to store read data from socket
340 * @to_read: number of bytes to read from socket
342 * Return: on success return number of bytes read from socket,
343 * otherwise return error number
345 static int ksmbd_tcp_read(struct ksmbd_transport *t, char *buf, unsigned int to_read)
350 iov.iov_len = to_read;
352 return ksmbd_tcp_readv(TCP_TRANS(t), &iov, 1, to_read);
355 static int ksmbd_tcp_writev(struct ksmbd_transport *t, struct kvec *iov,
356 int nvecs, int size, bool need_invalidate,
357 unsigned int remote_key)
360 struct msghdr smb_msg = {.msg_flags = MSG_NOSIGNAL};
362 return kernel_sendmsg(TCP_TRANS(t)->sock, &smb_msg, iov, nvecs, size);
365 static void ksmbd_tcp_disconnect(struct ksmbd_transport *t)
367 free_transport(TCP_TRANS(t));
370 static void tcp_destroy_socket(struct socket *ksmbd_socket)
377 /* set zero to timeout */
378 ksmbd_tcp_rcv_timeout(ksmbd_socket, 0);
379 ksmbd_tcp_snd_timeout(ksmbd_socket, 0);
381 ret = kernel_sock_shutdown(ksmbd_socket, SHUT_RDWR);
383 pr_err("Failed to shutdown socket: %d\n", ret);
384 sock_release(ksmbd_socket);
388 * create_socket - create socket for ksmbd/0
390 * Return: 0 on success, error number otherwise
392 static int create_socket(struct interface *iface)
395 struct sockaddr_in6 sin6;
396 struct sockaddr_in sin;
397 struct socket *ksmbd_socket;
400 ret = sock_create(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &ksmbd_socket);
402 pr_err("Can't create socket for ipv6, try ipv4: %d\n", ret);
403 ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP,
406 pr_err("Can't create socket for ipv4: %d\n", ret);
410 sin.sin_family = PF_INET;
411 sin.sin_addr.s_addr = htonl(INADDR_ANY);
412 sin.sin_port = htons(server_conf.tcp_port);
415 sin6.sin6_family = PF_INET6;
416 sin6.sin6_addr = in6addr_any;
417 sin6.sin6_port = htons(server_conf.tcp_port);
420 ksmbd_tcp_nodelay(ksmbd_socket);
421 ksmbd_tcp_reuseaddr(ksmbd_socket);
423 ret = sock_setsockopt(ksmbd_socket,
426 KERNEL_SOCKPTR(iface->name),
427 strlen(iface->name));
428 if (ret != -ENODEV && ret < 0) {
429 pr_err("Failed to set SO_BINDTODEVICE: %d\n", ret);
434 ret = kernel_bind(ksmbd_socket, (struct sockaddr *)&sin,
437 ret = kernel_bind(ksmbd_socket, (struct sockaddr *)&sin6,
440 pr_err("Failed to bind socket: %d\n", ret);
444 ksmbd_socket->sk->sk_rcvtimeo = KSMBD_TCP_RECV_TIMEOUT;
445 ksmbd_socket->sk->sk_sndtimeo = KSMBD_TCP_SEND_TIMEOUT;
447 ret = kernel_listen(ksmbd_socket, KSMBD_SOCKET_BACKLOG);
449 pr_err("Port listen() error: %d\n", ret);
453 iface->ksmbd_socket = ksmbd_socket;
454 ret = ksmbd_tcp_run_kthread(iface);
456 pr_err("Can't start ksmbd main kthread: %d\n", ret);
459 iface->state = IFACE_STATE_CONFIGURED;
464 tcp_destroy_socket(ksmbd_socket);
465 iface->ksmbd_socket = NULL;
469 static int ksmbd_netdev_event(struct notifier_block *nb, unsigned long event,
472 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
473 struct interface *iface;
478 if (netdev->priv_flags & IFF_BRIDGE_PORT)
481 list_for_each_entry(iface, &iface_list, entry) {
482 if (!strcmp(iface->name, netdev->name)) {
484 if (iface->state != IFACE_STATE_DOWN)
486 ret = create_socket(iface);
492 if (!found && bind_additional_ifaces) {
493 iface = alloc_iface(kstrdup(netdev->name, GFP_KERNEL));
496 ret = create_socket(iface);
502 list_for_each_entry(iface, &iface_list, entry) {
503 if (!strcmp(iface->name, netdev->name) &&
504 iface->state == IFACE_STATE_CONFIGURED) {
505 tcp_stop_kthread(iface->ksmbd_kthread);
506 iface->ksmbd_kthread = NULL;
507 mutex_lock(&iface->sock_release_lock);
508 tcp_destroy_socket(iface->ksmbd_socket);
509 iface->ksmbd_socket = NULL;
510 mutex_unlock(&iface->sock_release_lock);
512 iface->state = IFACE_STATE_DOWN;
522 static struct notifier_block ksmbd_netdev_notifier = {
523 .notifier_call = ksmbd_netdev_event,
526 int ksmbd_tcp_init(void)
528 register_netdevice_notifier(&ksmbd_netdev_notifier);
533 static void tcp_stop_kthread(struct task_struct *kthread)
540 ret = kthread_stop(kthread);
542 pr_err("failed to stop forker thread\n");
545 void ksmbd_tcp_destroy(void)
547 struct interface *iface, *tmp;
549 unregister_netdevice_notifier(&ksmbd_netdev_notifier);
551 list_for_each_entry_safe(iface, tmp, &iface_list, entry) {
552 list_del(&iface->entry);
558 static struct interface *alloc_iface(char *ifname)
560 struct interface *iface;
565 iface = kzalloc(sizeof(struct interface), GFP_KERNEL);
571 iface->name = ifname;
572 iface->state = IFACE_STATE_DOWN;
573 list_add(&iface->entry, &iface_list);
574 mutex_init(&iface->sock_release_lock);
578 int ksmbd_tcp_set_interfaces(char *ifc_list, int ifc_list_sz)
583 struct net_device *netdev;
586 for_each_netdev(&init_net, netdev) {
587 if (netdev->priv_flags & IFF_BRIDGE_PORT)
589 if (!alloc_iface(kstrdup(netdev->name, GFP_KERNEL)))
593 bind_additional_ifaces = 1;
597 while (ifc_list_sz > 0) {
598 if (!alloc_iface(kstrdup(ifc_list, GFP_KERNEL)))
601 sz = strlen(ifc_list);
606 ifc_list_sz -= (sz + 1);
609 bind_additional_ifaces = 0;
614 static struct ksmbd_transport_ops ksmbd_tcp_transport_ops = {
615 .read = ksmbd_tcp_read,
616 .writev = ksmbd_tcp_writev,
617 .disconnect = ksmbd_tcp_disconnect,