1/*-
2 * Copyright (c) 2012 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30/*-
31 * Copyright (c) 1982, 1986, 1989, 1990, 1993
32 *	The Regents of the University of California.  All rights reserved.
33 *
34 * sendfile(2) and related extensions:
35 * Copyright (c) 1998, David Greenman. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 *    notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in the
44 *    documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 *    may be used to endorse or promote products derived from this software
47 *    without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 *	@(#)uipc_syscalls.c	8.4 (Berkeley) 2/21/94
62 */
63
64/*
65 * iSCSI Common Layer, kernel proxy part.
66 */
67
68#ifdef ICL_KERNEL_PROXY
69
70#include <sys/cdefs.h>
71__FBSDID("$FreeBSD$");
72
73#include <sys/param.h>
74#include <sys/capsicum.h>
75#include <sys/condvar.h>
76#include <sys/conf.h>
77#include <sys/kernel.h>
78#include <sys/kthread.h>
79#include <sys/malloc.h>
80#include <sys/proc.h>
81#include <sys/socket.h>
82#include <sys/socketvar.h>
83#include <sys/sx.h>
84#include <sys/systm.h>
85#include <netinet/in.h>
86#include <netinet/tcp.h>
87
88#include <dev/iscsi/icl.h>
89
90struct icl_listen_sock {
91	TAILQ_ENTRY(icl_listen_sock)	ils_next;
92	struct icl_listen		*ils_listen;
93	struct socket			*ils_socket;
94	bool				ils_running;
95	int				ils_id;
96};
97
98struct icl_listen	{
99	TAILQ_HEAD(, icl_listen_sock)	il_sockets;
100	struct sx			il_lock;
101	void				(*il_accept)(struct socket *,
102					    struct sockaddr *, int);
103};
104
105static MALLOC_DEFINE(M_ICL_PROXY, "ICL_PROXY", "iSCSI common layer proxy");
106
107int
108icl_soft_proxy_connect(struct icl_conn *ic, int domain, int socktype,
109    int protocol, struct sockaddr *from_sa, struct sockaddr *to_sa)
110{
111	struct socket *so;
112	int error;
113	int interrupted = 0;
114
115	error = socreate(domain, &so, socktype, protocol,
116	    curthread->td_ucred, curthread);
117	if (error != 0)
118		return (error);
119
120	if (from_sa != NULL) {
121		error = sobind(so, from_sa, curthread);
122		if (error != 0) {
123			soclose(so);
124			return (error);
125		}
126	}
127
128	error = soconnect(so, to_sa, curthread);
129	if (error != 0) {
130		soclose(so);
131		return (error);
132	}
133
134	SOCK_LOCK(so);
135	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
136		error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
137		    "icl_connect", 0);
138		if (error) {
139			if (error == EINTR || error == ERESTART)
140				interrupted = 1;
141			break;
142		}
143	}
144	if (error == 0) {
145		error = so->so_error;
146		so->so_error = 0;
147	}
148	SOCK_UNLOCK(so);
149
150	if (error != 0) {
151		soclose(so);
152		return (error);
153	}
154
155	error = icl_soft_handoff_sock(ic, so);
156	if (error != 0)
157		soclose(so);
158
159	return (error);
160}
161
162struct icl_listen *
163icl_listen_new(void (*accept_cb)(struct socket *, struct sockaddr *, int))
164{
165	struct icl_listen *il;
166
167	il = malloc(sizeof(*il), M_ICL_PROXY, M_ZERO | M_WAITOK);
168	TAILQ_INIT(&il->il_sockets);
169	sx_init(&il->il_lock, "icl_listen");
170	il->il_accept = accept_cb;
171
172	return (il);
173}
174
175void
176icl_listen_free(struct icl_listen *il)
177{
178	struct icl_listen_sock *ils;
179
180	sx_xlock(&il->il_lock);
181	while (!TAILQ_EMPTY(&il->il_sockets)) {
182		ils = TAILQ_FIRST(&il->il_sockets);
183		while (ils->ils_running) {
184			ICL_DEBUG("waiting for accept thread to terminate");
185			sx_xunlock(&il->il_lock);
186			SOLISTEN_LOCK(ils->ils_socket);
187			ils->ils_socket->so_error = ENOTCONN;
188			SOLISTEN_UNLOCK(ils->ils_socket);
189			wakeup(&ils->ils_socket->so_timeo);
190			pause("icl_unlisten", 1 * hz);
191			sx_xlock(&il->il_lock);
192		}
193
194		TAILQ_REMOVE(&il->il_sockets, ils, ils_next);
195		soclose(ils->ils_socket);
196		free(ils, M_ICL_PROXY);
197	}
198	sx_xunlock(&il->il_lock);
199
200	free(il, M_ICL_PROXY);
201}
202
203/*
204 * XXX: Doing accept in a separate thread in each socket might not be the
205 * best way to do stuff, but it's pretty clean and debuggable - and you
206 * probably won't have hundreds of listening sockets anyway.
207 */
208static void
209icl_accept_thread(void *arg)
210{
211	struct icl_listen_sock *ils;
212	struct socket *head, *so;
213	struct sockaddr *sa;
214	int error;
215
216	ils = arg;
217	head = ils->ils_socket;
218
219	ils->ils_running = true;
220
221	for (;;) {
222		SOLISTEN_LOCK(head);
223		error = solisten_dequeue(head, &so, 0);
224		if (error == ENOTCONN) {
225			/*
226			 * XXXGL: ENOTCONN is our mark from icl_listen_free().
227			 * Neither socket code, nor msleep(9) may return it.
228			 */
229			ICL_DEBUG("terminating");
230			ils->ils_running = false;
231			kthread_exit();
232			return;
233		}
234		if (error) {
235			ICL_WARN("solisten_dequeue error %d", error);
236			continue;
237		}
238
239		sa = NULL;
240		error = soaccept(so, &sa);
241		if (error != 0) {
242			ICL_WARN("soaccept error %d", error);
243			if (sa != NULL)
244				free(sa, M_SONAME);
245			soclose(so);
246			continue;
247		}
248
249		(ils->ils_listen->il_accept)(so, sa, ils->ils_id);
250	}
251}
252
253static int
254icl_listen_add_tcp(struct icl_listen *il, int domain, int socktype,
255    int protocol, struct sockaddr *sa, int portal_id)
256{
257	struct icl_listen_sock *ils;
258	struct socket *so;
259	struct sockopt sopt;
260	int error, one = 1;
261
262	error = socreate(domain, &so, socktype, protocol,
263	    curthread->td_ucred, curthread);
264	if (error != 0) {
265		ICL_WARN("socreate failed with error %d", error);
266		return (error);
267	}
268
269	sopt.sopt_dir = SOPT_SET;
270	sopt.sopt_level = SOL_SOCKET;
271	sopt.sopt_name = SO_REUSEADDR;
272	sopt.sopt_val = &one;
273	sopt.sopt_valsize = sizeof(one);
274	sopt.sopt_td = NULL;
275	error = sosetopt(so, &sopt);
276	if (error != 0) {
277		ICL_WARN("failed to set SO_REUSEADDR with error %d", error);
278		soclose(so);
279		return (error);
280	}
281
282	error = sobind(so, sa, curthread);
283	if (error != 0) {
284		ICL_WARN("sobind failed with error %d", error);
285		soclose(so);
286		return (error);
287	}
288
289	error = solisten(so, -1, curthread);
290	if (error != 0) {
291		ICL_WARN("solisten failed with error %d", error);
292		soclose(so);
293		return (error);
294	}
295
296	ils = malloc(sizeof(*ils), M_ICL_PROXY, M_ZERO | M_WAITOK);
297	ils->ils_listen = il;
298	ils->ils_socket = so;
299	ils->ils_id = portal_id;
300
301	error = kthread_add(icl_accept_thread, ils, NULL, NULL, 0, 0, "iclacc");
302	if (error != 0) {
303		ICL_WARN("kthread_add failed with error %d", error);
304		soclose(so);
305		free(ils, M_ICL_PROXY);
306
307		return (error);
308	}
309
310	sx_xlock(&il->il_lock);
311	TAILQ_INSERT_TAIL(&il->il_sockets, ils, ils_next);
312	sx_xunlock(&il->il_lock);
313
314	return (0);
315}
316
317int
318icl_listen_add(struct icl_listen *il, bool rdma, int domain, int socktype,
319    int protocol, struct sockaddr *sa, int portal_id)
320{
321
322	if (rdma) {
323		ICL_DEBUG("RDMA not supported");
324		return (EOPNOTSUPP);
325	}
326
327
328	return (icl_listen_add_tcp(il, domain, socktype, protocol, sa,
329	    portal_id));
330}
331
332int
333icl_listen_remove(struct icl_listen *il, struct sockaddr *sa)
334{
335
336	/*
337	 * XXX
338	 */
339
340	return (EOPNOTSUPP);
341}
342
343#endif /* ICL_KERNEL_PROXY */
344