xref: /openbmc/linux/fs/smb/server/transport_tcp.c (revision ca2478a7d974f38d29d27acb42a952c7f168916e)
138c8a9a5SSteve French // SPDX-License-Identifier: GPL-2.0-or-later
238c8a9a5SSteve French /*
338c8a9a5SSteve French  *   Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
438c8a9a5SSteve French  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
538c8a9a5SSteve French  */
638c8a9a5SSteve French 
738c8a9a5SSteve French #include <linux/freezer.h>
838c8a9a5SSteve French 
938c8a9a5SSteve French #include "smb_common.h"
1038c8a9a5SSteve French #include "server.h"
1138c8a9a5SSteve French #include "auth.h"
1238c8a9a5SSteve French #include "connection.h"
1338c8a9a5SSteve French #include "transport_tcp.h"
1438c8a9a5SSteve French 
1538c8a9a5SSteve French #define IFACE_STATE_DOWN		BIT(0)
1638c8a9a5SSteve French #define IFACE_STATE_CONFIGURED		BIT(1)
1738c8a9a5SSteve French 
1838c8a9a5SSteve French static atomic_t active_num_conn;
1938c8a9a5SSteve French 
2038c8a9a5SSteve French struct interface {
2138c8a9a5SSteve French 	struct task_struct	*ksmbd_kthread;
2238c8a9a5SSteve French 	struct socket		*ksmbd_socket;
2338c8a9a5SSteve French 	struct list_head	entry;
2438c8a9a5SSteve French 	char			*name;
2538c8a9a5SSteve French 	struct mutex		sock_release_lock;
2638c8a9a5SSteve French 	int			state;
2738c8a9a5SSteve French };
2838c8a9a5SSteve French 
2938c8a9a5SSteve French static LIST_HEAD(iface_list);
3038c8a9a5SSteve French 
3138c8a9a5SSteve French static int bind_additional_ifaces;
3238c8a9a5SSteve French 
3338c8a9a5SSteve French struct tcp_transport {
3438c8a9a5SSteve French 	struct ksmbd_transport		transport;
3538c8a9a5SSteve French 	struct socket			*sock;
3638c8a9a5SSteve French 	struct kvec			*iov;
3738c8a9a5SSteve French 	unsigned int			nr_iov;
3838c8a9a5SSteve French };
3938c8a9a5SSteve French 
4038c8a9a5SSteve French static struct ksmbd_transport_ops ksmbd_tcp_transport_ops;
4138c8a9a5SSteve French 
4238c8a9a5SSteve French static void tcp_stop_kthread(struct task_struct *kthread);
4338c8a9a5SSteve French static struct interface *alloc_iface(char *ifname);
4438c8a9a5SSteve French 
4538c8a9a5SSteve French #define KSMBD_TRANS(t)	(&(t)->transport)
4638c8a9a5SSteve French #define TCP_TRANS(t)	((struct tcp_transport *)container_of(t, \
4738c8a9a5SSteve French 				struct tcp_transport, transport))
4838c8a9a5SSteve French 
ksmbd_tcp_nodelay(struct socket * sock)4938c8a9a5SSteve French static inline void ksmbd_tcp_nodelay(struct socket *sock)
5038c8a9a5SSteve French {
5138c8a9a5SSteve French 	tcp_sock_set_nodelay(sock->sk);
5238c8a9a5SSteve French }
5338c8a9a5SSteve French 
ksmbd_tcp_reuseaddr(struct socket * sock)5438c8a9a5SSteve French static inline void ksmbd_tcp_reuseaddr(struct socket *sock)
5538c8a9a5SSteve French {
5638c8a9a5SSteve French 	sock_set_reuseaddr(sock->sk);
5738c8a9a5SSteve French }
5838c8a9a5SSteve French 
ksmbd_tcp_rcv_timeout(struct socket * sock,s64 secs)5938c8a9a5SSteve French static inline void ksmbd_tcp_rcv_timeout(struct socket *sock, s64 secs)
6038c8a9a5SSteve French {
6138c8a9a5SSteve French 	lock_sock(sock->sk);
6238c8a9a5SSteve French 	if (secs && secs < MAX_SCHEDULE_TIMEOUT / HZ - 1)
6338c8a9a5SSteve French 		sock->sk->sk_rcvtimeo = secs * HZ;
6438c8a9a5SSteve French 	else
6538c8a9a5SSteve French 		sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
6638c8a9a5SSteve French 	release_sock(sock->sk);
6738c8a9a5SSteve French }
6838c8a9a5SSteve French 
ksmbd_tcp_snd_timeout(struct socket * sock,s64 secs)6938c8a9a5SSteve French static inline void ksmbd_tcp_snd_timeout(struct socket *sock, s64 secs)
7038c8a9a5SSteve French {
7138c8a9a5SSteve French 	sock_set_sndtimeo(sock->sk, secs);
7238c8a9a5SSteve French }
7338c8a9a5SSteve French 
alloc_transport(struct socket * client_sk)7438c8a9a5SSteve French static struct tcp_transport *alloc_transport(struct socket *client_sk)
7538c8a9a5SSteve French {
7638c8a9a5SSteve French 	struct tcp_transport *t;
7738c8a9a5SSteve French 	struct ksmbd_conn *conn;
7838c8a9a5SSteve French 
7938c8a9a5SSteve French 	t = kzalloc(sizeof(*t), GFP_KERNEL);
8038c8a9a5SSteve French 	if (!t)
8138c8a9a5SSteve French 		return NULL;
8238c8a9a5SSteve French 	t->sock = client_sk;
8338c8a9a5SSteve French 
8438c8a9a5SSteve French 	conn = ksmbd_conn_alloc();
8538c8a9a5SSteve French 	if (!conn) {
8638c8a9a5SSteve French 		kfree(t);
8738c8a9a5SSteve French 		return NULL;
8838c8a9a5SSteve French 	}
8938c8a9a5SSteve French 
9038c8a9a5SSteve French 	conn->transport = KSMBD_TRANS(t);
9138c8a9a5SSteve French 	KSMBD_TRANS(t)->conn = conn;
9238c8a9a5SSteve French 	KSMBD_TRANS(t)->ops = &ksmbd_tcp_transport_ops;
9338c8a9a5SSteve French 	return t;
9438c8a9a5SSteve French }
9538c8a9a5SSteve French 
free_transport(struct tcp_transport * t)9638c8a9a5SSteve French static void free_transport(struct tcp_transport *t)
9738c8a9a5SSteve French {
9838c8a9a5SSteve French 	kernel_sock_shutdown(t->sock, SHUT_RDWR);
9938c8a9a5SSteve French 	sock_release(t->sock);
10038c8a9a5SSteve French 	t->sock = NULL;
10138c8a9a5SSteve French 
10238c8a9a5SSteve French 	ksmbd_conn_free(KSMBD_TRANS(t)->conn);
10338c8a9a5SSteve French 	kfree(t->iov);
10438c8a9a5SSteve French 	kfree(t);
10538c8a9a5SSteve French }
10638c8a9a5SSteve French 
10738c8a9a5SSteve French /**
10838c8a9a5SSteve French  * kvec_array_init() - initialize a IO vector segment
10938c8a9a5SSteve French  * @new:	IO vector to be initialized
11038c8a9a5SSteve French  * @iov:	base IO vector
11138c8a9a5SSteve French  * @nr_segs:	number of segments in base iov
11238c8a9a5SSteve French  * @bytes:	total iovec length so far for read
11338c8a9a5SSteve French  *
11438c8a9a5SSteve French  * Return:	Number of IO segments
11538c8a9a5SSteve French  */
kvec_array_init(struct kvec * new,struct kvec * iov,unsigned int nr_segs,size_t bytes)11638c8a9a5SSteve French static unsigned int kvec_array_init(struct kvec *new, struct kvec *iov,
11738c8a9a5SSteve French 				    unsigned int nr_segs, size_t bytes)
11838c8a9a5SSteve French {
11938c8a9a5SSteve French 	size_t base = 0;
12038c8a9a5SSteve French 
12138c8a9a5SSteve French 	while (bytes || !iov->iov_len) {
12238c8a9a5SSteve French 		int copy = min(bytes, iov->iov_len);
12338c8a9a5SSteve French 
12438c8a9a5SSteve French 		bytes -= copy;
12538c8a9a5SSteve French 		base += copy;
12638c8a9a5SSteve French 		if (iov->iov_len == base) {
12738c8a9a5SSteve French 			iov++;
12838c8a9a5SSteve French 			nr_segs--;
12938c8a9a5SSteve French 			base = 0;
13038c8a9a5SSteve French 		}
13138c8a9a5SSteve French 	}
13238c8a9a5SSteve French 
13338c8a9a5SSteve French 	memcpy(new, iov, sizeof(*iov) * nr_segs);
13438c8a9a5SSteve French 	new->iov_base += base;
13538c8a9a5SSteve French 	new->iov_len -= base;
13638c8a9a5SSteve French 	return nr_segs;
13738c8a9a5SSteve French }
13838c8a9a5SSteve French 
13938c8a9a5SSteve French /**
14038c8a9a5SSteve French  * get_conn_iovec() - get connection iovec for reading from socket
14138c8a9a5SSteve French  * @t:		TCP transport instance
14238c8a9a5SSteve French  * @nr_segs:	number of segments in iov
14338c8a9a5SSteve French  *
14438c8a9a5SSteve French  * Return:	return existing or newly allocate iovec
14538c8a9a5SSteve French  */
get_conn_iovec(struct tcp_transport * t,unsigned int nr_segs)14638c8a9a5SSteve French static struct kvec *get_conn_iovec(struct tcp_transport *t, unsigned int nr_segs)
14738c8a9a5SSteve French {
14838c8a9a5SSteve French 	struct kvec *new_iov;
14938c8a9a5SSteve French 
15038c8a9a5SSteve French 	if (t->iov && nr_segs <= t->nr_iov)
15138c8a9a5SSteve French 		return t->iov;
15238c8a9a5SSteve French 
15338c8a9a5SSteve French 	/* not big enough -- allocate a new one and release the old */
15438c8a9a5SSteve French 	new_iov = kmalloc_array(nr_segs, sizeof(*new_iov), GFP_KERNEL);
15538c8a9a5SSteve French 	if (new_iov) {
15638c8a9a5SSteve French 		kfree(t->iov);
15738c8a9a5SSteve French 		t->iov = new_iov;
15838c8a9a5SSteve French 		t->nr_iov = nr_segs;
15938c8a9a5SSteve French 	}
16038c8a9a5SSteve French 	return new_iov;
16138c8a9a5SSteve French }
16238c8a9a5SSteve French 
ksmbd_tcp_get_port(const struct sockaddr * sa)16338c8a9a5SSteve French static unsigned short ksmbd_tcp_get_port(const struct sockaddr *sa)
16438c8a9a5SSteve French {
16538c8a9a5SSteve French 	switch (sa->sa_family) {
16638c8a9a5SSteve French 	case AF_INET:
16738c8a9a5SSteve French 		return ntohs(((struct sockaddr_in *)sa)->sin_port);
16838c8a9a5SSteve French 	case AF_INET6:
16938c8a9a5SSteve French 		return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
17038c8a9a5SSteve French 	}
17138c8a9a5SSteve French 	return 0;
17238c8a9a5SSteve French }
17338c8a9a5SSteve French 
17438c8a9a5SSteve French /**
17538c8a9a5SSteve French  * ksmbd_tcp_new_connection() - create a new tcp session on mount
17638c8a9a5SSteve French  * @client_sk:	socket associated with new connection
17738c8a9a5SSteve French  *
17838c8a9a5SSteve French  * whenever a new connection is requested, create a conn thread
17938c8a9a5SSteve French  * (session thread) to handle new incoming smb requests from the connection
18038c8a9a5SSteve French  *
18138c8a9a5SSteve French  * Return:	0 on success, otherwise error
18238c8a9a5SSteve French  */
ksmbd_tcp_new_connection(struct socket * client_sk)18338c8a9a5SSteve French static int ksmbd_tcp_new_connection(struct socket *client_sk)
18438c8a9a5SSteve French {
18538c8a9a5SSteve French 	struct sockaddr *csin;
18638c8a9a5SSteve French 	int rc = 0;
18738c8a9a5SSteve French 	struct tcp_transport *t;
18824290ba9SNamjae Jeon 	struct task_struct *handler;
18938c8a9a5SSteve French 
19038c8a9a5SSteve French 	t = alloc_transport(client_sk);
19138c8a9a5SSteve French 	if (!t) {
19238c8a9a5SSteve French 		sock_release(client_sk);
19338c8a9a5SSteve French 		return -ENOMEM;
19438c8a9a5SSteve French 	}
19538c8a9a5SSteve French 
19638c8a9a5SSteve French 	csin = KSMBD_TCP_PEER_SOCKADDR(KSMBD_TRANS(t)->conn);
19738c8a9a5SSteve French 	if (kernel_getpeername(client_sk, csin) < 0) {
19838c8a9a5SSteve French 		pr_err("client ip resolution failed\n");
19938c8a9a5SSteve French 		rc = -EINVAL;
20038c8a9a5SSteve French 		goto out_error;
20138c8a9a5SSteve French 	}
20238c8a9a5SSteve French 
20324290ba9SNamjae Jeon 	handler = kthread_run(ksmbd_conn_handler_loop,
20438c8a9a5SSteve French 			      KSMBD_TRANS(t)->conn,
20538c8a9a5SSteve French 			      "ksmbd:%u",
20638c8a9a5SSteve French 			      ksmbd_tcp_get_port(csin));
20724290ba9SNamjae Jeon 	if (IS_ERR(handler)) {
20838c8a9a5SSteve French 		pr_err("cannot start conn thread\n");
20924290ba9SNamjae Jeon 		rc = PTR_ERR(handler);
21038c8a9a5SSteve French 		free_transport(t);
21138c8a9a5SSteve French 	}
21238c8a9a5SSteve French 	return rc;
21338c8a9a5SSteve French 
21438c8a9a5SSteve French out_error:
21538c8a9a5SSteve French 	free_transport(t);
21638c8a9a5SSteve French 	return rc;
21738c8a9a5SSteve French }
21838c8a9a5SSteve French 
21938c8a9a5SSteve French /**
22038c8a9a5SSteve French  * ksmbd_kthread_fn() - listen to new SMB connections and callback server
22138c8a9a5SSteve French  * @p:		arguments to forker thread
22238c8a9a5SSteve French  *
22338c8a9a5SSteve French  * Return:	0 on success, error number otherwise
22438c8a9a5SSteve French  */
ksmbd_kthread_fn(void * p)22538c8a9a5SSteve French static int ksmbd_kthread_fn(void *p)
22638c8a9a5SSteve French {
22738c8a9a5SSteve French 	struct socket *client_sk = NULL;
22838c8a9a5SSteve French 	struct interface *iface = (struct interface *)p;
22938c8a9a5SSteve French 	int ret;
23038c8a9a5SSteve French 
23138c8a9a5SSteve French 	while (!kthread_should_stop()) {
23238c8a9a5SSteve French 		mutex_lock(&iface->sock_release_lock);
23338c8a9a5SSteve French 		if (!iface->ksmbd_socket) {
23438c8a9a5SSteve French 			mutex_unlock(&iface->sock_release_lock);
23538c8a9a5SSteve French 			break;
23638c8a9a5SSteve French 		}
23738c8a9a5SSteve French 		ret = kernel_accept(iface->ksmbd_socket, &client_sk,
23838c8a9a5SSteve French 				    SOCK_NONBLOCK);
23938c8a9a5SSteve French 		mutex_unlock(&iface->sock_release_lock);
24038c8a9a5SSteve French 		if (ret) {
24138c8a9a5SSteve French 			if (ret == -EAGAIN)
24238c8a9a5SSteve French 				/* check for new connections every 100 msecs */
24338c8a9a5SSteve French 				schedule_timeout_interruptible(HZ / 10);
24438c8a9a5SSteve French 			continue;
24538c8a9a5SSteve French 		}
24638c8a9a5SSteve French 
24738c8a9a5SSteve French 		if (server_conf.max_connections &&
24838c8a9a5SSteve French 		    atomic_inc_return(&active_num_conn) >= server_conf.max_connections) {
24938c8a9a5SSteve French 			pr_info_ratelimited("Limit the maximum number of connections(%u)\n",
25038c8a9a5SSteve French 					    atomic_read(&active_num_conn));
25138c8a9a5SSteve French 			atomic_dec(&active_num_conn);
25238c8a9a5SSteve French 			sock_release(client_sk);
25338c8a9a5SSteve French 			continue;
25438c8a9a5SSteve French 		}
25538c8a9a5SSteve French 
25638c8a9a5SSteve French 		ksmbd_debug(CONN, "connect success: accepted new connection\n");
25738c8a9a5SSteve French 		client_sk->sk->sk_rcvtimeo = KSMBD_TCP_RECV_TIMEOUT;
25838c8a9a5SSteve French 		client_sk->sk->sk_sndtimeo = KSMBD_TCP_SEND_TIMEOUT;
25938c8a9a5SSteve French 
26038c8a9a5SSteve French 		ksmbd_tcp_new_connection(client_sk);
26138c8a9a5SSteve French 	}
26238c8a9a5SSteve French 
26338c8a9a5SSteve French 	ksmbd_debug(CONN, "releasing socket\n");
26438c8a9a5SSteve French 	return 0;
26538c8a9a5SSteve French }
26638c8a9a5SSteve French 
26738c8a9a5SSteve French /**
26838c8a9a5SSteve French  * ksmbd_tcp_run_kthread() - start forker thread
26938c8a9a5SSteve French  * @iface: pointer to struct interface
27038c8a9a5SSteve French  *
27138c8a9a5SSteve French  * start forker thread(ksmbd/0) at module init time to listen
27238c8a9a5SSteve French  * on port 445 for new SMB connection requests. It creates per connection
27338c8a9a5SSteve French  * server threads(ksmbd/x)
27438c8a9a5SSteve French  *
27538c8a9a5SSteve French  * Return:	0 on success or error number
27638c8a9a5SSteve French  */
ksmbd_tcp_run_kthread(struct interface * iface)27738c8a9a5SSteve French static int ksmbd_tcp_run_kthread(struct interface *iface)
27838c8a9a5SSteve French {
27938c8a9a5SSteve French 	int rc;
28038c8a9a5SSteve French 	struct task_struct *kthread;
28138c8a9a5SSteve French 
28238c8a9a5SSteve French 	kthread = kthread_run(ksmbd_kthread_fn, (void *)iface, "ksmbd-%s",
28338c8a9a5SSteve French 			      iface->name);
28438c8a9a5SSteve French 	if (IS_ERR(kthread)) {
28538c8a9a5SSteve French 		rc = PTR_ERR(kthread);
28638c8a9a5SSteve French 		return rc;
28738c8a9a5SSteve French 	}
28838c8a9a5SSteve French 	iface->ksmbd_kthread = kthread;
28938c8a9a5SSteve French 
29038c8a9a5SSteve French 	return 0;
29138c8a9a5SSteve French }
29238c8a9a5SSteve French 
29338c8a9a5SSteve French /**
29438c8a9a5SSteve French  * ksmbd_tcp_readv() - read data from socket in given iovec
29538c8a9a5SSteve French  * @t:			TCP transport instance
29638c8a9a5SSteve French  * @iov_orig:		base IO vector
29738c8a9a5SSteve French  * @nr_segs:		number of segments in base iov
29838c8a9a5SSteve French  * @to_read:		number of bytes to read from socket
29938c8a9a5SSteve French  * @max_retries:	maximum retry count
30038c8a9a5SSteve French  *
30138c8a9a5SSteve French  * Return:	on success return number of bytes read from socket,
30238c8a9a5SSteve French  *		otherwise return error number
30338c8a9a5SSteve French  */
ksmbd_tcp_readv(struct tcp_transport * t,struct kvec * iov_orig,unsigned int nr_segs,unsigned int to_read,int max_retries)30438c8a9a5SSteve French static int ksmbd_tcp_readv(struct tcp_transport *t, struct kvec *iov_orig,
30538c8a9a5SSteve French 			   unsigned int nr_segs, unsigned int to_read,
30638c8a9a5SSteve French 			   int max_retries)
30738c8a9a5SSteve French {
30838c8a9a5SSteve French 	int length = 0;
30938c8a9a5SSteve French 	int total_read;
31038c8a9a5SSteve French 	unsigned int segs;
31138c8a9a5SSteve French 	struct msghdr ksmbd_msg;
31238c8a9a5SSteve French 	struct kvec *iov;
31338c8a9a5SSteve French 	struct ksmbd_conn *conn = KSMBD_TRANS(t)->conn;
31438c8a9a5SSteve French 
31538c8a9a5SSteve French 	iov = get_conn_iovec(t, nr_segs);
31638c8a9a5SSteve French 	if (!iov)
31738c8a9a5SSteve French 		return -ENOMEM;
31838c8a9a5SSteve French 
31938c8a9a5SSteve French 	ksmbd_msg.msg_control = NULL;
32038c8a9a5SSteve French 	ksmbd_msg.msg_controllen = 0;
32138c8a9a5SSteve French 
32238c8a9a5SSteve French 	for (total_read = 0; to_read; total_read += length, to_read -= length) {
32338c8a9a5SSteve French 		try_to_freeze();
32438c8a9a5SSteve French 
32538c8a9a5SSteve French 		if (!ksmbd_conn_alive(conn)) {
32638c8a9a5SSteve French 			total_read = -ESHUTDOWN;
32738c8a9a5SSteve French 			break;
32838c8a9a5SSteve French 		}
32938c8a9a5SSteve French 		segs = kvec_array_init(iov, iov_orig, nr_segs, total_read);
33038c8a9a5SSteve French 
33138c8a9a5SSteve French 		length = kernel_recvmsg(t->sock, &ksmbd_msg,
33238c8a9a5SSteve French 					iov, segs, to_read, 0);
33338c8a9a5SSteve French 
33438c8a9a5SSteve French 		if (length == -EINTR) {
33538c8a9a5SSteve French 			total_read = -ESHUTDOWN;
33638c8a9a5SSteve French 			break;
33738c8a9a5SSteve French 		} else if (ksmbd_conn_need_reconnect(conn)) {
33838c8a9a5SSteve French 			total_read = -EAGAIN;
33938c8a9a5SSteve French 			break;
34038c8a9a5SSteve French 		} else if (length == -ERESTARTSYS || length == -EAGAIN) {
34138c8a9a5SSteve French 			/*
34238c8a9a5SSteve French 			 * If max_retries is negative, Allow unlimited
34338c8a9a5SSteve French 			 * retries to keep connection with inactive sessions.
34438c8a9a5SSteve French 			 */
34538c8a9a5SSteve French 			if (max_retries == 0) {
34638c8a9a5SSteve French 				total_read = length;
34738c8a9a5SSteve French 				break;
34838c8a9a5SSteve French 			} else if (max_retries > 0) {
34938c8a9a5SSteve French 				max_retries--;
35038c8a9a5SSteve French 			}
35138c8a9a5SSteve French 
35238c8a9a5SSteve French 			usleep_range(1000, 2000);
35338c8a9a5SSteve French 			length = 0;
35438c8a9a5SSteve French 			continue;
35538c8a9a5SSteve French 		} else if (length <= 0) {
35638c8a9a5SSteve French 			total_read = length;
35738c8a9a5SSteve French 			break;
35838c8a9a5SSteve French 		}
35938c8a9a5SSteve French 	}
36038c8a9a5SSteve French 	return total_read;
36138c8a9a5SSteve French }
36238c8a9a5SSteve French 
36338c8a9a5SSteve French /**
36438c8a9a5SSteve French  * ksmbd_tcp_read() - read data from socket in given buffer
36538c8a9a5SSteve French  * @t:		TCP transport instance
36638c8a9a5SSteve French  * @buf:	buffer to store read data from socket
36738c8a9a5SSteve French  * @to_read:	number of bytes to read from socket
3685cf3fddbSYang Li  * @max_retries: number of retries if reading from socket fails
36938c8a9a5SSteve French  *
37038c8a9a5SSteve French  * Return:	on success return number of bytes read from socket,
37138c8a9a5SSteve French  *		otherwise return error number
37238c8a9a5SSteve French  */
ksmbd_tcp_read(struct ksmbd_transport * t,char * buf,unsigned int to_read,int max_retries)37338c8a9a5SSteve French static int ksmbd_tcp_read(struct ksmbd_transport *t, char *buf,
37438c8a9a5SSteve French 			  unsigned int to_read, int max_retries)
37538c8a9a5SSteve French {
37638c8a9a5SSteve French 	struct kvec iov;
37738c8a9a5SSteve French 
37838c8a9a5SSteve French 	iov.iov_base = buf;
37938c8a9a5SSteve French 	iov.iov_len = to_read;
38038c8a9a5SSteve French 
38138c8a9a5SSteve French 	return ksmbd_tcp_readv(TCP_TRANS(t), &iov, 1, to_read, max_retries);
38238c8a9a5SSteve French }
38338c8a9a5SSteve French 
ksmbd_tcp_writev(struct ksmbd_transport * t,struct kvec * iov,int nvecs,int size,bool need_invalidate,unsigned int remote_key)38438c8a9a5SSteve French static int ksmbd_tcp_writev(struct ksmbd_transport *t, struct kvec *iov,
38538c8a9a5SSteve French 			    int nvecs, int size, bool need_invalidate,
38638c8a9a5SSteve French 			    unsigned int remote_key)
38738c8a9a5SSteve French 
38838c8a9a5SSteve French {
38938c8a9a5SSteve French 	struct msghdr smb_msg = {.msg_flags = MSG_NOSIGNAL};
39038c8a9a5SSteve French 
39138c8a9a5SSteve French 	return kernel_sendmsg(TCP_TRANS(t)->sock, &smb_msg, iov, nvecs, size);
39238c8a9a5SSteve French }
39338c8a9a5SSteve French 
ksmbd_tcp_disconnect(struct ksmbd_transport * t)39438c8a9a5SSteve French static void ksmbd_tcp_disconnect(struct ksmbd_transport *t)
39538c8a9a5SSteve French {
39638c8a9a5SSteve French 	free_transport(TCP_TRANS(t));
39738c8a9a5SSteve French 	if (server_conf.max_connections)
39838c8a9a5SSteve French 		atomic_dec(&active_num_conn);
39938c8a9a5SSteve French }
40038c8a9a5SSteve French 
tcp_destroy_socket(struct socket * ksmbd_socket)40138c8a9a5SSteve French static void tcp_destroy_socket(struct socket *ksmbd_socket)
40238c8a9a5SSteve French {
40338c8a9a5SSteve French 	int ret;
40438c8a9a5SSteve French 
40538c8a9a5SSteve French 	if (!ksmbd_socket)
40638c8a9a5SSteve French 		return;
40738c8a9a5SSteve French 
40838c8a9a5SSteve French 	/* set zero to timeout */
40938c8a9a5SSteve French 	ksmbd_tcp_rcv_timeout(ksmbd_socket, 0);
41038c8a9a5SSteve French 	ksmbd_tcp_snd_timeout(ksmbd_socket, 0);
41138c8a9a5SSteve French 
41238c8a9a5SSteve French 	ret = kernel_sock_shutdown(ksmbd_socket, SHUT_RDWR);
41338c8a9a5SSteve French 	if (ret)
41438c8a9a5SSteve French 		pr_err("Failed to shutdown socket: %d\n", ret);
41538c8a9a5SSteve French 	sock_release(ksmbd_socket);
41638c8a9a5SSteve French }
41738c8a9a5SSteve French 
41838c8a9a5SSteve French /**
41938c8a9a5SSteve French  * create_socket - create socket for ksmbd/0
4205cf3fddbSYang Li  * @iface:      interface to bind the created socket to
42138c8a9a5SSteve French  *
42238c8a9a5SSteve French  * Return:	0 on success, error number otherwise
42338c8a9a5SSteve French  */
create_socket(struct interface * iface)42438c8a9a5SSteve French static int create_socket(struct interface *iface)
42538c8a9a5SSteve French {
42638c8a9a5SSteve French 	int ret;
42738c8a9a5SSteve French 	struct sockaddr_in6 sin6;
42838c8a9a5SSteve French 	struct sockaddr_in sin;
42938c8a9a5SSteve French 	struct socket *ksmbd_socket;
43038c8a9a5SSteve French 	bool ipv4 = false;
43138c8a9a5SSteve French 
43238c8a9a5SSteve French 	ret = sock_create(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &ksmbd_socket);
43338c8a9a5SSteve French 	if (ret) {
43438c8a9a5SSteve French 		if (ret != -EAFNOSUPPORT)
43538c8a9a5SSteve French 			pr_err("Can't create socket for ipv6, fallback to ipv4: %d\n", ret);
43638c8a9a5SSteve French 		ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP,
43738c8a9a5SSteve French 				  &ksmbd_socket);
43838c8a9a5SSteve French 		if (ret) {
43938c8a9a5SSteve French 			pr_err("Can't create socket for ipv4: %d\n", ret);
44038c8a9a5SSteve French 			goto out_clear;
44138c8a9a5SSteve French 		}
44238c8a9a5SSteve French 
44338c8a9a5SSteve French 		sin.sin_family = PF_INET;
44438c8a9a5SSteve French 		sin.sin_addr.s_addr = htonl(INADDR_ANY);
44538c8a9a5SSteve French 		sin.sin_port = htons(server_conf.tcp_port);
44638c8a9a5SSteve French 		ipv4 = true;
44738c8a9a5SSteve French 	} else {
44838c8a9a5SSteve French 		sin6.sin6_family = PF_INET6;
44938c8a9a5SSteve French 		sin6.sin6_addr = in6addr_any;
45038c8a9a5SSteve French 		sin6.sin6_port = htons(server_conf.tcp_port);
451b2ed5495SNamjae Jeon 
452b2ed5495SNamjae Jeon 		lock_sock(ksmbd_socket->sk);
453b2ed5495SNamjae Jeon 		ksmbd_socket->sk->sk_ipv6only = false;
454b2ed5495SNamjae Jeon 		release_sock(ksmbd_socket->sk);
45538c8a9a5SSteve French 	}
45638c8a9a5SSteve French 
45738c8a9a5SSteve French 	ksmbd_tcp_nodelay(ksmbd_socket);
45838c8a9a5SSteve French 	ksmbd_tcp_reuseaddr(ksmbd_socket);
45938c8a9a5SSteve French 
46038c8a9a5SSteve French 	ret = sock_setsockopt(ksmbd_socket,
46138c8a9a5SSteve French 			      SOL_SOCKET,
46238c8a9a5SSteve French 			      SO_BINDTODEVICE,
46338c8a9a5SSteve French 			      KERNEL_SOCKPTR(iface->name),
46438c8a9a5SSteve French 			      strlen(iface->name));
46538c8a9a5SSteve French 	if (ret != -ENODEV && ret < 0) {
46638c8a9a5SSteve French 		pr_err("Failed to set SO_BINDTODEVICE: %d\n", ret);
46738c8a9a5SSteve French 		goto out_error;
46838c8a9a5SSteve French 	}
46938c8a9a5SSteve French 
47038c8a9a5SSteve French 	if (ipv4)
47138c8a9a5SSteve French 		ret = kernel_bind(ksmbd_socket, (struct sockaddr *)&sin,
47238c8a9a5SSteve French 				  sizeof(sin));
47338c8a9a5SSteve French 	else
47438c8a9a5SSteve French 		ret = kernel_bind(ksmbd_socket, (struct sockaddr *)&sin6,
47538c8a9a5SSteve French 				  sizeof(sin6));
47638c8a9a5SSteve French 	if (ret) {
47738c8a9a5SSteve French 		pr_err("Failed to bind socket: %d\n", ret);
47838c8a9a5SSteve French 		goto out_error;
47938c8a9a5SSteve French 	}
48038c8a9a5SSteve French 
48138c8a9a5SSteve French 	ksmbd_socket->sk->sk_rcvtimeo = KSMBD_TCP_RECV_TIMEOUT;
48238c8a9a5SSteve French 	ksmbd_socket->sk->sk_sndtimeo = KSMBD_TCP_SEND_TIMEOUT;
48338c8a9a5SSteve French 
48438c8a9a5SSteve French 	ret = kernel_listen(ksmbd_socket, KSMBD_SOCKET_BACKLOG);
48538c8a9a5SSteve French 	if (ret) {
48638c8a9a5SSteve French 		pr_err("Port listen() error: %d\n", ret);
48738c8a9a5SSteve French 		goto out_error;
48838c8a9a5SSteve French 	}
48938c8a9a5SSteve French 
49038c8a9a5SSteve French 	iface->ksmbd_socket = ksmbd_socket;
49138c8a9a5SSteve French 	ret = ksmbd_tcp_run_kthread(iface);
49238c8a9a5SSteve French 	if (ret) {
49338c8a9a5SSteve French 		pr_err("Can't start ksmbd main kthread: %d\n", ret);
49438c8a9a5SSteve French 		goto out_error;
49538c8a9a5SSteve French 	}
49638c8a9a5SSteve French 	iface->state = IFACE_STATE_CONFIGURED;
49738c8a9a5SSteve French 
49838c8a9a5SSteve French 	return 0;
49938c8a9a5SSteve French 
50038c8a9a5SSteve French out_error:
50138c8a9a5SSteve French 	tcp_destroy_socket(ksmbd_socket);
50238c8a9a5SSteve French out_clear:
50338c8a9a5SSteve French 	iface->ksmbd_socket = NULL;
50438c8a9a5SSteve French 	return ret;
50538c8a9a5SSteve French }
50638c8a9a5SSteve French 
ksmbd_netdev_event(struct notifier_block * nb,unsigned long event,void * ptr)50738c8a9a5SSteve French static int ksmbd_netdev_event(struct notifier_block *nb, unsigned long event,
50838c8a9a5SSteve French 			      void *ptr)
50938c8a9a5SSteve French {
51038c8a9a5SSteve French 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
51138c8a9a5SSteve French 	struct interface *iface;
51238c8a9a5SSteve French 	int ret, found = 0;
51338c8a9a5SSteve French 
51438c8a9a5SSteve French 	switch (event) {
51538c8a9a5SSteve French 	case NETDEV_UP:
51638c8a9a5SSteve French 		if (netif_is_bridge_port(netdev))
51738c8a9a5SSteve French 			return NOTIFY_OK;
51838c8a9a5SSteve French 
51938c8a9a5SSteve French 		list_for_each_entry(iface, &iface_list, entry) {
52038c8a9a5SSteve French 			if (!strcmp(iface->name, netdev->name)) {
52138c8a9a5SSteve French 				found = 1;
52238c8a9a5SSteve French 				if (iface->state != IFACE_STATE_DOWN)
52338c8a9a5SSteve French 					break;
52438c8a9a5SSteve French 				ret = create_socket(iface);
52538c8a9a5SSteve French 				if (ret)
52638c8a9a5SSteve French 					return NOTIFY_OK;
52738c8a9a5SSteve French 				break;
52838c8a9a5SSteve French 			}
52938c8a9a5SSteve French 		}
53038c8a9a5SSteve French 		if (!found && bind_additional_ifaces) {
53138c8a9a5SSteve French 			iface = alloc_iface(kstrdup(netdev->name, GFP_KERNEL));
53238c8a9a5SSteve French 			if (!iface)
53338c8a9a5SSteve French 				return NOTIFY_OK;
53438c8a9a5SSteve French 			ret = create_socket(iface);
53538c8a9a5SSteve French 			if (ret)
53638c8a9a5SSteve French 				break;
53738c8a9a5SSteve French 		}
53838c8a9a5SSteve French 		break;
53938c8a9a5SSteve French 	case NETDEV_DOWN:
54038c8a9a5SSteve French 		list_for_each_entry(iface, &iface_list, entry) {
54138c8a9a5SSteve French 			if (!strcmp(iface->name, netdev->name) &&
54238c8a9a5SSteve French 			    iface->state == IFACE_STATE_CONFIGURED) {
54338c8a9a5SSteve French 				tcp_stop_kthread(iface->ksmbd_kthread);
54438c8a9a5SSteve French 				iface->ksmbd_kthread = NULL;
54538c8a9a5SSteve French 				mutex_lock(&iface->sock_release_lock);
54638c8a9a5SSteve French 				tcp_destroy_socket(iface->ksmbd_socket);
54738c8a9a5SSteve French 				iface->ksmbd_socket = NULL;
54838c8a9a5SSteve French 				mutex_unlock(&iface->sock_release_lock);
54938c8a9a5SSteve French 
55038c8a9a5SSteve French 				iface->state = IFACE_STATE_DOWN;
55138c8a9a5SSteve French 				break;
55238c8a9a5SSteve French 			}
55338c8a9a5SSteve French 		}
55438c8a9a5SSteve French 		break;
55538c8a9a5SSteve French 	}
55638c8a9a5SSteve French 
55738c8a9a5SSteve French 	return NOTIFY_DONE;
55838c8a9a5SSteve French }
55938c8a9a5SSteve French 
56038c8a9a5SSteve French static struct notifier_block ksmbd_netdev_notifier = {
56138c8a9a5SSteve French 	.notifier_call = ksmbd_netdev_event,
56238c8a9a5SSteve French };
56338c8a9a5SSteve French 
ksmbd_tcp_init(void)56438c8a9a5SSteve French int ksmbd_tcp_init(void)
56538c8a9a5SSteve French {
56638c8a9a5SSteve French 	register_netdevice_notifier(&ksmbd_netdev_notifier);
56738c8a9a5SSteve French 
56838c8a9a5SSteve French 	return 0;
56938c8a9a5SSteve French }
57038c8a9a5SSteve French 
tcp_stop_kthread(struct task_struct * kthread)57138c8a9a5SSteve French static void tcp_stop_kthread(struct task_struct *kthread)
57238c8a9a5SSteve French {
57338c8a9a5SSteve French 	int ret;
57438c8a9a5SSteve French 
57538c8a9a5SSteve French 	if (!kthread)
57638c8a9a5SSteve French 		return;
57738c8a9a5SSteve French 
57838c8a9a5SSteve French 	ret = kthread_stop(kthread);
57938c8a9a5SSteve French 	if (ret)
58038c8a9a5SSteve French 		pr_err("failed to stop forker thread\n");
58138c8a9a5SSteve French }
58238c8a9a5SSteve French 
ksmbd_tcp_destroy(void)58338c8a9a5SSteve French void ksmbd_tcp_destroy(void)
58438c8a9a5SSteve French {
58538c8a9a5SSteve French 	struct interface *iface, *tmp;
58638c8a9a5SSteve French 
58738c8a9a5SSteve French 	unregister_netdevice_notifier(&ksmbd_netdev_notifier);
58838c8a9a5SSteve French 
58938c8a9a5SSteve French 	list_for_each_entry_safe(iface, tmp, &iface_list, entry) {
59038c8a9a5SSteve French 		list_del(&iface->entry);
59138c8a9a5SSteve French 		kfree(iface->name);
59238c8a9a5SSteve French 		kfree(iface);
59338c8a9a5SSteve French 	}
59438c8a9a5SSteve French }
59538c8a9a5SSteve French 
alloc_iface(char * ifname)59638c8a9a5SSteve French static struct interface *alloc_iface(char *ifname)
59738c8a9a5SSteve French {
59838c8a9a5SSteve French 	struct interface *iface;
59938c8a9a5SSteve French 
60038c8a9a5SSteve French 	if (!ifname)
60138c8a9a5SSteve French 		return NULL;
60238c8a9a5SSteve French 
60338c8a9a5SSteve French 	iface = kzalloc(sizeof(struct interface), GFP_KERNEL);
60438c8a9a5SSteve French 	if (!iface) {
60538c8a9a5SSteve French 		kfree(ifname);
60638c8a9a5SSteve French 		return NULL;
60738c8a9a5SSteve French 	}
60838c8a9a5SSteve French 
60938c8a9a5SSteve French 	iface->name = ifname;
61038c8a9a5SSteve French 	iface->state = IFACE_STATE_DOWN;
61138c8a9a5SSteve French 	list_add(&iface->entry, &iface_list);
61238c8a9a5SSteve French 	mutex_init(&iface->sock_release_lock);
61338c8a9a5SSteve French 	return iface;
61438c8a9a5SSteve French }
61538c8a9a5SSteve French 
ksmbd_tcp_set_interfaces(char * ifc_list,int ifc_list_sz)61638c8a9a5SSteve French int ksmbd_tcp_set_interfaces(char *ifc_list, int ifc_list_sz)
61738c8a9a5SSteve French {
61838c8a9a5SSteve French 	int sz = 0;
61938c8a9a5SSteve French 
62038c8a9a5SSteve French 	if (!ifc_list_sz) {
62138c8a9a5SSteve French 		struct net_device *netdev;
62238c8a9a5SSteve French 
62338c8a9a5SSteve French 		rtnl_lock();
62438c8a9a5SSteve French 		for_each_netdev(&init_net, netdev) {
62538c8a9a5SSteve French 			if (netif_is_bridge_port(netdev))
62638c8a9a5SSteve French 				continue;
627*f75881f5SDan Carpenter 			if (!alloc_iface(kstrdup(netdev->name, GFP_KERNEL))) {
628*f75881f5SDan Carpenter 				rtnl_unlock();
62938c8a9a5SSteve French 				return -ENOMEM;
63038c8a9a5SSteve French 			}
631*f75881f5SDan Carpenter 		}
63238c8a9a5SSteve French 		rtnl_unlock();
63338c8a9a5SSteve French 		bind_additional_ifaces = 1;
63438c8a9a5SSteve French 		return 0;
63538c8a9a5SSteve French 	}
63638c8a9a5SSteve French 
63738c8a9a5SSteve French 	while (ifc_list_sz > 0) {
63838c8a9a5SSteve French 		if (!alloc_iface(kstrdup(ifc_list, GFP_KERNEL)))
63938c8a9a5SSteve French 			return -ENOMEM;
64038c8a9a5SSteve French 
64138c8a9a5SSteve French 		sz = strlen(ifc_list);
64238c8a9a5SSteve French 		if (!sz)
64338c8a9a5SSteve French 			break;
64438c8a9a5SSteve French 
64538c8a9a5SSteve French 		ifc_list += sz + 1;
64638c8a9a5SSteve French 		ifc_list_sz -= (sz + 1);
64738c8a9a5SSteve French 	}
64838c8a9a5SSteve French 
64938c8a9a5SSteve French 	bind_additional_ifaces = 0;
65038c8a9a5SSteve French 
65138c8a9a5SSteve French 	return 0;
65238c8a9a5SSteve French }
65338c8a9a5SSteve French 
65438c8a9a5SSteve French static struct ksmbd_transport_ops ksmbd_tcp_transport_ops = {
65538c8a9a5SSteve French 	.read		= ksmbd_tcp_read,
65638c8a9a5SSteve French 	.writev		= ksmbd_tcp_writev,
65738c8a9a5SSteve French 	.disconnect	= ksmbd_tcp_disconnect,
65838c8a9a5SSteve French };
659