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